stage1_assembler-1.s 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ; Copyright (C) 2016 Jeremiah Orians
  2. ; This file is part of stage0.
  3. ;
  4. ; stage0 is free software: you can redistribute it and/or modify
  5. ; it under the terms of the GNU General Public License as published by
  6. ; the Free Software Foundation, either version 3 of the License, or
  7. ; (at your option) any later version.
  8. ;
  9. ; stage0 is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. :start
  17. LOADUI R11 0x200 ; Where we are putting our address pointers
  18. TRUE R12 ; Our toggle
  19. FALSE R13 ; Our PC counter
  20. LOADUI R14 $getLables_2 ; our first iterator
  21. ;; We will be using R15 for holding our processed nybbles
  22. ;; Prep TAPE_01
  23. LOADUI R0 0x1100
  24. FOPEN_READ
  25. ;; Prep TAPE_02
  26. LOADUI R0 0x1101
  27. FOPEN_WRITE
  28. LOADUI R1 0x1100 ; Read from tape_01
  29. ;; Function for collecting the address of all labels
  30. :getLables
  31. FGETC ; Read a Char
  32. ;; Check for EOF
  33. JUMP.NP R0 @stage2
  34. ;; Check for Label
  35. CMPSKIPI.NE R0 58 ; If the Char is : the next char is the label
  36. JUMP @storeLabel
  37. ;; Check for pointer to label
  38. CMPSKIPI.NE R0 64 ; If the Char is @ the next char is the pointer to a label
  39. JUMP @ignorePointer
  40. ;; Otherwise attempt to process
  41. JUMP @hex ; Convert it
  42. :getLables_2
  43. JUMP.NP R0 @getLables ; Don't record, nonhex values
  44. NOT R12 R12 ; Flip the toggle
  45. JUMP.Z R12 @getLables ; First half doesn't need anything
  46. ;; Deal with case of second half of byte
  47. ADDUI R13 R13 1 ; increment PC now that we have a full byte
  48. JUMP @getLables
  49. :ignorePointer
  50. ;; Ignore the pointer for now
  51. FGETC ; Read a Char
  52. ADDUI R13 R13 2 ; The pointer will end up taking 2 bytes
  53. JUMP @getLables
  54. ;; Function for storing the address of the label
  55. :storeLabel
  56. ;; Get the char of the Label
  57. FGETC ; Read a Char
  58. ;; We require 2 bytes to store the pointer values
  59. SL0I R0 1 ; Thus we multiply our label by 2
  60. ;; Store the current Program counter
  61. STOREX16 R13 R11 R0
  62. ;; Label is safely stored, return
  63. JUMP @getLables
  64. ;; Now that we have all of the label addresses,
  65. ;; We can process input to produce our output
  66. :stage2
  67. ;; We first need to rewind tape_01 to perform our second pass
  68. LOADUI R0 0x1100
  69. REWIND
  70. ;; Reset our toggle and counter
  71. LOADUI R9 0x1101 ; Where to write the combined byte
  72. TRUE R12 ; Our toggle
  73. FALSE R13 ; Our PC counter
  74. LOADUI R14 $loop_hex ; The hex return target
  75. :loop
  76. FGETC ; Read a Char
  77. ;; Check for EOF
  78. JUMP.NP R0 @finish
  79. ;; Check for Label
  80. CMPSKIPI.NE R0 58 ; Make sure we jump over the label
  81. JUMP @ignoreLabel
  82. ;; Check for Pointer
  83. CMPSKIPI.NE R0 64 ; If it is a pointer Deal with it
  84. JUMP @storePointer
  85. ;; Process Char
  86. JUMP @hex ; Convert it
  87. :loop_hex
  88. JUMP.NP R0 @loop ; Don't use nonhex chars
  89. NOT R12 R12 ; Flip the toggle
  90. JUMP.NZ R12 @loop_second_nybble ; Jump if toggled
  91. ;; Process first byte of pair
  92. ANDI R15 R0 0xF ; Store First nibble
  93. JUMP @loop
  94. :loop_second_nybble
  95. SL0I R15 4 ; Shift our first nibble
  96. ANDI R0 R0 0xF ; Mask out top
  97. ADD R0 R0 R15 ; Combine nibbles
  98. SWAP R1 R9 ; Set to write to tape_2
  99. FPUTC ; To TAPE_02
  100. SWAP R1 R9 ; Restore from tape_1
  101. ADDUI R13 R13 1 ; increment PC now that we have a full byte
  102. JUMP @loop ; Try to get more bytes
  103. :ignoreLabel
  104. ;; Consume next char
  105. FGETC ; Read a Char
  106. JUMP @loop
  107. :storePointer
  108. ;; Correct the PC to reflect the size of the pointer
  109. ADDUI R13 R13 2 ; Exactly 2 bytes
  110. ;; Get the char of the Label
  111. FGETC ; Read a Char
  112. ;; Since we stored a short pointer taking up 2 bytes
  113. SL0I R0 1 ; Thus we multiply our label by 2 to get where it is stored
  114. LOADXU16 R3 R11 R0 ; Load the address of the label
  115. ;; We now have to calculate the distance and store the 2 bytes
  116. SUB R3 R3 R13 ; First determine the difference between the current PC and the stored PC of the label
  117. ;; Store Upper byte
  118. ANDI R0 R3 0xFF00 ; Mask out everything but top byte
  119. SARI R0 8 ; Drop the bottom 8 bits
  120. SWAP R1 R9 ; Write the byte
  121. FPUTC ; To TAPE_02
  122. ;; Store Lower byte
  123. ANDI R0 R3 0xFF ; Preserve bottom half for later
  124. FPUTC ; Write the byte to TAPE_02
  125. SWAP R1 R9 ; Restore Read
  126. JUMP @loop
  127. ;; Hex function
  128. ;; Returns hex value of ascii char
  129. ;; Or -1 if not a hex char
  130. :hex
  131. ;; Deal with line comments starting with #
  132. CMPSKIPI.NE R0 35
  133. JUMP @ascii_comment
  134. ;; Deal with line comments starting with ;
  135. CMPSKIPI.NE R0 59
  136. JUMP @ascii_comment
  137. ;; Deal with all ascii less than '0'
  138. CMPSKIPI.GE R0 48
  139. JUMP @ascii_other
  140. ;; Deal with '0'-'9'
  141. CMPSKIPI.G R0 57
  142. JUMP @ascii_num
  143. ;; Deal with all ascii less than 'A'
  144. CMPSKIPI.GE R0 65
  145. JUMP @ascii_other
  146. ;; Unset high bit to set everything into uppercase
  147. ANDI R0 R0 0xDF
  148. ;; Deal with 'A'-'F'
  149. CMPSKIPI.G R0 70
  150. JUMP @ascii_high
  151. ;; Ignore the rest
  152. JUMP @ascii_other
  153. :ascii_num
  154. SUBUI R0 R0 48
  155. JSR_COROUTINE R14
  156. :ascii_high
  157. SUBUI R0 R0 55
  158. JSR_COROUTINE R14
  159. :ascii_comment
  160. FGETC ; Read another char
  161. CMPSKIPI.E R0 10 ; Stop at the end of line
  162. JUMP @ascii_comment ; Otherwise keep looping
  163. :ascii_other
  164. TRUE R0
  165. JSR_COROUTINE R14
  166. :finish
  167. LOADUI R0 0x1100 ; Close TAPE_01
  168. FCLOSE
  169. LOADUI R0 0x1101 ; Close TAPE_02
  170. FCLOSE
  171. HALT
  172. ;; Where all of our pointers will be stored for our locations
  173. :table