libc-core.M1 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ## Copyright (C) 2016 Jeremiah Orians
  2. ## This file is part of M2-Planet.
  3. ##
  4. ## M2-Planet 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. ## M2-Planet 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 M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. :_start
  17. '0' SP BP NO_SHIFT MOVE_ALWAYS ; Setup Base Pointer
  18. ;; Prepare argv
  19. !4 R0 ADD BP ARITH_ALWAYS ; ARGV_address = BP + 4
  20. {R0} PUSH_ALWAYS ; Put argv on the stack
  21. ;; Prepare envp
  22. '0' BP R0 NO_SHIFT MOVE_ALWAYS ; Address we need to load from
  23. !0 R0 LOAD32 R0 MEMORY ; Get ARGC
  24. !2 R0 ADD R0 ARITH_ALWAYS ; OFFSET = ARGC + 2
  25. '0' R0 R0 '1' MOVE_ALWAYS ; OFFSET = OFFSET * WORDSIZE
  26. '0' R0 R0 ADD BP ARITH2_ALWAYS ; ENVP_address = BP + OFFSET
  27. {R0} PUSH_ALWAYS ; Put envp on the stack
  28. ;; Stack offset
  29. !4 BP ADD BP ARITH_ALWAYS ; Fix BP
  30. ^~FUNCTION_main CALL_ALWAYS ; Jump right into main
  31. !1 R7 LOADI8_ALWAYS ; Setup for final exit
  32. SYSCALL_ALWAYS ; Exit
  33. # Unsigned Divide
  34. :divide
  35. {R4} PUSH_ALWAYS ; Protect R4
  36. {R3} PUSH_ALWAYS ; Protect R3
  37. {R2} PUSH_ALWAYS ; Protect R2
  38. '0' R0 R3 NO_SHIFT MOVE_ALWAYS ; MOV R3,R0
  39. '0' R1 R2 NO_SHIFT MOVE_ALWAYS ; MOV R2,R1
  40. !0 R0 LOADI8_ALWAYS ; MOV R0,#0
  41. !0 CMPI8 R2 IMM_ALWAYS ; CMP R2,#0
  42. !1 R0 SUB R0 ARITH_LT ; SUBLT R0,R0,#1
  43. !0 CMPI8 R3 IMM_ALWAYS ; CMP R3,#0
  44. !0 R3 RSUB R3 ARITH_LT ; RSBLT R3,R3,#0
  45. '0' R0 R0 MVN_LT ; MVNLT R0,R0
  46. '0' R0 R4 NO_SHIFT MOVE_ALWAYS ; MOV R4,R0
  47. !32 R0 LOADI8_ALWAYS ; MOV R0,#32.
  48. !0 R1 LOADI8_ALWAYS ; MOV R1,#0
  49. :divide_loop
  50. '0' R2 R2 ADDS R2 ARITH2_ALWAYS ; ADDS R2,R2,R2
  51. '0' R1 R1 ADCS R1 ARITH2_ALWAYS ; ADCS R1,R1,R1
  52. '0' R3 CMP R1 AUX_ALWAYS ; CMP R1,R3
  53. '0' R3 R1 SUB R1 ARITH2_GE ; SUBGE R1,R1,R3
  54. !1 R2 ADD R2 ARITH_GE ; ADDGE R2,R2,#1
  55. !1 R0 SUB R0 ARITH_ALWAYS ; SUB R0,R0,#1
  56. !0 CMPI8 R0 IMM_ALWAYS ; CMP R0,#0
  57. ^~divide_loop JUMP_NE ; BNE loop
  58. '0' R2 R0 NO_SHIFT MOVE_ALWAYS ; MOV R0,R2
  59. {R2} POP_ALWAYS ; Restore R2
  60. {R3} POP_ALWAYS ; Restore R3
  61. {R4} POP_ALWAYS ; Restore R4
  62. '1' LR RETURN
  63. # Signed Divide
  64. :divides
  65. {R4} PUSH_ALWAYS ; Protect R4
  66. {R3} PUSH_ALWAYS ; Protect R3
  67. {R2} PUSH_ALWAYS ; Protect R2
  68. '0' R0 R3 NO_SHIFT MOVE_ALWAYS ; MOV R3,R0
  69. '0' R1 R2 NO_SHIFT MOVE_ALWAYS ; MOV R2,R1
  70. !0 R0 LOADI8_ALWAYS ; MOV R0,#0
  71. !0 CMPI8 R2 IMM_ALWAYS ; CMP R2,#0
  72. !0 R2 RSUB R2 ARITH_LT ; RSBLT R2,R2,#0
  73. !1 R0 SUB R0 ARITH_LT ; SUBLT R0,R0,#1
  74. !0 CMPI8 R3 IMM_ALWAYS ; CMP R3,#0
  75. !0 R3 RSUB R3 ARITH_LT ; RSBLT R3,R3,#0
  76. '0' R0 R0 MVN_LT ; MVNLT R0,R0
  77. '0' R0 R4 NO_SHIFT MOVE_ALWAYS ; MOV R4,R0
  78. !32 R0 LOADI8_ALWAYS ; MOV R0,#32.
  79. !0 R1 LOADI8_ALWAYS ; MOV R1,#0
  80. :divides_loop
  81. '0' R2 R2 ADDS R2 ARITH2_ALWAYS ; ADDS R2,R2,R2
  82. '0' R1 R1 ADCS R1 ARITH2_ALWAYS ; ADCS R1,R1,R1
  83. '0' R3 CMP R1 AUX_ALWAYS ; CMP R1,R3
  84. '0' R3 R1 SUB R1 ARITH2_GE ; SUBGE R1,R1,R3
  85. !1 R2 ADD R2 ARITH_GE ; ADDGE R2,R2,#1
  86. !1 R0 SUB R0 ARITH_ALWAYS ; SUB R0,R0,#1
  87. !0 CMPI8 R0 IMM_ALWAYS ; CMP R0,#0
  88. ^~divides_loop JUMP_NE ; BNE loop
  89. !0 CMPI8 R4 IMM_ALWAYS ; CMP R4,#0
  90. !0 R2 RSUB R2 ARITH_NE ; RSBNE R2,R2,#0
  91. '0' R2 R0 NO_SHIFT MOVE_ALWAYS ; MOV R0,R2
  92. {R2} POP_ALWAYS ; Restore R2
  93. {R3} POP_ALWAYS ; Restore R3
  94. {R4} POP_ALWAYS ; Restore R4
  95. '1' LR RETURN
  96. # Unsigned Modulus
  97. :modulus
  98. {LR} PUSH_ALWAYS ; Prepare to leverage divide
  99. ^~divide CALL_ALWAYS ; Use divide
  100. '0' R1 R0 NO_SHIFT MOVE_ALWAYS ; MOV R0,R1
  101. {LR} POP_ALWAYS ; Prepare for return
  102. '1' LR RETURN
  103. # Signed Modulus
  104. :moduluss
  105. {LR} PUSH_ALWAYS ; Prepare to leverage divide
  106. ^~divides CALL_ALWAYS ; Use divides
  107. '0' R1 R0 NO_SHIFT MOVE_ALWAYS ; MOV R0,R1
  108. {LR} POP_ALWAYS ; Prepare for return
  109. '1' LR RETURN