05-05-2013, 07:35 AM
babababababa
My CTRL-V:
SECTION .data
fopen_errm: db "Can't open", 0x0a
fopen_errl: equ $-fopen_errm
fread_errm: db "Can't read", 0x0a
fread_errl: equ $-fread_errm
dbg_msg: db "dbg msg", 0x0a
dbg_msg_l equ $-dbg_msg
SECTION .bss
code: resb 4096 ; reserve 4K for code
mem: resb 4096 ; reserve 4K for memory
mem_p: resw 1
curr: resw 1 ; reserve 16 bits for current index
SECTION .text
global _start
_start:
; initialise stuff
MOV word [curr], 0x00 ; start at 0
MOV word [mem_p], 0x00
; read file
open:
; open it
POP ebx ; argc
POP ebx ; arg #1 (executable name)
POP ebx ; arg #2 (filename)
MOV eax, 0x05
XOR ecx, ecx ; O_RDONLY = 0
XOR edx, edx ; mode is ignored when O_CREAT is not specified
INT 0x80
TEST eax, eax
JNS read
JMP fopen_err
; read into buffer
read:
MOV ebx, eax
MOV eax, 0x03
MOV ecx, code
MOV edx, 4096
INT 0x80
TEST eax, eax ; check for errors / EOF
JMP run_code ; if EOF, then write our buffer out.
; JS fread_err ; if read failed, we exit.
; we failed reading the file
fread_err:
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, fread_errm
MOV edx, fread_errl
INT 0x80
JMP exit
fopen_err:
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, fopen_errm
MOV edx, fopen_errl
INT 0x80
JMP exit
run_code:
loop_start:
MOVZX esi, word [curr]
CMP byte [code+esi], '+'
JE case_plus
CMP byte [code+esi], '-'
JE case_minus
CMP byte [code+esi], '>'
JE case_up
CMP byte [code+esi], '<'
JE case_down
CMP byte [code+esi], '.'
JE case_dot
CMP byte [code+esi], ','
JE case_comma
CMP byte [code+esi], '['
JE case_open
CMP byte [code+esi], ']'
JE case_close
JMP chk_loop ; was not a valid brainfuck char
case_plus:
MOVZX esi, word [mem_p]
ADD byte [mem+esi], 0x01
JMP chk_loop
case_minus:
MOVZX esi, word [mem_p]
SUB byte [mem+esi], 0x01
JMP chk_loop
case_up:
ADD word [mem_p], 0x01
JMP chk_loop
case_down:
SUB word [mem_p], 0x01
JMP chk_loop
case_dot:
MOV eax, 0x04
MOV ebx, 0x01
MOVZX esi, word [mem_p]
ADD esi, mem
MOV ecx, esi
MOV edx, 0x01
INT 0x80
JMP chk_loop
case_comma:
MOV eax, 0x03
MOV ebx, 0x01
MOVZX esi, word [mem_p]
ADD esi, mem
MOV ecx, esi
MOV edx, 0x01
INT 0x80
; flush
PUSH ecx
MOV eax, 0x03
MOV ebx, 0x01
MOV ecx, esp
MOV edx, 0x01
INT 0x80
POP ecx
case_open:
MOVZX esi, word [mem_p]
ADD esi, mem
CMP word [esi], 0x00
JE skip_loop
JMP chk_loop
skip_loop:
PUSH 0x00 ; loop depth
XOR ebp, ebp
MOV bp, word [curr]
skiploop_it:
ADD ebp, 0x01
MOV esi, ebp
ADD esi, code
CMP byte [esi], '['
JE skiploop_inc_d
CMP byte [esi], ']'
JE skiploop_dec_d
skiploop_inc_d:
ADD word [esp], 0x01
JMP skiploop_chk
skiploop_dec_d:
SUB word [esp], 0x01
CMP word [esp], 0x00
JLE skiploop_done
JMP skiploop_chk
skiploop_chk:
CMP ebp, 4096
JL skiploop_it
skiploop_done:
MOV eax, ebp
SUB ax, word [curr]
ADD word [curr], ax
ADD word [curr], 0x01
JMP chk_loop
case_close:
XOR esi, esi
MOVZX esi, word [mem_p]
ADD esi, mem
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, esi
MOV edx, 0x01
INT 0x80
SUB esi, mem
CMP byte [mem+esi], 0x00
JE chk_loop
XOR ebp, ebp
MOV bp, word [curr]
revert_loop:
SUB ebp, 0x01
MOV esi, ebp
ADD esi, code
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, esi
MOV edx, 0x01
;INT 0x80
PUSH 0x0a
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, esp
MOV edx, 0x01
INT 0x80
POP ecx
CMP byte [esi], '['
JE revert_done
revertloop_chk:
CMP ebp, 0x01
JG revert_loop
JMP chk_loop
revert_done:
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, dbg_msg
MOV edx, dbg_msg_l
;INT 0x80
MOV eax, [curr]
SUB eax, ebp
MOV word [curr], ax
chk_loop:
ADD word [curr], 0x01
CMP word [curr], 4096
JNE loop_start
exit:
MOV eax, 0x04
MOV ebx, 0x01
PUSH 0x0a
MOV ecx, esp
MOV edx, 0x01
INT 0x80
POP ecx
MOV eax, 0x01
MOV ebx, 0x00
INT 0x80
My CTRL-V:
SECTION .data
fopen_errm: db "Can't open", 0x0a
fopen_errl: equ $-fopen_errm
fread_errm: db "Can't read", 0x0a
fread_errl: equ $-fread_errm
dbg_msg: db "dbg msg", 0x0a
dbg_msg_l equ $-dbg_msg
SECTION .bss
code: resb 4096 ; reserve 4K for code
mem: resb 4096 ; reserve 4K for memory
mem_p: resw 1
curr: resw 1 ; reserve 16 bits for current index
SECTION .text
global _start
_start:
; initialise stuff
MOV word [curr], 0x00 ; start at 0
MOV word [mem_p], 0x00
; read file
open:
; open it
POP ebx ; argc
POP ebx ; arg #1 (executable name)
POP ebx ; arg #2 (filename)
MOV eax, 0x05
XOR ecx, ecx ; O_RDONLY = 0
XOR edx, edx ; mode is ignored when O_CREAT is not specified
INT 0x80
TEST eax, eax
JNS read
JMP fopen_err
; read into buffer
read:
MOV ebx, eax
MOV eax, 0x03
MOV ecx, code
MOV edx, 4096
INT 0x80
TEST eax, eax ; check for errors / EOF
JMP run_code ; if EOF, then write our buffer out.
; JS fread_err ; if read failed, we exit.
; we failed reading the file
fread_err:
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, fread_errm
MOV edx, fread_errl
INT 0x80
JMP exit
fopen_err:
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, fopen_errm
MOV edx, fopen_errl
INT 0x80
JMP exit
run_code:
loop_start:
MOVZX esi, word [curr]
CMP byte [code+esi], '+'
JE case_plus
CMP byte [code+esi], '-'
JE case_minus
CMP byte [code+esi], '>'
JE case_up
CMP byte [code+esi], '<'
JE case_down
CMP byte [code+esi], '.'
JE case_dot
CMP byte [code+esi], ','
JE case_comma
CMP byte [code+esi], '['
JE case_open
CMP byte [code+esi], ']'
JE case_close
JMP chk_loop ; was not a valid brainfuck char
case_plus:
MOVZX esi, word [mem_p]
ADD byte [mem+esi], 0x01
JMP chk_loop
case_minus:
MOVZX esi, word [mem_p]
SUB byte [mem+esi], 0x01
JMP chk_loop
case_up:
ADD word [mem_p], 0x01
JMP chk_loop
case_down:
SUB word [mem_p], 0x01
JMP chk_loop
case_dot:
MOV eax, 0x04
MOV ebx, 0x01
MOVZX esi, word [mem_p]
ADD esi, mem
MOV ecx, esi
MOV edx, 0x01
INT 0x80
JMP chk_loop
case_comma:
MOV eax, 0x03
MOV ebx, 0x01
MOVZX esi, word [mem_p]
ADD esi, mem
MOV ecx, esi
MOV edx, 0x01
INT 0x80
; flush
PUSH ecx
MOV eax, 0x03
MOV ebx, 0x01
MOV ecx, esp
MOV edx, 0x01
INT 0x80
POP ecx
case_open:
MOVZX esi, word [mem_p]
ADD esi, mem
CMP word [esi], 0x00
JE skip_loop
JMP chk_loop
skip_loop:
PUSH 0x00 ; loop depth
XOR ebp, ebp
MOV bp, word [curr]
skiploop_it:
ADD ebp, 0x01
MOV esi, ebp
ADD esi, code
CMP byte [esi], '['
JE skiploop_inc_d
CMP byte [esi], ']'
JE skiploop_dec_d
skiploop_inc_d:
ADD word [esp], 0x01
JMP skiploop_chk
skiploop_dec_d:
SUB word [esp], 0x01
CMP word [esp], 0x00
JLE skiploop_done
JMP skiploop_chk
skiploop_chk:
CMP ebp, 4096
JL skiploop_it
skiploop_done:
MOV eax, ebp
SUB ax, word [curr]
ADD word [curr], ax
ADD word [curr], 0x01
JMP chk_loop
case_close:
XOR esi, esi
MOVZX esi, word [mem_p]
ADD esi, mem
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, esi
MOV edx, 0x01
INT 0x80
SUB esi, mem
CMP byte [mem+esi], 0x00
JE chk_loop
XOR ebp, ebp
MOV bp, word [curr]
revert_loop:
SUB ebp, 0x01
MOV esi, ebp
ADD esi, code
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, esi
MOV edx, 0x01
;INT 0x80
PUSH 0x0a
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, esp
MOV edx, 0x01
INT 0x80
POP ecx
CMP byte [esi], '['
JE revert_done
revertloop_chk:
CMP ebp, 0x01
JG revert_loop
JMP chk_loop
revert_done:
MOV eax, 0x04
MOV ebx, 0x01
MOV ecx, dbg_msg
MOV edx, dbg_msg_l
;INT 0x80
MOV eax, [curr]
SUB eax, ebp
MOV word [curr], ax
chk_loop:
ADD word [curr], 0x01
CMP word [curr], 4096
JNE loop_start
exit:
MOV eax, 0x04
MOV ebx, 0x01
PUSH 0x0a
MOV ecx, esp
MOV edx, 0x01
INT 0x80
POP ecx
MOV eax, 0x01
MOV ebx, 0x00
INT 0x80