stage1_assembler-1.hex0 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 0
  17. E0002D2B0200 # LOADUI R11 0x200 ; Where we are putting our address pointers
  18. 0D00003C # TRUE R12 ; Our toggle
  19. 0D00002D # FALSE R13 ; Our PC counter
  20. E0002D2E0050 # LOADUI R14 $getLables_2 ; our first iterator
  21. # ;; We will be using R15 for holding our processed nybbles
  22. # ;; Prep TAPE_01
  23. E0002D201100 # LOADUI R0 0x1100
  24. 42100000 # FOPEN_READ
  25. # ;; Prep TAPE_02
  26. E0002D201101 # LOADUI R0 0x1101
  27. 42100001 # FOPEN_WRITE
  28. E0002D211100 # LOADUI R1 0x1100 ; Read from tape_01
  29. # ;; Function for collecting the address of all labels
  30. # :getLables 2e
  31. 42100100 # FGETC ; Read a Char
  32. # ;; Check for EOF
  33. E0002CC00052 # JUMP.NP R0 @stage2
  34. # ;; Check for Label
  35. E000A030003a # CMPSKIPI.NE R0 58 ; If the Char is : the next char is the label
  36. 3C000036 # JUMP @storeLabel
  37. # ;; Check for pointer to label
  38. E000A0300040 # CMPSKIPI.NE R0 64 ; If the Char is @ the next char is the pointer to a label
  39. 3C00001e # JUMP @ignorePointer
  40. # ;; Otherwise attempt to process
  41. 3C000100 # JUMP @hex ; Convert it
  42. # :getLables_2 50
  43. E0002CC0ffd8 # JUMP.NP R0 @getLables ; Don't record, nonhex values
  44. 090006CC # NOT R12 R12 ; Flip the toggle
  45. E0002C9Cffce # JUMP.Z R12 @getLables ; First half doesn't need anything
  46. # ;; Deal with case of second half of byte
  47. E1000FDD0001 # ADDUI R13 R13 1 ; increment PC now that we have a full byte
  48. 3C00ffc4 # JUMP @getLables
  49. # :ignorePointer 6a
  50. # ;; Ignore the pointer for now
  51. 42100100 # FGETC ; Read a Char
  52. E1000FDD0002 # ADDUI R13 R13 2 ; The pointer will end up taking 2 bytes
  53. 3C00ffb6 # JUMP @getLables
  54. # ;; Function for storing the address of the label
  55. # :storeLabel 78
  56. # ;; Get the char of the Label
  57. 42100100 # FGETC ; Read a Char
  58. # ;; We require 2 bytes to store the pointer values
  59. E0002D500001 # SL0I R0 1 ; Thus we multiply our label by 2
  60. # ;; Store the current Program counter
  61. 0504ADB0 # STOREX16 R13 R11 R0
  62. # ;; Label is safely stored, return
  63. 3C00ffa4 # JUMP @getLables
  64. # ;; Now that we have all of the label addresses,
  65. # ;; We can process input to produce our output
  66. # :stage2 8a
  67. # ;; We first need to rewind tape_01 to perform our second pass
  68. E0002D201100 # LOADUI R0 0x1100
  69. 42100003 # REWIND
  70. # ;; Reset our toggle and counter
  71. E0002D291101 # LOADUI R9 0x1101 ; Where to write the combined byte
  72. 0D00003C # TRUE R12 ; Our toggle
  73. 0D00002D # FALSE R13 ; Our PC counter
  74. E0002D2E00ca # LOADUI R14 $loop_hex ; The hex return target
  75. # :loop a8
  76. 42100100 # FGETC ; Read a Char
  77. # ;; Check for EOF
  78. E0002CC0010e # JUMP.NP R0 @finish
  79. # ;; Check for Label
  80. E000A030003a # CMPSKIPI.NE R0 58 ; Make sure we jump over the label
  81. 3C00004e # JUMP @ignoreLabel
  82. # ;; Check for Pointer
  83. E000A0300040 # CMPSKIPI.NE R0 64 ; If it is a pointer Deal with it
  84. 3C00004c # JUMP @storePointer
  85. # ;; Process Char
  86. 3C000086 # JUMP @hex ; Convert it
  87. # :loop_hex ca
  88. E0002CC0ffd8 # JUMP.NP R0 @loop ; Don't use nonhex chars
  89. 090006CC # NOT R12 R12 ; Flip the toggle
  90. E0002CAC000a # JUMP.NZ R12 @loop_second_nybble ; Jump if toggled
  91. # ;; Process first byte of pair
  92. E100B0F0000f # ANDI R15 R0 0xF ; Store First nibble
  93. 3C00ffc4 # JUMP @loop
  94. # :loop_second_nybble e4
  95. E0002D5F0004 # SL0I R15 4 ; Shift our first nibble
  96. E100B000000f # ANDI R0 R0 0xF ; Mask out top
  97. 0500000F # ADD R0 R0 R15 ; Combine nibbles
  98. 09000319 # SWAP R1 R9 ; Set to write to tape_2
  99. 42100200 # FPUTC ; To TAPE_02
  100. 09000319 # SWAP R1 R9 ; Restore from tape_1
  101. E1000FDD0001 # ADDUI R13 R13 1 ; increment PC now that we have a full byte
  102. 3C00ff9e # JUMP @loop ; Try to get more bytes
  103. # :ignoreLabel 10a
  104. # ;; Consume next char
  105. 42100100 # FGETC ; Read a Char
  106. 3C00ff96 # JUMP @loop
  107. # :storePointer 112
  108. # ;; Correct the PC to reflect the size of the pointer
  109. E1000FDD0002 # ADDUI R13 R13 2 ; Exactly 2 bytes
  110. # ;; Get the char of the Label
  111. 42100100 # FGETC ; Read a Char
  112. # ;; Since we stored a short pointer taking up 2 bytes
  113. E0002D500001 # SL0I R0 1 ; Thus we multiply our label by 2 to get where it is stored
  114. 0503C3B0 # LOADXU16 R3 R11 R0 ; Load the address of the label
  115. # ;; We now have to calculate the distance and store the 2 bytes
  116. 0500233D # SUB R3 R3 R13 ; First determine the difference between the current PC and the stored PC of the label
  117. # ;; Store Upper byte
  118. E100B003ff00 # ANDI R0 R3 0xFF00 ; Mask out everything but top byte
  119. E0002D400008 # SARI R0 8 ; Drop the bottom 8 bits
  120. 09000319 # SWAP R1 R9 ; Write the byte
  121. 42100200 # FPUTC ; To TAPE_02
  122. # ;; Store Lower byte
  123. E100B00300ff # ANDI R0 R3 0xFF ; Preserve bottom half for later
  124. 42100200 # FPUTC ; Write the byte to TAPE_02
  125. 09000319 # SWAP R1 R9 ; Restore Read
  126. 3C00ff58 # JUMP @loop
  127. # ;; Hex function
  128. # ;; Returns hex value of ascii char
  129. # ;; Or -1 if not a hex char
  130. # :hex 150
  131. # ;; Deal with line comments starting with #
  132. E000A0300023 # CMPSKIPI.NE R0 35
  133. 3C000050 # JUMP @ascii_comment
  134. # ;; Deal with line comments starting with ;
  135. E000A030003b # CMPSKIPI.NE R0 59
  136. 3C000046 # JUMP @ascii_comment
  137. # ;; Deal with all ascii less than '0'
  138. E000A0100030 # CMPSKIPI.GE R0 48
  139. 3C00004a # JUMP @ascii_other
  140. # ;; Deal with '0'-'9'
  141. E000A0000039 # CMPSKIPI.G R0 57
  142. 3C00001e # JUMP @ascii_num
  143. # ;; Deal with all ascii less than 'A'
  144. E000A0100041 # CMPSKIPI.GE R0 65
  145. 3C000036 # JUMP @ascii_other
  146. # ;; Unset high bit to set everything into uppercase
  147. E100B00000df # ANDI R0 R0 0xDF
  148. # ;; Deal with 'A'-'F'
  149. E000A0000046 # CMPSKIPI.G R0 70
  150. 3C00000e # JUMP @ascii_high
  151. # ;; Ignore the rest
  152. 3C000022 # JUMP @ascii_other
  153. # :ascii_num 196
  154. E10011000030 # SUBUI R0 R0 48
  155. 0D01000E # JSR_COROUTINE R14
  156. # :ascii_high 1a0
  157. E10011000037 # SUBUI R0 R0 55
  158. 0D01000E # JSR_COROUTINE R14
  159. # :ascii_comment 1aa
  160. 42100100 # FGETC ; Read another char
  161. E000A020000a # CMPSKIPI.E R0 10 ; Stop at the end of line
  162. 3C00fff2 # JUMP @ascii_comment ; Otherwise keep looping
  163. # :ascii_other 1b8
  164. 0D000030 # TRUE R0
  165. 0D01000E # JSR_COROUTINE R14
  166. # :finish 1c0
  167. E0002D201100 # LOADUI R0 0x1100 ; Close TAPE_01
  168. 42100002 # FCLOSE
  169. E0002D201101 # LOADUI R0 0x1101 ; Close TAPE_02
  170. 42100002 # FCLOSE
  171. FFFFFFFF # HALT
  172. # ;; Where all of our pointers will be stored for our locations
  173. # :table 1d8