add_node.s 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. ;; add_node Function
  17. ;; Receives pointers in R0 R1
  18. ;; Alters R0 if NULL
  19. ;; Appends nodes together
  20. ;; Returns to whatever called it
  21. :add_node
  22. ;; Preserve Registers
  23. PUSHR R2 R15
  24. PUSHR R1 R15
  25. PUSHR R0 R15
  26. ;; Handle if Head is NULL
  27. JUMP.NZ R0 @add_node_0
  28. POPR R0 R15
  29. PUSHR R1 R15
  30. JUMP @add_node_2
  31. :add_node_0
  32. ;; Handle if Head->next is NULL
  33. LOAD32 R2 R0 0
  34. JUMP.NZ R2 @add_node_1
  35. ;; Set head->next = p
  36. STORE32 R1 R0 0
  37. ;; Set p->prev = head
  38. STORE32 R0 R1 4
  39. JUMP @add_node_2
  40. :add_node_1
  41. ;; Handle case of Head->next not being NULL
  42. LOAD32 R0 R0 0 ; Move to next node
  43. LOAD32 R2 R0 0 ; Get node->next
  44. CMPSKIPI.E R2 0 ; If it is not null
  45. JUMP @add_node_1 ; Move to the next node and try again
  46. JUMP @add_node_0 ; Else simply act as if we got this node
  47. ; in the first place
  48. :add_node_2
  49. ;; Restore registers
  50. POPR R0 R15
  51. POPR R1 R15
  52. POPR R2 R15
  53. RET R15