stage1_assembler-0.s 2.8 KB

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