stage1_assembler-2.hex1 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. ;; Node format:
  17. ;; PREV->pointer (register size)
  18. ;; Address (register size)
  19. ;; NULL terminated string (strln + 1)
  20. # :start 0
  21. :0
  22. # ;; R1 is reserved for reading/writing bytes (don't touch)
  23. # ;; We will be using R8 for our malloc pointer
  24. # ;; We will be using R9 for our header size in bytes
  25. # ;; We will be using R10 for our toggle
  26. # ;; We will be using R11 for our PC counter
  27. # ;; We will be using R12 for holding our nybble
  28. # ;; We will be using R13 for our register size in bytes
  29. # ;; We will be using R14 for our head-node
  30. E0002D2F 0400 # LOADUI R15 $stack ; We will be using R15 for our stack
  31. # ;; Main program functionality
  32. # ;; Reads in Tape_01 and writes out results onto Tape_02
  33. # ;; Accepts no arguments and HALTS when done
  34. # :main 6
  35. :2
  36. # ;; Initialize header info
  37. 0D000010 # READSCID R0 ; Get process capabilities
  38. E100B010000f # ANDI R1 R0 0xF ; We only care about size nybble
  39. E0002D200001 # LOADUI R0 1 ; Assume we are 8bit
  40. 05032D01 # SL0 R13 R0 R1 ; Let size nybble correct answer
  41. 0900049D # COPY R9 R13 ; Prepare Header size
  42. E0002D590001 # SL0I R9 1 ; Double to make proper size
  43. # ;; Prep TAPE_01
  44. E0002D201100 # LOADUI R0 0x1100
  45. 42100000 # FOPEN_READ
  46. # ;; Intialize environment
  47. E0002D211100 # LOADUI R1 0x1100 ; Read from tape_01
  48. 0D00002C # FALSE R12 ; Set holder to zero
  49. 0D00002B # FALSE R11 ; Set PC counter to zero
  50. 0D00002A # FALSE R10 ; Our toggle
  51. E0002D280700 # LOADUI R8 0x700 ; Where we want our heap to start
  52. # ;; Perform first pass
  53. E0002D0F @3 # CALLI R15 @first_pass
  54. # ;; We need to rewind tape_01 to perform our second pass
  55. E0002D201100 # LOADUI R0 0x1100
  56. 42100003 # REWIND
  57. # ;; Reintialize environment
  58. 0D00002C # FALSE R12 ; Set holder to zero
  59. 0D00002B # FALSE R11 ; Set PC counter to zero
  60. 0D00002A # FALSE R10 ; Our toggle
  61. # ;; Prep TAPE_02
  62. E0002D201101 # LOADUI R0 0x1101
  63. 42100001 # FOPEN_WRITE
  64. E0002D0F @4 # CALLI R15 @second_pass
  65. # ;; Close up as we are done
  66. E0002D201100 # LOADUI R0 0x1100 ; Close TAPE_01
  67. 42100002 # FCLOSE
  68. E0002D201101 # LOADUI R0 0x1101 ; Close TAPE_02
  69. 42100002 # FCLOSE
  70. FFFFFFFF # HALT
  71. # ;; First pass function
  72. # ;; Reads Tape_01 and creates our label table
  73. # ;; Will Overwrite R0 R10 R11
  74. # ;; Returns to Main function when done
  75. # :first_pass 8a
  76. :3
  77. 42100100 # FGETC ; Read a Char
  78. # ;; Check for EOF
  79. E000A0100000 # CMPSKIPI.GE R0 0
  80. 0D01001F # RET R15
  81. # ;; Check for and deal with label (:)
  82. E000A030003a # CMPSKIPI.NE R0 58
  83. 3C00 @5 # JUMP @storeLabel
  84. # ;; Check for and deal with pointers to labels
  85. # ;; Starting with (@)
  86. E000A0300040 # CMPSKIPI.NE R0 64
  87. 3C00 @6 # JUMP @ThrowAwayPointer
  88. # ;; Then dealing with ($)
  89. E000A0300024 # CMPSKIPI.NE R0 36
  90. 3C00 @6 # JUMP @ThrowAwayPointer
  91. # ;; Now check for absolute addresses (&)
  92. E000A0300026 # CMPSKIPI.NE R0 38
  93. 3C00 @7 # JUMP @ThrowAwayAddress
  94. # ;; Otherwise attempt to process
  95. E0002D0F @8 # CALLI R15 @hex ; Convert it
  96. E0002CC0 @3 # JUMP.NP R0 @first_pass ; Don't record, nonhex values
  97. # ;; Flip the toggle
  98. 090006AA # NOT R10 R10
  99. E0002C9A @3 # JUMP.Z R10 @first_pass ; Jump if toggled
  100. # ;; Deal with case of second half of byte
  101. E1000FBB0001 # ADDUI R11 R11 1 ; increment PC now that that we have a full byte
  102. 3C00 @3 # JUMP @first_pass
  103. # ;; Second pass function
  104. # ;; Reads from Tape_01 and uses the values in the table
  105. # ;; To write desired contents onto Tape_02
  106. # ;; Will Overwrite R0 R10 R11
  107. # ;; Returns to Main function when done
  108. # :second_pass e0
  109. :4
  110. 42100100 # FGETC ; Read a Char
  111. # ;; Check for EOF
  112. E000A0100000 # CMPSKIPI.GE R0 0
  113. 0D01001F # RET R15
  114. # ;; Check for and deal with label
  115. E000A030003a # CMPSKIPI.NE R0 58
  116. 3C00 @9 # JUMP @ThrowAwayLabel
  117. # ;; Check for and deal with Pointers to labels
  118. E000A0300040 # CMPSKIPI.NE R0 64 ; @ for relative
  119. 3C00 @a # JUMP @StoreRelativePointer
  120. E000A0300024 # CMPSKIPI.NE R0 36 ; $ for absolute
  121. 3C00 @b # JUMP @StoreAbsolutePointer
  122. E000A0300026 # CMPSKIPI.NE R0 38 ; & for address
  123. 3C00 @c # JUMP @StoreAbsoluteAddress
  124. # ;; Process everything else
  125. E0002D0F @8 # CALLI R15 @hex ; Attempt to Convert it
  126. E000A0100000 # CMPSKIPI.GE R0 0 ; Don't record, nonhex values
  127. 3C00 @4 # JUMP @second_pass ; Move onto Next char
  128. # ;; Determine if we got a full byte
  129. 090006AA # NOT R10 R10
  130. E0002C9A @d # JUMP.Z R10 @second_pass_0 ; Jump if toggled
  131. # ;; Deal with case of first half of byte
  132. E100B0C0000f # ANDI R12 R0 0x0F ; Store our first nibble
  133. 3C00 @4 # JUMP @second_pass
  134. # :second_pass_0 13a
  135. :d
  136. # ;; Deal with case of second half of byte
  137. E0002D5C0004 # SL0I R12 4 ; Shift our first nybble
  138. E100B000000f # ANDI R0 R0 0x0F ; Mask out top
  139. 0500000C # ADD R0 R0 R12 ; Combine nybbles
  140. E0002D211101 # LOADUI R1 0x1101 ; Write the combined byte
  141. 42100200 # FPUTC ; To TAPE_02
  142. E0002D211100 # LOADUI R1 0x1100 ; Read from tape_01
  143. E1000FBB0001 # ADDUI R11 R11 1 ; increment PC now that that we have a full byte
  144. 3C00 @4 # JUMP @second_pass
  145. # ;; Store Label function
  146. # ;; Writes out the token and the current PC value
  147. # ;; Its static variable for storing the next index to be used
  148. # ;; Will overwrite R0
  149. # ;; Returns to first pass when done
  150. # :storeLabel 164
  151. :5
  152. 09000408 # COPY R0 R8 ; get current malloc
  153. 05000889 # ADD R8 R8 R9 ; update malloc
  154. # ;; Add node info
  155. 05048B0D # STOREX R11 R0 R13 ; Store the PC of the label
  156. E10020E00000 # STORE R14 R0 0 ; Store the Previous Head
  157. 090005E0 # MOVE R14 R0 ; Update Head
  158. # ;; Store the name of the Label
  159. E0002D0F @e # CALLI R15 @writeout_token
  160. # ;; And be done
  161. 3C00 @3 # JUMP @first_pass
  162. # ;; StoreRelativepointer function
  163. # ;; Deals with the special case of relative pointers
  164. # ;; Stores string
  165. # ;; Finds match in Table
  166. # ;; Writes out the offset
  167. # ;; Modifies R0 R11
  168. # ;; Jumps back into Pass2
  169. # :StoreRelativePointer 184
  170. :a
  171. # ;; Correct the PC to reflect the size of the pointer
  172. E1000FBB0002 # ADDUI R11 R11 2 ; Exactly 2 bytes
  173. E0002D0F @o # CALLI R15 @Match_string ; Find the Match
  174. 0500200B # SUB R0 R0 R11 ; Determine the difference
  175. E0002D0F @p # CALLI R15 @ProcessImmediate ; Write out the value
  176. 3C00 @4 # JUMP @second_pass
  177. # ;; StoreAbsolutepointer function
  178. # ;; Deals with the special case of absolute pointers
  179. # ;; Stores string
  180. # ;; Finds match in Table
  181. # ;; Writes out the absolute address of match
  182. # ;; Modifies R0 R11
  183. # ;; Jumps back into Pass2
  184. # :StoreAbsolutePointer 1a4
  185. :b
  186. # ;; Correct the PC to reflect the size of the pointer
  187. E1000FBB0002 # ADDUI R11 R11 2 ; Exactly 2 bytes
  188. E0002D0F @o # CALLI R15 @Match_string ; Find the Match
  189. E0002D0F @p # CALLI R15 @ProcessImmediate ; Write out the value
  190. 3C00 @4 # JUMP @second_pass
  191. # ;; StoreAbsoluteAddress function
  192. # ;; Deal with the special case of absolute Addresses
  193. # ;; Stores string
  194. # ;; Finds match in Table
  195. # ;; Writes out the full absolute address [32 bit machine]
  196. # ;; Modifies R0 R11
  197. # ;; Jumpbacs back into Pass2
  198. # :StoreAbsoluteAddress 1ba
  199. :c
  200. # ;; Correct the PC to reflect the size of the address
  201. E1000FBB0004 # ADDUI R11 R11 4 ; 4 Bytes on 32bit machines
  202. E0002D0F @o # CALLI R15 @Match_string ; Find the Match
  203. E100B020ffff # ANDI R2 R0 0xFFFF ; Save bottom half for next function
  204. E0002D400010 # SARI R0 16 ; Drop bottom 16 bits
  205. E0002D0F @p # CALLI R15 @ProcessImmediate ; Write out top 2 bytes
  206. 09000502 # MOVE R0 R2 ; Use the saved 16bits
  207. E0002D0F @p # CALLI R15 @ProcessImmediate ; Write out bottom 2 bytes
  208. 3C00 @4 # JUMP @second_pass
  209. # ;; Writeout Token Function
  210. # ;; Writes the Token [minus first char] to the address
  211. # ;; given by malloc and updates malloc pointer
  212. # ;; Returns starting address of string
  213. # :writeout_token 1e6
  214. :e
  215. # ;; Preserve registers
  216. 0902001F # PUSHR R1 R15
  217. 0902002F # PUSHR R2 R15
  218. # ;; Initialize
  219. 09000428 # COPY R2 R8 ; Get current malloc pointer
  220. # ;; Our core loop
  221. # :writeout_token_0 1f2
  222. :r
  223. 42100100 # FGETC ; Get another byte
  224. # ;; Deal with termination cases
  225. E000A0300020 # CMPSKIPI.NE R0 32 ; Finished if space
  226. 3C00 @q # JUMP @writeout_token_done
  227. E000A0300009 # CMPSKIPI.NE R0 9 ; Finished if tab
  228. 3C00 @q # JUMP @writeout_token_done
  229. E000A030000a # CMPSKIPI.NE R0 10 ; Finished if newline
  230. 3C00 @q # JUMP @writeout_token_done
  231. E000A030ffff # CMPSKIPI.NE R0 -1 ; Finished if EOF
  232. 3C00 @q # JUMP @writeout_token_done
  233. # ;; Deal with valid input
  234. E10021080000 # STORE8 R0 R8 0 ; Write out the byte
  235. E1000F880001 # ADDUI R8 R8 1 ; Increment
  236. 3C00 @r # JUMP @writeout_token_0 ; Keep looping
  237. # ;; Clean up now that we are done
  238. # :writeout_token_done 22e
  239. :q
  240. # ;; Fix malloc
  241. E1000F880001 # ADDUI R8 R8 1
  242. # ;; Prepare for return
  243. 09000502 # MOVE R0 R2
  244. # ;; Restore registers
  245. 0902802F # POPR R2 R15
  246. 0902801F # POPR R1 R15
  247. # ;; And be done
  248. 0D01001F # RET R15
  249. # ;; Match string function
  250. # ;; Walks down list until match is found or returns -1
  251. # ;; Reads a token
  252. # ;; Then returns address of match in R0
  253. # ;; Returns to whatever called it
  254. # :Match_string 244
  255. :o
  256. # ;; Preserve registers
  257. 0902001F # PUSHR R1 R15
  258. 0902002F # PUSHR R2 R15
  259. # ;; Initialize for Loop
  260. E0002D0F @e # CALLI R15 @writeout_token ; Get our desired string
  261. 09000510 # MOVE R1 R0 ; Position our desired string
  262. 0900042E # COPY R2 R14 ; Begin at our head node
  263. # ;; Loop until we find a match
  264. # :Match_string_0 25a
  265. :h
  266. 05000029 # ADD R0 R2 R9 ; Where the string is located
  267. E0002D0F @f # CALLI R15 @strcmp
  268. E0002C50 @g # JUMP.E R0 @Match_string_1 ; It is a match!
  269. # ;; Prepare for next loop
  270. E10013220000 # LOAD R2 R2 0 ; Move to next node
  271. E0002CA2 @h # JUMP.NZ R2 @Match_string_0 ; Keep looping
  272. 0D000032 # TRUE R2 ; Set result to -1 if not found
  273. # :Match_string_1 27a
  274. :g
  275. # ;; Store the correct answer
  276. E000A022ffff # CMPSKIPI.E R2 -1 ; Otherwise get the value
  277. 0503802D # LOADX R0 R2 R13 ; Get the value we care about
  278. # ;; Restore registers
  279. 0902802F # POPR R2 R15
  280. 0902801F # POPR R1 R15
  281. 0D01001F # RET R15
  282. # ;; Our simple string compare function
  283. # ;; Receives two pointers in R0 and R1
  284. # ;; Returns the difference between the strings in R0
  285. # ;; Returns to whatever called it
  286. # :strcmp 290
  287. :f
  288. # ;; Preserve registers
  289. 0902001F # PUSHR R1 R15
  290. 0902002F # PUSHR R2 R15
  291. 0902003F # PUSHR R3 R15
  292. 0902004F # PUSHR R4 R15
  293. # ;; Setup registers
  294. 09000520 # MOVE R2 R0
  295. 09000531 # MOVE R3 R1
  296. E0002D240000 # LOADUI R4 0
  297. # :cmpbyte 2ae
  298. :i
  299. 0503A024 # LOADXU8 R0 R2 R4 ; Get a byte of our first string
  300. 0503A134 # LOADXU8 R1 R3 R4 ; Get a byte of our second string
  301. E1000F440001 # ADDUI R4 R4 1 ; Prep for next loop
  302. 05004101 # CMP R1 R0 R1 ; Compare the bytes
  303. E000A0200000 # CMPSKIPI.E R0 0 ; Stop if byte is NULL
  304. E0002C51 @i # JUMP.E R1 @cmpbyte ; Loop if bytes are equal
  305. # ;; Done
  306. 09000501 # MOVE R0 R1 ; Prepare for return
  307. # ;; Restore registers
  308. 0902804F # POPR R4 R15
  309. 0902803F # POPR R3 R15
  310. 0902802F # POPR R2 R15
  311. 0902801F # POPR R1 R15
  312. 0D01001F # RET R15
  313. # ;; Processimmediate Function
  314. # ;; Receives an integer value in R0
  315. # ;; Writes out the values to Tape_02
  316. # ;; Doesn't modify registers
  317. # ;; Returns to whatever called it
  318. # :ProcessImmediate 2e4
  319. :p
  320. # ;; Preserve registers
  321. 0902000F # PUSHR R0 R15
  322. 0902001F # PUSHR R1 R15
  323. 0902002F # PUSHR R2 R15
  324. # ;; Break up Immediate
  325. E100B02000ff # ANDI R2 R0 0xFF ; Put lower byte in R2
  326. E0002D400008 # SARI R0 8 ; Drop Bottom byte from R0
  327. E100B00000ff # ANDI R0 R0 0xFF ; Maskout everything outside of top byte
  328. # ;; Write out Top Byte
  329. E0002D211101 # LOADUI R1 0x1101 ; Write the byte
  330. 42100200 # FPUTC ; To TAPE_02
  331. # ;; Write out bottom Byte
  332. 09000502 # MOVE R0 R2 ; Put Lower byte in R0
  333. 42100200 # FPUTC ; To TAPE_02
  334. # ;; Restore registers
  335. 0902802F # POPR R2 R15
  336. 0902801F # POPR R1 R15
  337. 0902800F # POPR R0 R15
  338. # ;; Be Done
  339. 0D01001F # RET R15
  340. # ;; ThrowAwaypointer function
  341. # ;; Handle the special case of a generic problem
  342. # ;; for Pass1, Will update R11 and modify R0
  343. # ;; Will return to the start of first_pass
  344. # ;; Never call this function, only jump to it
  345. # :ThrowAwayPointer 324
  346. :6
  347. E1000FBB0002 # ADDUI R11 R11 2 ; Pointers always take up 2 bytes
  348. E0002D0F @j # CALLI R15 @throwAwayToken ; Get rid of rest of token
  349. 3C00 @3 # JUMP @first_pass ; Then return to the proper place
  350. # ;; ThrowAwayAddress function
  351. # ;; Handle the case of a 32bit absolute address storage
  352. # ;; for Pass1, Will update R11 and modify R0
  353. # ;; Will return to the start of first_pass
  354. # ;; Never call this function, conly jump to it
  355. # :ThrowAwayAddress 334
  356. :7
  357. E1000FBB0004 # ADDUI R11 R11 4 ; Addresses on 32bit systems take up 4 bytes
  358. E0002D0F @j # CALLI R15 @throwAwayToken ; Get rid of rest of token
  359. 3C00 @3 # JUMP @first_pass ; Then return to the proper place
  360. # ;; ThrowAwaylabel function
  361. # ;; Handle the special case of a generic problem
  362. # ;; for Pass2, Will update R11 and modify R0
  363. # ;; Will return to the start of second_pass
  364. # ;; Never call this function, only jump to it
  365. # :ThrowAwayLabel 344
  366. :9
  367. E0002D0F @j # CALLI R15 @throwAwayToken ; Get rid of rest of token
  368. 3C00 @4 # JUMP @second_pass
  369. # ;; Throw away token function
  370. # ;; Deals with the general case of not wanting
  371. # ;; The rest of the characters in a token
  372. # ;; This Will alter the values of R0 R1
  373. # ;; Returns back to whatever called it
  374. # :throwAwayToken 34e
  375. :j
  376. 42100100 # FGETC ; Read a Char
  377. # ;; Stop looping if space
  378. E000A0300020 # CMPSKIPI.NE R0 32
  379. 0D01001F # RET R15
  380. # ;; Stop looping if tab
  381. E000A0300009 # CMPSKIPI.NE R0 9
  382. 0D01001F # RET R15
  383. # ;; Stop looping if newline
  384. E000A030000a # CMPSKIPI.NE R0 10
  385. 0D01001F # RET R15
  386. # ;; Stop looping if EOF
  387. E000A030ffff # CMPSKIPI.NE R0 -1
  388. 0D01001F # RET R15
  389. # ;; Otherwise keep looping
  390. 3C00 @j # JUMP @throwAwayToken
  391. # ;; Hex function
  392. # ;; This function is serving three purposes:
  393. # ;; Identifying hex characters
  394. # ;; Purging line comments
  395. # ;; Returning the converted value of a hex character
  396. # ;; This function will alter the values of R0
  397. # ;; Returns back to whatever called it
  398. # :hex 37e
  399. :8
  400. # ;; Deal with line comments starting with #
  401. E000A0300023 # CMPSKIPI.NE R0 35
  402. 3C00 @k # JUMP @ascii_comment
  403. # ;; Deal with line comments starting with ;
  404. E000A030003b # CMPSKIPI.NE R0 59
  405. 3C00 @k # JUMP @ascii_comment
  406. # ;; Deal with all ascii less than '0'
  407. E000A0100030 # CMPSKIPI.GE R0 48
  408. 3C00 @l # JUMP @ascii_other
  409. # ;; Deal with '0'-'9'
  410. E000A0000039 # CMPSKIPI.G R0 57
  411. 3C00 @m # JUMP @ascii_num
  412. # ;; Deal with all ascii less than 'A'
  413. E000A0100041 # CMPSKIPI.GE R0 65
  414. 3C00 @l # JUMP @ascii_other
  415. # ;; Unset high bit to set everything into uppercase
  416. E100B00000df # ANDI R0 R0 0xDF
  417. # ;; Deal with 'A'-'F'
  418. E000A0000046 # CMPSKIPI.G R0 70
  419. 3C00 @n # JUMP @ascii_high
  420. # ;; Ignore the rest
  421. 3C00 @l # JUMP @ascii_other
  422. # :ascii_num 3c4
  423. :m
  424. E10011000030 # SUBUI R0 R0 48
  425. 0D01001F # RET R15
  426. # :ascii_high 3ce
  427. :n
  428. E10011000037 # SUBUI R0 R0 55
  429. 0D01001F # RET R15
  430. # :ascii_comment 3d8
  431. :k
  432. 42100100 # FGETC ; Read another char
  433. E0002CC0 @l # JUMP.NP R0 @ascii_other ; Stop with EOF
  434. E000A020000a # CMPSKIPI.E R0 10 ; Stop at the end of line
  435. 3C00 @k # JUMP @ascii_comment ; Otherwise keep looping
  436. # :ascii_other 3ec
  437. :l
  438. 0D000030 # TRUE R0
  439. 0D01001F # RET R15
  440. # ;; Where we will putting our stack
  441. # :stack 3f4
  442. :1