libc-core.M1 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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_x3 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. ; Stack offset
  31. rd_fp rs1_fp !4 addi
  32. ; Call main function
  33. rd_ra $FUNCTION_main jal
  34. ; Exit to kernel
  35. :FUNCTION_exit
  36. :FUNCTION__exit
  37. rd_a7 !93 addi ; Syscall for exit
  38. ecall ; Exit with code in a0