libc-full.M1 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_fp rs1_sp mv ; Protect stack pointer
  18. ; Prepare argv
  19. rd_a0 rs1_fp !4 addi ; ARGV_address = FP + 4
  20. rd_sp rs1_sp !-4 addi
  21. rs1_sp rs2_a0 sw ; Put argv on the stack
  22. ; Prepare envp
  23. rd_a0 rs1_fp mv ; Address we need to load from
  24. rd_a0 rs1_a0 lw ; Get ARGC
  25. rd_a0 rs1_a0 !2 addi ; OFFSET = ARGC + 2
  26. rd_a0 rs1_a0 rs2_x2 slli ; OFFSET = OFFSET * WORDSIZE
  27. rd_a0 rs1_fp rs2_a0 add ; ENVP_address = RSP + OFFSET
  28. rd_sp rs1_sp !-4 addi
  29. rs1_sp rs2_a0 sw ; Put envp on the stack
  30. rd_a1 ~GLOBAL__envp auipc ; Get _envp global
  31. rd_a1 rs1_a1 !GLOBAL__envp addi
  32. rs1_a1 rs2_a0 sw ; Save environment to _envp
  33. ; Stack offset
  34. rd_fp rs1_fp !4 addi
  35. ; Setup for malloc
  36. rd_ra $FUNCTION___init_malloc jal
  37. ; Setup for FILE*
  38. rd_ra $FUNCTION___init_io jal
  39. ; Call main function
  40. rd_ra $FUNCTION_main jal
  41. ; Put return value on the stack so that _exit gets it
  42. rd_sp rs1_sp !-8 addi
  43. rs1_sp rs2_a0 sw
  44. ; Exit to kernel
  45. :FUNCTION_exit
  46. rd_ra $FUNCTION___kill_io jal
  47. :FUNCTION__exit
  48. rd_a0 rs1_sp lw
  49. rd_a7 !93 addi ; Syscall for exit
  50. ecall ; Exit with code in a0
  51. :GLOBAL__envp
  52. NULL