readline.s 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. ;; Readline function
  17. ;; Recieves Pointer to node in R0
  18. ;; And Input in R1
  19. ;; Allocates Text segment on Heap
  20. ;; Sets node's pointer to Text segment
  21. ;; Sets R14 to True if EOF reached
  22. ;; Requires a malloc function to exist
  23. ;; Returns to whatever called it
  24. :Readline
  25. ;; Preserve registers
  26. PUSHR R0 R15
  27. PUSHR R1 R15
  28. PUSHR R2 R15
  29. PUSHR R3 R15
  30. PUSHR R4 R15
  31. ;; Initialize
  32. MOVE R4 R0
  33. FALSE R0 ; Get where space is free
  34. CALLI R15 @malloc
  35. MOVE R2 R0
  36. FALSE R3
  37. :Readline_0
  38. FGETC ; Read a Char
  39. ;; Flag if reached EOF
  40. CMPSKIPI.GE R0 0
  41. TRUE R14
  42. ;; Stop if EOF
  43. CMPSKIPI.GE R0 0
  44. JUMP @Readline_2
  45. ;; Handle Backspace
  46. CMPSKIPI.E R0 127
  47. JUMP @Readline_1
  48. ;; Move back 1 character if R3 > 0
  49. CMPSKIPI.LE R3 0
  50. SUBUI R3 R3 1
  51. ;; Hopefully they keep typing
  52. JUMP @Readline_0
  53. :Readline_1
  54. ;; Replace all CR with LF
  55. CMPSKIPI.NE R0 13
  56. LOADUI R0 10
  57. ;; Store the Byte
  58. STOREX8 R0 R2 R3
  59. ;; Prep for next loop
  60. ADDUI R3 R3 1
  61. ;; Check for EOL
  62. CMPSKIPI.NE R0 10
  63. JUMP @Readline_2
  64. ;; Otherwise loop
  65. JUMP @Readline_0
  66. :Readline_2
  67. ;; Set Text pointer
  68. CMPSKIPI.E R3 0 ; Don't bother for Empty strings
  69. STORE32 R2 R4 8
  70. ;; Correct Malloc
  71. MOVE R0 R3 ; Ensure actually allocates exactly
  72. CALLI R15 @malloc ; the amount of space required
  73. ;; Restore Registers
  74. POPR R4 R15
  75. POPR R3 R15
  76. POPR R2 R15
  77. POPR R1 R15
  78. POPR R0 R15
  79. RET R15