Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1.  
  2. .equ ENDL2, 10
  3. .equ END, 0
  4. .equ PLUS, 43
  5. .equ MINUS, 45
  6. .equ BG, 62
  7. .equ LS, 60
  8. .equ COMA, 44
  9. .equ POINT, 46
  10. .equ PSTART, 91
  11. .equ PFINAL, 93
  12. .global brainfuck
  13.  
  14. .bss
  15. tape: .skip 30000
  16. .text
  17. format_str: .asciz "We should be executing the following code:\n%s"
  18. char: .asciz "%c "
  19. nr: .asciz "%ld "
  20. ENDL: .asciz "\n"
  21. fail: .asciz "***"
  22. ok: .asciz "ok-"
  23.  
  24. #format_str: .asciz "We should be executing the following code:\n%s"
  25.  
  26. # Your brainfuck subroutine will receive one argument:
  27. # a zero termianted string containing the code to execute.
  28. brainfuck:
  29. pushq %rbp
  30. movq %rsp, %rbp
  31.  
  32. movq %rdi, %r15 # copy of brainfuck text
  33.  
  34. movq %rdi, %rsi
  35. movq $format_str, %rdi
  36. movq $0, %rax
  37. call printf
  38.  
  39. movq $0, %r12
  40. loopInitTape:
  41. movq $0, tape(%r12)
  42. incq %r12
  43. #movq $0, %r13
  44. cmpq $3000, %r12
  45. jne loopInitTape
  46.  
  47. movq $0, %r12
  48. movb $65, %dl
  49.  
  50. movq $0, %r12 # index of the tape
  51. movb $65, %dl
  52.  
  53. movb %dl, tape(%r12)
  54.  
  55. movq tape(%r12), %rsi
  56. movq $char, %rdi
  57. movq $0, %rax
  58. call printf
  59.  
  60. movb tape(%r12), %dl
  61. incb %dl
  62. movb %dl,tape(%r12)
  63.  
  64. movq tape(%r12), %rsi
  65. movq $char, %rdi
  66. movq $0, %rax
  67. call printf
  68.  
  69. movq $0, %r12 # index of the tape
  70. loop1:
  71.  
  72. #################################PRINT TEST############################
  73. #movq (%r15), %rsi
  74. #movq $char, %rdi
  75. #movq $0, %rax
  76. #call printf
  77. #################################PRINT TEST############################
  78.  
  79. movb (%r15), %al
  80. movb $PLUS, %dl
  81. cmpb %al, %dl
  82. je ifplus
  83.  
  84. movb (%r15), %al
  85. movb $MINUS, %dl
  86. cmpb %al, %dl
  87. je ifminus
  88.  
  89. movb (%r15), %al
  90. movb $BG, %dl
  91. cmpb %al, %dl
  92. je ifbg
  93.  
  94. movb (%r15), %al
  95. movb $LS, %dl
  96. cmpb %al, %dl
  97. je ifls
  98.  
  99. movb (%r15), %al
  100. movb $POINT, %dl
  101. cmpb %al, %dl
  102. je ifpoint
  103.  
  104.  
  105. ifplus:
  106. movb tape(%r12), %dl
  107. incb %dl
  108. movb %dl,tape(%r12)
  109. jp endifs
  110.  
  111. ifminus:
  112. #decq tape(%r12)
  113. movb tape(%r12), %dl
  114. decb %dl
  115. movb %dl, tape(%r12)
  116. jp endifs
  117. ifbg:
  118. addq $1, %r12
  119. jp endifs
  120. ifls:
  121. decq %r12
  122. jp endifs
  123. ifpoint:
  124. movq tape(%r12), %rsi
  125. movq $char, %rdi
  126. movq $0, %rax
  127. call printf
  128.  
  129. endifs:
  130. incq %r15
  131. movb (%r15), %al
  132. movb $END, %dl
  133. cmpb %al, %dl
  134. jne loop1
  135.  
  136.  
  137. movq %rbp, %rsp
  138. popq %rbp
  139. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement