strcmp.s 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. # We will be using R0 and R1 to pass values to the function
  17. # R15 will be used as the stack pointer
  18. :start
  19. LOADUI R0 @string
  20. COPY R1 R0
  21. LOADUI R2 33
  22. LOADUI R3 44
  23. LOADUI R4 55
  24. LOADUI R15 0x600
  25. CALLI R15 @strcmp
  26. HALT
  27. :string
  28. HALT
  29. NOP
  30. ;; Our simple string compare function
  31. :strcmp
  32. ;; Preserve registers
  33. PUSHR R2 R15
  34. PUSHR R3 R15
  35. PUSHR R4 R15
  36. ;; Setup registers
  37. MOVE R2 R0
  38. MOVE R3 R1
  39. LOADUI R4 0
  40. :cmpbyte
  41. LOADXU8 R0 R2 R4 ; Get a byte of our first string
  42. LOADXU8 R1 R3 R4 ; Get a byte of our second string
  43. ADDUI R4 R4 1 ; Prep for next loop
  44. CMP R1 R0 R1 ; Compare the bytes
  45. CMPSKIPI.E R0 0 ; Stop if byte is NULL
  46. JUMP.E R1 @cmpbyte ; Loop if bytes are equal
  47. ;; Done
  48. MOVE R0 R1 ; Prepare for return
  49. ;; Restore registers
  50. POPR R4 R15
  51. POPR R3 R15
  52. POPR R2 R15
  53. RET R15