Guest User

Untitled

a guest
Nov 6th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. Inspired by Gynvael Coldwind's 125 bytes version
  2.  
  3. Assemble with Yasm
  4. non-compliant (dialect) version, non-commands are not ignored
  5.  
  6. [bits 16]
  7. [org 0x100]
  8. ; assume bp=091e used
  9. ; assume di=fffe
  10. ; assume si=0100
  11. ; assume dx=cs (see here)
  12. ; assume cx=00ff
  13. ; assume bx=0000
  14. ; assume ax=0000 used (ah)
  15. ; assume sp=fffe
  16. start: mov al, code_left - start
  17. code_start: mov ch, 0x7f ; allow bigger programs
  18. mov bx, cx
  19. mov di, cx
  20. rep stosb
  21. mov bp, find_right + start - code_start ;cache loop head for smaller compiled programs
  22. jmp code_start_end
  23. find_right: pop si
  24. dec si
  25. dec si ;point to loop head
  26. cmp [bx], cl
  27. jne loop_right_end
  28. loop_right: lodsb
  29. cmp al, 0xD5 ; the "bp" part of "call bp" (because 0xFF is not unique, watch for additional '[')
  30. jne loop_left
  31. inc cx
  32. loop_left: cmp al, 0xC3 ; ret (watch for ']')
  33. jne loop_right
  34. loop loop_right ;all brackets matched when cx==0
  35. db 0x3c ;cmp al, xx (mask push)
  36. loop_right_end: push si
  37. lodsw ; skip "call" or dummy "dec" instruction, depending on context
  38. push si
  39. code_sqright: ret
  40. code_dec: dec byte [bx]
  41. code_start_end: db '$' ;end DOS string, also "and al, xx"
  42. code_inc: inc byte [bx]
  43. db '$'
  44. code_right: inc bx ;al -> 2
  45. db '$'
  46. code_left: dec bx
  47. db '$'
  48. code_sqleft: call bp
  49. db '$'
  50. ; create lookup table
  51. real_start: sub byte [bx+'>'], al ;point to code_right
  52. add byte [bx+'['], al ;point to code_sqleft
  53. mov byte [bx+']'], code_sqright - start
  54. lea sp, [bx+45+2] ;'+' + 4 (2b='+', 2c=',', 2d='-', 2e='.')
  55. push (code_dec - start) + (code_dot - start) * 256
  56. push (code_inc - start) + (code_comma - start) * 256
  57. pre_write: mov ah, code_start >> 8
  58. xchg dx, ax
  59. ; write
  60. mov ah, 9
  61. int 0x21
  62. ; read
  63. code_comma: mov dl, 0xff
  64. db 0x3d ; cmp ax, xxxx (mask mov)
  65. code_dot: mov dl, [bx]
  66. mov ah, 6
  67. int 0x21
  68. mov [bx], al
  69. db '$'
  70. db 0xff ; parameter for '$', doubles as test for zero
  71. ; switch
  72. xlatb
  73. jne pre_write
  74. ; next two lines can also be removed
  75. ; if the program ends with extra ']'
  76. ; and then we are at 96 bytes... :-)
  77. the_end: mov dl, 0xC3
  78. int 0x21
  79. int 0x20
  80. compliant version, non-commands are ignored, but 104 bytes long
  81.  
  82. [bits 16]
  83. [org 0x100]
  84. ; assume bp=091e used
  85. ; assume di=fffe
  86. ; assume si=0100
  87. ; assume dx=cs (see here)
  88. ; assume cx=00ff
  89. ; assume bx=0000
  90. ; assume ax=0000 used (ah)
  91. ; assume sp=fffe
  92. start: mov al, code_nothing - start
  93. code_start: mov ch, 0x7f ; allow bigger programs
  94. mov bx, cx
  95. mov di, cx
  96. rep stosb
  97. mov bp, find_right + start - code_start ;cache loop head for smaller compiled programs
  98. jmp code_start_end
  99. find_right: pop si
  100. dec si
  101. dec si ;point to loop head
  102. cmp [bx], cl
  103. jne loop_right_end
  104. loop_right: lodsb
  105. cmp al, 0xD5 ; the "bp" part of "call bp" (because 0xFF is not unique, watch for additional '[')
  106. jne loop_left
  107. inc cx
  108. loop_left: cmp al, 0xC3 ; ret (watch for ']')
  109. jne loop_right
  110. loop loop_right ;all brackets matched when cx==0
  111. db 0x3c ;cmp al, xx (mask push)
  112. loop_right_end: push si
  113. lodsw ; skip "call" or dummy "dec" instruction, depending on context
  114. push si
  115. code_sqright: ret
  116. code_dec: dec byte [bx]
  117. code_start_end: db '$' ;end DOS string, also "and al, xx"
  118. code_inc: inc byte [bx]
  119. db '$'
  120. code_right: inc bx ;al -> 2
  121. code_nothing: db '$'
  122. code_left: dec bx
  123. db '$'
  124. code_sqleft: call bp
  125. db '$'
  126. ; create lookup table
  127. real_start: inc byte [bx+'<'] ;point to code_left
  128. dec byte [bx+'>'] ;point to code_right
  129. mov byte [bx+'['], code_sqleft - start
  130. mov byte [bx+']'], code_sqright - start
  131. lea sp, [bx+45+2] ;'+' + 4 (2b='+', 2c=',', 2d='-', 2e='.')
  132. push (code_dec - start) + (code_dot - start) * 256
  133. push (code_inc - start) + (code_comma - start) * 256
  134. pre_write: mov ah, code_start >> 8
  135. xchg dx, ax
  136. ; write
  137. mov ah, 9
  138. int 0x21
  139. ; read
  140. code_comma: mov dl, 0xff
  141. db 0x3d ; cmp ax, xxxx (mask mov)
  142. code_dot: mov dl, [bx]
  143. mov ah, 6
  144. int 0x21
  145. mov [bx], al
  146. db '$'
  147. db 0xff ; parameter for '$', doubles as test for zero
  148. ; switch
  149. xlatb
  150. jne pre_write
  151. ; next two lines can also be removed
  152. ; if the program ends with extra ']'
  153. ; and then we are at 100 bytes... :-)
  154. the_end: mov dl, 0xC3
  155. int 0x21
  156. int 0x20
Advertisement
Add Comment
Please, Sign In to add comment