Guest User

bf.asm

a guest
Feb 23rd, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. ASCII EQUS " \t\n \r !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz\{|}~"
  2.  
  3. bf: MACRO
  4. ; Run a brainfuck program.
  5. ; bf source[, input]
  6.  
  7. ; point to the first program char
  8. src_i = 1
  9. src_len = STRLEN("\1")
  10.  
  11. ; point to the first input char
  12. in_p = 1
  13. in_len = 0
  14. if _NARG > 1
  15. in_len = STRLEN("\2")
  16. endc
  17.  
  18. ; point to the first cell
  19. mem_p = 0
  20. mem_len = 32768
  21. cell_size = 256
  22. ; initialize 32768 memory cells to 0s
  23. for i, mem_len
  24. mem{d:i} = 0
  25. endr
  26.  
  27. ; run the program
  28. rept $7fffffff
  29. if src_i > src_len
  30. break
  31. endc
  32.  
  33. ; get the current source code character
  34. x = STRSUB("\1", src_i, 1)
  35.  
  36. ; increment the current cell
  37. if x == "+"
  38. mem{d:mem_p} = (mem{d:mem_p} + 1) % cell_size
  39.  
  40. ; decrement the current cell
  41. elif x == "-"
  42. mem{d:mem_p} = (mem{d:mem_p} - 1) % cell_size
  43.  
  44. ; move to the next cell
  45. elif x == ">"
  46. mem_p = (mem_p + 1) % mem_len
  47.  
  48. ; move to the previous cell
  49. elif x == "<"
  50. mem_p = (mem_p - 1) % mem_len
  51.  
  52. ; output the current cell
  53. elif x == "."
  54. print STRSUB("{ASCII}", mem{d:mem_p} + 1, 1)
  55.  
  56. ; input the current cell
  57. elif x == ","
  58. if in_p <= in_len
  59. mem{d:mem_p} = STRSUB("\2", in_p, 1)
  60. in_p = in_p + 1
  61. else
  62. mem{d:mem_p} = cell_size - 1 ; EOF
  63. endc
  64.  
  65. ; begin while loop
  66. elif x == "["
  67. if mem{d:mem_p} == 0
  68. if !DEF(pair{d:src_i})
  69. ; find the corresponding ']' after the '['
  70. depth = 1
  71. for i, src_i + 1, src_len + 1
  72. y = STRSUB("\1", i, 1)
  73. depth = depth + (y == "[") - (y == "]")
  74. if depth == 0
  75. break
  76. endc
  77. endr
  78. if i == src_len
  79. fail "[ without ]"
  80. endc
  81. pair{d:src_i} = i
  82. endc
  83. src_i = pair{d:src_i}
  84. endc
  85.  
  86. ; end while loop
  87. elif x == "]"
  88. if mem{d:mem_p} != 0
  89. if !DEF(pair{d:src_i})
  90. ; find the corresponding '[' before the ']'
  91. depth = 1
  92. for i, src_i - 1, 0, -1
  93. y = STRSUB("\1", i, 1)
  94. depth = depth - (y == "[") + (y == "]")
  95. if depth == 0
  96. break
  97. endc
  98. endr
  99. if i == 0
  100. fail "] without ["
  101. endc
  102. pair{d:src_i} = i
  103. endc
  104. src_i = pair{d:src_i}
  105. endc
  106.  
  107. endc
  108.  
  109. ; move to the next source code character
  110. src_i = src_i + 1
  111.  
  112. endr
  113.  
  114. ENDM
  115.  
  116. ; hello world
  117. bf ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
  118. ; echo
  119. bf \,+[-.\,+], HELLO WORLD!\n
  120.  
Advertisement
Add Comment
Please, Sign In to add comment