>+>+>+>+>++<[>[<+++>- >>>>> >+>+>+>+>++<[>[<+++>- >>>>> >+>+>+>+>++<[>[<+++>- >>>>> >+>+>+>+>++<[>[<+++>- >>>>> +++[->+++++<]>[-]< <<<<< ]<<]>[-] <<<<< ]<<]>[-] <<<<< ]<<]>[-] <<<<< ]<<]>.
Var Stack : Pointer; Begin GetMem(Stack,30000); // brainfuck - 30000 End;
mov esi, Stack
Brainfuck | Assembler | Opcode |
+ | inc byte [esi] | FE 06 |
- | dec byte [esi] | FE 0E |
> | inc esi | 46 |
< | dec esi | 4E |
[ | cmp byte [esi], 0 | 80 3E 00 |
je "] +1" | 0F 84 xx xx xx xx or 74 xx | |
] | jmp "[" | E9 xx xx xx xx or EB xx |
procedure WriteChar(S: Char); begin Write(S); end; function ReadChar: Char; var c : char; begin Read(c ); ReadChar := c; end;
mov edx, offset WriteChar
mov ebx, offset ReadChar
mov al,[esi] 8A 06 ; AL cbw 66 98 ; EAX push eax 50 ; () call edx FF D2 ; WriteChar
call ebx FF D3; ReadChar mov [esi],al 88 06;
Var ExBuf : Array [1..65535] of Byte; Ptr : Word; Tmp : LongInt;
ptr := 1; ExBuf[ptr] := $BE; inc(ptr); // mov esi,Stack asm mov edx,Stack mov Tmp,edx end; Move(Tmp,ExBuf[ptr],4); inc(ptr,4); ExBuf[ptr] := $BA; inc(ptr); // mov edx,offset WriteChar asm mov edx, offset WriteChar mov Tmp, edx end; Move(Tmp,ExBuf[ptr],4); inc(ptr,4); ExBuf[ptr] := $BB; inc(ptr); // mov ebx,offset ReadChar asm mov edx, offset ReadChar mov Tmp, edx end; Move(Tmp,ExBuf[ptr],4); inc(ptr,4);
Procedure JITCode(S: Char); Begin Case S of '+': Begin ExBuf[ptr] := $FE; //inc byte ptr [esi] ExBuf[ptr+1] := $06; Inc(ptr,2); End; '-': Begin ExBuf[ptr] := $FE; //dec byte ptr [esi] ExBuf[ptr+1] := $0E; Inc(ptr,2); End; '>': Begin ExBuf[ptr] := $46; //inc esi Inc(ptr); End; '<': Begin ExBuf[ptr] := $4E; //dec esi Inc(ptr); End; '.': Begin ExBuf[ptr] := $8A; //mov al,[esi] ExBuf[ptr+1] := $06; Inc(ptr,2); ExBuf[ptr] := $66; //cbw ExBuf[ptr+1] := $98; Inc(ptr,2); ExBuf[ptr] := $50; //push eax Inc(ptr); ExBuf[ptr] := $FF; //call edx ExBuf[ptr+1] := $D2; Inc(ptr,2); End; ',': Begin ExBuf[ptr] := $FF; //call ReadChar ExBuf[ptr+1] := $D3; Inc(ptr,2); ExBuf[ptr] := $88; //mov [esi],al ExBuf[ptr+1] := $06; Inc(ptr,2); End; End;
'[': Begin ExBuf[ptr] := $80; //cmp byte ptr [esi],0 ExBuf[ptr+1] := $3E; ExBuf[ptr+2] := $00; Inc(ptr,3); ExBuf[ptr] := $0F; //je near xxx ExBuf[ptr+1] := $84; Inc(ptr,2); bstack[bcnt] := ptr; inc(bcnt); Inc(ptr,4); End; ']': Begin Tmp := ptr - bstack[bcnt-1]; If Tmp > 122 then begin ExBuf[ptr] := $E9; //jmp Inc(ptr); Inc(Tmp); Move(Tmp,ExBuf[bstack[bcnt-1]],4); Tmp := -Tmp-9; Move(Tmp,ExBuf[ptr],4); Inc(ptr,4); Dec(bcnt); end else begin ExBuf[ptr] := $EB; //jmp short Inc(ptr); ExBuf[bstack[bcnt-1]-2] := $74; // je short xxx Dec(Tmp,2); Move(Tmp,ExBuf[bstack[bcnt-1]-1],1); Tmp := -Tmp-5; Move(Tmp,ExBuf[ptr],1); Inc(ptr); Move(ExBuf[bstack[bcnt-1]+4],ExBuf[bstack[bcnt-1]],ptr-bstack[bcnt-1]-4); Dec(ptr,4); Dec(bcnt); end; End;
ExBuf[ptr] := $C3; //retn
asm mov edx,offset ExBuf call edx end;
'i': Begin ExBuf[ptr] := $80; //add byte ptr [esi],xx ExBuf[ptr+1] := $06; ExBuf[ptr+2] := Ord(Buf[i+1]); Inc(i); Inc(ptr,3); End; 'd': Begin ExBuf[ptr] := $80; //sub byte ptr [esi],xx ExBuf[ptr+1] := $2E; ExBuf[ptr+2] := Ord(Buf[i+1]); Inc(i); Inc(ptr,3); End; 'n': Begin ExBuf[ptr] := $83; //add esi,xx ExBuf[ptr+1] := $C6; ExBuf[ptr+2] := Ord(Buf[i+1]); Inc(i); Inc(ptr,3); End; 'p': Begin ExBuf[ptr] := $83; //sub esi,xx ExBuf[ptr+1] := $EE; ExBuf[ptr+2] := Ord(Buf[i+1]); Inc(i); Inc(ptr,3); End; 'z': Begin ExBuf[ptr] := $C6; //mov byte ptr [esi],0 ExBuf[ptr+1] := $06; ExBuf[ptr+2] := $00; Inc(ptr,3); End;
Source: https://habr.com/ru/post/113339/
All Articles