/Samples/bfasm.asm

https://bitbucket.org/joanbrugueram/8085emujs · Assembly · 181 lines · 119 code · 38 blank · 24 comment · 0 complexity · 6253089c2de229eb04b1069c8819ae30 MD5 · raw file

  1. ; Brainfuck JIT compiler (not an optimizing one)
  2. ; Sample program requires text view
  3. .define
  4. ; Memory layout
  5. jit_dst 0000h
  6. bf_mem 1000h
  7. text_out E000h
  8. program_text F000h
  9. assembler_code F800h
  10. .data program_text
  11. programa:
  12. ;db 2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh
  13. ;db 2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh
  14. ;db 2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh
  15. ;db 2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2eh
  16. db 22h,3eh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,5bh,3ch,2bh,2bh,2bh,2bh,2bh
  17. db 2bh,2bh,2bh,3eh,2dh,5dh,3ch,2eh,3eh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,5bh,3ch
  18. db 2bh,2bh,2bh,2bh,3eh,2dh,5dh,3ch,2bh,2eh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2eh
  19. db 2eh,2bh,2bh,2bh,2eh,5bh,2dh,5dh,3eh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,5bh
  20. db 3ch,2bh,2bh,2bh,2bh,3eh,2dh,5dh,20h,3ch,2eh,3eh,2bh,2bh,2bh,2bh,2bh,2bh
  21. db 2bh,2bh,2bh,2bh,2bh,5bh,3ch,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,3eh,2dh,5dh
  22. db 3ch,2dh,2eh,2dh,2dh,2dh,2dh,2dh,2dh,2dh,2dh,2eh,2bh,2bh,2bh,2eh,2dh,2dh
  23. db 2dh,2dh,2dh,2dh,2eh,2dh,2dh,2dh,2dh,2dh,2dh,2dh,2dh,2eh,5bh,2dh,5dh,3eh
  24. db 2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,5bh,3ch,2bh,2bh,2bh,2bh,3eh,2dh,20h,5dh
  25. db 3ch,2bh,2eh,5bh,2dh,5dh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2bh,2eh
  26. db 0h
  27. .org assembler_code
  28. main:
  29. ; Assemble program
  30. lxi D, programa
  31. lxi H, jit_dst
  32. call bfasm
  33. ; Execute program
  34. lxi D, text_out
  35. lxi H, bf_mem
  36. jmp jit_dst
  37. ; BRAINFUCK ASSEMBLER FUNCTION
  38. ; Inputs:
  39. ; - DE: [Input] Pointer into Brainfuck source code (terminated by zero)
  40. ; - HL: [Output] Pointer into 8085 program code
  41. ;
  42. ; The assembled program code requires two parameters before being executed:
  43. ; - DE: Pointer to text output memory
  44. ; - HL: Pointer to Brainfuck memory
  45. bfasm:
  46. ; Dispatch assembler handler from brainfuck character
  47. ldax D
  48. cpi '>'
  49. jz bfasm_greater
  50. cpi '<'
  51. jz bfasm_lesser
  52. cpi '+'
  53. jz bfasm_sum
  54. cpi '-'
  55. jz bfasm_minus
  56. cpi 2Eh ; Dot
  57. jz bfasm_dot
  58. cpi 2Ch ; Comma
  59. jz bfasm_comma
  60. cpi '['
  61. jz bfasm_openbracket
  62. cpi ']'
  63. jz bfasm_closebracket
  64. cpi 00h ; End of input
  65. jz bfasm_end
  66. jmp bfasm_other
  67. bfasm_greater:
  68. inx D ; Skip '>' character
  69. mvi M, 23h ; inx H (increase memory pointer)
  70. inx H
  71. jmp bfasm_next
  72. bfasm_lesser:
  73. inx D ; Skip '<' character
  74. mvi M, 2Bh ; dcx H (decrease memory pointer)
  75. inx H
  76. jmp bfasm_next
  77. bfasm_sum:
  78. inx D ; Skip '+' character
  79. mvi M, 34h ; inr M (increase memory content)
  80. inx H
  81. jmp bfasm_next
  82. bfasm_minus:
  83. inx D ; Skip '-' character
  84. mvi M, 35h ; dcr M (decrease memory content)
  85. inx H
  86. jmp bfasm_next
  87. bfasm_dot:
  88. inx D ; Skip '.' character
  89. mvi M, 7Eh ; mov A, M (load character from memory)
  90. inx H
  91. mvi M, 12h ; stax D (put character to text output)
  92. inx H
  93. mvi M, 13h ; inx D (increase text output)
  94. inx H
  95. jmp bfasm_next
  96. bfasm_comma:
  97. hlt ; TODO
  98. bfasm_openbracket:
  99. inx D ; Skip '[' character
  100. ; Save JIT address of start at the top of stack
  101. push H
  102. ; Reserve space for loop start
  103. inx H
  104. inx H
  105. inx H
  106. inx H
  107. ; Assemble subprogram
  108. call bfasm
  109. ; Restore the pointer before the jump to BC
  110. pop B
  111. ; Assemble jump end
  112. mvi M, C3h
  113. inx H
  114. mov M, C
  115. inx H
  116. mov M, B
  117. inx H
  118. ; Assemble jump start
  119. mvi A, 7Eh
  120. stax B
  121. inx B
  122. mvi A, CAh
  123. stax B
  124. inx B
  125. mov A, L
  126. stax B
  127. inx B
  128. mov A, H
  129. stax B
  130. inx B
  131. jmp bfasm_next
  132. bfasm_closebracket:
  133. inx D ; Skip the ']' character
  134. ret
  135. bfasm_end:
  136. mvi M, 76h ; hlt (stop program)
  137. inx H
  138. ret
  139. bfasm_other:
  140. inx D ; Skip the character
  141. jmp bfasm_next
  142. bfasm_next:
  143. jmp bfasm