stage1_assembler-0.s 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 1 ; Our toggle
  18. ;; R14 is storing our condition code
  19. ;; R15 is storing our nybble
  20. ;; Prep TAPE_01
  21. LOADUI R0 0x1100
  22. FOPEN_READ
  23. ;; Prep TAPE_02
  24. LOADUI R0 0x1101
  25. FOPEN_WRITE
  26. LOADUI R1 0x1100 ; Read from tape_01
  27. ;; Main program loop
  28. ;; Halts when done
  29. :loop
  30. FGETC ; Read a Char
  31. ;; Check for EOF
  32. JUMP.NP R0 @finish
  33. JUMP @hex ; Convert it
  34. :loop_1
  35. JUMP.NP R0 @loop ; Don't use nonhex chars
  36. JUMP.Z R11 @loop_2 ; Jump if toggled
  37. ;; Process first byte of pair
  38. ANDI R15 R0 0xF ; Store First nibble
  39. FALSE R11 ; Flip the toggle
  40. JUMP @loop
  41. :loop_2
  42. SL0I R15 4 ; Shift our first nibble
  43. ANDI R0 R0 0xF ; Mask out top
  44. ADD R0 R0 R15 ; Combine nibbles
  45. LOADI R11 1 ; Flip the toggle
  46. LOADUI R1 0x1101 ; Write the combined byte
  47. FPUTC ; To TAPE_02
  48. LOADUI R1 0x1100 ; Read from tape_01
  49. JUMP @loop ; Try to get more bytes
  50. ;; Hex function
  51. ;; Converts Ascii chars to their hex values
  52. ;; Or -1 if not a hex char
  53. ;; Returns to whatever called it
  54. :hex
  55. ;; Deal with line comments starting with #
  56. CMPSKIPI.NE R0 35
  57. JUMP @ascii_comment
  58. ;; Deal with line comments starting with ;
  59. CMPSKIPI.NE R0 59
  60. JUMP @ascii_comment
  61. ;; Deal with all ascii less than '0'
  62. CMPSKIPI.GE R0 48
  63. JUMP @ascii_other
  64. ;; Deal with '0'-'9'
  65. CMPSKIPI.G R0 57
  66. JUMP @ascii_num
  67. ;; Unset high bit to set everything into uppercase
  68. ANDI R0 R0 0xDF
  69. ;; Deal with all ascii less than 'A'
  70. CMPSKIPI.GE R0 65
  71. JUMP @ascii_other
  72. ;; Deal with 'A'-'F'
  73. CMPSKIPI.G R0 70
  74. JUMP @ascii_high
  75. ;; Ignore the rest
  76. JUMP @ascii_other
  77. :ascii_num
  78. SUBUI R0 R0 48
  79. JUMP @loop_1
  80. :ascii_high
  81. SUBUI R0 R0 55
  82. JUMP @loop_1
  83. :ascii_comment
  84. FGETC ; Read another char
  85. CMPSKIPI.E R0 10 ; Stop at the end of line
  86. JUMP @ascii_comment ; Otherwise keep looping
  87. :ascii_other
  88. TRUE R0
  89. JUMP @loop_1
  90. ;; Finish function
  91. ;; Cleans up at the end of the program
  92. ;; Performs the HALT
  93. :finish
  94. LOADUI R0 0x1100 ; Close TAPE_01
  95. FCLOSE
  96. LOADUI R0 0x1101 ; Close TAPE_02
  97. FCLOSE
  98. HALT