Advertisement
leomaster

100 byte bf compiler in asm

Jun 30th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;non-compliant (dialect) version, non-commands are not ignored, only 100 bytes
  2.  
  3. [bits 16]  
  4. [org 0x100]  
  5. ; assume bp=091e used  
  6. ; assume di=fffe  
  7. ; assume si=0100  
  8. ; assume dx=cs (see here)  
  9. ; assume cx=00ff  
  10. ; assume bx=0000  
  11. ; assume ax=0000 used (ah)  
  12. ; assume sp=fffe  
  13. start:
  14.         mov al, code_left - start  
  15. code_start:
  16.         mov ch, 0x7f ; allow bigger programs  
  17.         mov bx, cx  
  18.         mov di, cx  
  19.         rep stosb  
  20.         mov bp, find_right + start - code_start ;cache loop head for smaller compiled programs  
  21.         jmp code_start_end  
  22. find_right:
  23.         pop si  
  24.         dec si  
  25.         dec si ;point to loop head  
  26.         cmp [bx], cl  
  27.         jne loop_right_end  
  28. loop_right:
  29.         lodsb  
  30.         cmp al, 0xD5 ; the "bp" part of "call bp" (because 0xFF is not unique, watch for additional '[')  
  31.         jne loop_left  
  32.         inc cx  
  33. loop_left:
  34.         cmp al, 0xC3 ; ret (watch for ']')  
  35.         jne loop_right  
  36.         loop loop_right ;all brackets matched when cx==0  
  37.         db 0x3c ;cmp al, xx (mask push)  
  38. loop_right_end:
  39.         push si  
  40.         lodsw ; skip "call" or dummy "dec" instruction, depending on context  
  41.         push si  
  42. code_sqright:
  43.         ret  
  44. code_dec:
  45.         dec byte [bx]  
  46. code_start_end:
  47.         db '$' ;end DOS string, also "and al, xx"  
  48. code_inc:
  49.         inc byte [bx]  
  50.         db '$'  
  51. code_right:
  52.         inc bx ;al -> 2  
  53.         db '$'  
  54. code_left:
  55.         dec bx  
  56.         db '$'  
  57. code_sqleft:
  58.         call bp  
  59.         db '$'  
  60. ; create lookup table  
  61. real_start:
  62.         sub byte [bx+'>'], al ;point to code_right  
  63.         add byte [bx+'['], al ;point to code_sqleft  
  64.         mov byte [bx+']'], code_sqright - start  
  65.         lea sp, [bx+45+2] ;'+' + 4 (2b='+', 2c=',', 2d='-', 2e='.')  
  66.         push (code_dec - start) + (code_dot - start) * 256  
  67.         push (code_inc - start) + (code_comma - start) * 256  
  68. pre_write:
  69.         mov ah, code_start >> 8  
  70.         xchg dx, ax  
  71. ; write  
  72.         mov ah, 9  
  73.         int 0x21  
  74. ; read  
  75. code_comma:
  76.         mov dl, 0xff  
  77.         db 0x3d ; cmp ax, xxxx (mask mov)  
  78. code_dot:
  79.         mov dl, [bx]  
  80.         mov ah, 6  
  81.         int 0x21  
  82.         mov [bx], al  
  83.         db '$'  
  84.         db 0xff ; parameter for '$', doubles as test for zero  
  85. ; switch  
  86.         xlatb  
  87.         jne pre_write  
  88.   ; next two lines can also be removed  
  89.   ; if the program ends with extra ']'  
  90.   ; and then we are at 96 bytes... :-)  
  91. the_end:
  92.         mov dl, 0xC3  
  93.         int 0x21  
  94.         int 0x20
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement