crt1.M1 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ## Copyright (C) 2021 Andrius Štikonas
  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. RD_SP RS1_SP !-8 ADDI
  18. RS1_SP RS2_FP SD
  19. RD_FP RS1_SP MV ; Protect stack pointer
  20. RD_SP RS1_SP !-4180 ADDI
  21. RD_A0 ~GLOBAL___stdin AUIPC
  22. RD_A0 RS1_A0 !GLOBAL___stdin ADDI
  23. RS1_A0 SD
  24. RD_A0 ~GLOBAL___stdout AUIPC
  25. RD_A0 RS1_A0 !GLOBAL___stdout ADDI
  26. RD_T0 !1 ADDI
  27. RS1_A0 RS2_T0 SD
  28. RD_A0 ~GLOBAL___stderr AUIPC
  29. RD_A0 RS1_A0 !GLOBAL___stderr ADDI
  30. RD_T0 !2 ADDI
  31. RS1_A0 RS2_T0 SD
  32. RD_TP RS1_SP MV
  33. ; argc
  34. RD_A0 RS1_FP !8 ADDI
  35. RD_A0 RS1_A0 LD
  36. RD_SP RS1_SP !-8 ADDI
  37. RS1_SP RS2_A0 SD
  38. ; argv
  39. RD_A0 RS1_FP !16 ADDI
  40. RD_SP RS1_SP !-8 ADDI
  41. RS1_SP RS2_A0 SD
  42. ; Prepare envp
  43. RD_A0 RS1_FP !8 ADDI ; Address we need to load from
  44. RD_A0 RS1_A0 LD ; Get ARGC
  45. RD_A0 RS1_A0 !3 ADDI ; OFFSET = ARGC + 3
  46. RD_A0 RS1_A0 RS2_X3 SLLI ; OFFSET = OFFSET * WORDSIZE
  47. RD_A0 RS1_FP RS2_A0 ADD ; ENVP_address = RSP + OFFSET
  48. RD_T0 ~GLOBAL_environ AUIPC
  49. RD_T0 RS1_T0 !GLOBAL_environ ADDI
  50. RS1_T0 RS2_A0 SD
  51. RD_SP RS1_SP !-8 ADDI
  52. RS1_SP RS2_A0 SD
  53. RD_FP RS1_TP MV
  54. ; Call main function
  55. RD_RA $FUNCTION_main JAL
  56. ; Exit to kernel
  57. :FUNCTION_exit
  58. :FUNCTION__exit
  59. RD_A7 !93 ADDI ; Syscall for exit
  60. ECALL ; Exit with code in a0