Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .equ ENDL2, 10
- .equ END, 0
- .equ PLUS, 43
- .equ MINUS, 45
- .equ BG, 62
- .equ LS, 60
- .equ COMA, 44
- .equ POINT, 46
- .equ PSTART, 91
- .equ PFINAL, 93
- .global brainfuck
- .bss
- tape: .skip 30000
- .text
- format_str: .asciz "We should be executing the following code:\n%s"
- char: .asciz "%c "
- nr: .asciz "%ld "
- ENDL: .asciz "\n"
- fail: .asciz "***"
- ok: .asciz "ok-"
- #format_str: .asciz "We should be executing the following code:\n%s"
- # Your brainfuck subroutine will receive one argument:
- # a zero termianted string containing the code to execute.
- brainfuck:
- pushq %rbp
- movq %rsp, %rbp
- movq %rdi, %r15 # copy of brainfuck text
- movq %rdi, %rsi
- movq $format_str, %rdi
- movq $0, %rax
- call printf
- movq $0, %r12
- loopInitTape:
- movq $0, tape(%r12)
- incq %r12
- #movq $0, %r13
- cmpq $3000, %r12
- jne loopInitTape
- movq $0, %r12
- movb $65, %dl
- movq $0, %r12 # index of the tape
- movb $65, %dl
- movb %dl, tape(%r12)
- movq tape(%r12), %rsi
- movq $char, %rdi
- movq $0, %rax
- call printf
- movb tape(%r12), %dl
- incb %dl
- movb %dl,tape(%r12)
- movq tape(%r12), %rsi
- movq $char, %rdi
- movq $0, %rax
- call printf
- movq $0, %r12 # index of the tape
- loop1:
- #################################PRINT TEST############################
- #movq (%r15), %rsi
- #movq $char, %rdi
- #movq $0, %rax
- #call printf
- #################################PRINT TEST############################
- movb (%r15), %al
- movb $PLUS, %dl
- cmpb %al, %dl
- je ifplus
- movb (%r15), %al
- movb $MINUS, %dl
- cmpb %al, %dl
- je ifminus
- movb (%r15), %al
- movb $BG, %dl
- cmpb %al, %dl
- je ifbg
- movb (%r15), %al
- movb $LS, %dl
- cmpb %al, %dl
- je ifls
- movb (%r15), %al
- movb $POINT, %dl
- cmpb %al, %dl
- je ifpoint
- ifplus:
- movb tape(%r12), %dl
- incb %dl
- movb %dl,tape(%r12)
- jp endifs
- ifminus:
- #decq tape(%r12)
- movb tape(%r12), %dl
- decb %dl
- movb %dl, tape(%r12)
- jp endifs
- ifbg:
- addq $1, %r12
- jp endifs
- ifls:
- decq %r12
- jp endifs
- ifpoint:
- movq tape(%r12), %rsi
- movq $char, %rdi
- movq $0, %rax
- call printf
- endifs:
- incq %r15
- movb (%r15), %al
- movb $END, %dl
- cmpb %al, %dl
- jne loop1
- movq %rbp, %rsp
- popq %rbp
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement