fork download
  1. SECTION .data
  2. fopen_errm: db "Can't open", 0x0a
  3. fopen_errl: equ $-fopen_errm
  4.  
  5. fread_errm: db "Can't read", 0x0a
  6. fread_errl: equ $-fread_errm
  7.  
  8. dbg_msg: db "dbg msg", 0x0a
  9. dbg_msg_l: equ $-dbg_msg
  10.  
  11. SECTION .bss
  12. code: resb 4096 ; reserve 4K for code
  13. mem: resb 4096 ; reserve 4K for memory
  14. mem_p: resw 1
  15. curr: resw 1 ; reserve 16 bits for current index
  16.  
  17. SECTION .text
  18. global _start
  19.  
  20. _start:
  21. ; initialise stuff
  22. MOV word [curr], 0x00 ; start at 0
  23. MOV word [mem_p], 0x00
  24.  
  25. ; read file
  26. open:
  27. ; open it
  28. POP ebx ; argc
  29. POP ebx ; arg #1 (executable name)
  30. POP ebx ; arg #2 (filename)
  31. MOV eax, 0x05
  32. XOR ecx, ecx ; O_RDONLY = 0
  33. XOR edx, edx ; mode is ignored when O_CREAT is not specified
  34. INT 0x80
  35.  
  36. TEST eax, eax
  37. JNS read
  38. JMP fopen_err
  39.  
  40. ; read into buffer
  41. read:
  42. MOV ebx, eax
  43. MOV eax, 0x03
  44. MOV ecx, code
  45. MOV edx, 4096
  46. INT 0x80
  47.  
  48. TEST eax, eax ; check for errors / EOF
  49. JMP run_code ; if EOF, then write our buffer out.
  50. ; JS fread_err ; if read failed, we exit.
  51.  
  52. ; we failed reading the file
  53. fread_err:
  54. MOV eax, 0x04
  55. MOV ebx, 0x01
  56. MOV ecx, fread_errm
  57. MOV edx, fread_errl
  58. INT 0x80
  59. JMP exit
  60.  
  61. fopen_err:
  62. MOV eax, 0x04
  63. MOV ebx, 0x01
  64. MOV ecx, fopen_errm
  65. MOV edx, fopen_errl
  66. INT 0x80
  67. JMP exit
  68.  
  69. run_code:
  70. loop_start:
  71. MOVZX esi, word [curr]
  72. CMP byte [code+esi], '+'
  73. JE case_plus
  74.  
  75. CMP byte [code+esi], '-'
  76. JE case_minus
  77.  
  78. CMP byte [code+esi], '>'
  79. JE case_up
  80.  
  81. CMP byte [code+esi], '<'
  82. JE case_down
  83.  
  84. CMP byte [code+esi], '.'
  85. JE case_dot
  86.  
  87. CMP byte [code+esi], ','
  88. JE case_comma
  89.  
  90. CMP byte [code+esi], '['
  91. JE case_open
  92.  
  93. CMP byte [code+esi], ']'
  94. JE case_close
  95.  
  96. JMP chk_loop ; was not a valid brainfuck char
  97.  
  98. case_plus:
  99. MOVZX esi, word [mem_p]
  100. ADD byte [mem+esi], 0x01
  101.  
  102. JMP chk_loop
  103.  
  104. case_minus:
  105. MOVZX esi, word [mem_p]
  106. SUB byte [mem+esi], 0x01
  107.  
  108. JMP chk_loop
  109.  
  110. case_up:
  111. ADD word [mem_p], 0x01
  112. JMP chk_loop
  113.  
  114. case_down:
  115. SUB word [mem_p], 0x01
  116. JMP chk_loop
  117.  
  118. case_dot:
  119. MOV eax, 0x04
  120. MOV ebx, 0x01
  121. MOVZX esi, word [mem_p]
  122. ADD esi, mem
  123. MOV ecx, esi
  124. MOV edx, 0x01
  125. INT 0x80
  126. JMP chk_loop
  127.  
  128. case_comma:
  129. MOV eax, 0x03
  130. MOV ebx, 0x01
  131. MOVZX esi, word [mem_p]
  132. ADD esi, mem
  133. MOV ecx, esi
  134. MOV edx, 0x01
  135. INT 0x80
  136.  
  137. ; flush
  138. PUSH ecx
  139. MOV eax, 0x03
  140. MOV ebx, 0x01
  141. MOV ecx, esp
  142. MOV edx, 0x01
  143. INT 0x80
  144. POP ecx
  145.  
  146. case_open:
  147. MOVZX esi, word [mem_p]
  148. ADD esi, mem
  149. CMP word [esi], 0x00
  150. JE skip_loop
  151. JMP chk_loop
  152.  
  153. skip_loop:
  154. PUSH 0x00 ; loop depth
  155. XOR ebp, ebp
  156. MOV bp, word [curr]
  157.  
  158. skiploop_it:
  159. ADD ebp, 0x01
  160. MOV esi, ebp
  161. ADD esi, code
  162. CMP byte [esi], '['
  163. JE skiploop_inc_d
  164. CMP byte [esi], ']'
  165. JE skiploop_dec_d
  166.  
  167. skiploop_inc_d:
  168. ADD word [esp], 0x01
  169. JMP skiploop_chk
  170.  
  171. skiploop_dec_d:
  172. SUB word [esp], 0x01
  173. CMP word [esp], 0x00
  174. JLE skiploop_done
  175. JMP skiploop_chk
  176.  
  177. skiploop_chk:
  178. CMP ebp, 4096
  179. JL skiploop_it
  180.  
  181. skiploop_done:
  182. MOV eax, ebp
  183. SUB ax, word [curr]
  184. ADD word [curr], ax
  185. ADD word [curr], 0x01
  186.  
  187. JMP chk_loop
  188.  
  189. case_close:
  190. XOR esi, esi
  191. MOVZX esi, word [mem_p]
  192. ADD esi, mem
  193.  
  194. MOV eax, 0x04
  195. MOV ebx, 0x01
  196. MOV ecx, esi
  197. MOV edx, 0x01
  198. INT 0x80
  199.  
  200. SUB esi, mem
  201.  
  202.  
  203. CMP byte [mem+esi], 0x00
  204. JE chk_loop
  205.  
  206. XOR ebp, ebp
  207. MOV bp, word [curr]
  208.  
  209. revert_loop:
  210. SUB ebp, 0x01
  211. MOV esi, ebp
  212. ADD esi, code
  213.  
  214. MOV eax, 0x04
  215. MOV ebx, 0x01
  216. MOV ecx, esi
  217. MOV edx, 0x01
  218. ;INT 0x80
  219.  
  220. PUSH 0x0a
  221. MOV eax, 0x04
  222. MOV ebx, 0x01
  223. MOV ecx, esp
  224. MOV edx, 0x01
  225. INT 0x80
  226. POP ecx
  227.  
  228.  
  229.  
  230. CMP byte [esi], '['
  231. JE revert_done
  232.  
  233. revertloop_chk:
  234. CMP ebp, 0x01
  235. JG revert_loop
  236. JMP chk_loop
  237.  
  238. revert_done:
  239. MOV eax, 0x04
  240. MOV ebx, 0x01
  241. MOV ecx, dbg_msg
  242. MOV edx, dbg_msg_l
  243. ;INT 0x80
  244.  
  245. MOV eax, [curr]
  246. SUB eax, ebp
  247. MOV word [curr], ax
  248.  
  249. chk_loop:
  250. ADD word [curr], 0x01
  251. CMP word [curr], 4096
  252. JNE loop_start
  253.  
  254. exit:
  255. MOV eax, 0x04
  256. MOV ebx, 0x01
  257. PUSH 0x0a
  258. MOV ecx, esp
  259. MOV edx, 0x01
  260. INT 0x80
  261. POP ecx
  262.  
  263. MOV eax, 0x01
  264. MOV ebx, 0x00
  265. INT 0x80
  266.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Can't open