123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- ## Copyright (C) 2021 Andrius Štikonas
- ## This file is part of stage0.
- ##
- ## stage0 is free software: you can redistribute it and/or modify
- ## it under the terms of the GNU General Public License as published by
- ## the Free Software Foundation, either version 3 of the License, or
- ## (at your option) any later version.
- ##
- ## stage0 is distributed in the hope that it will be useful,
- ## but WITHOUT ANY WARRANTY; without even the implied warranty of
- ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ## GNU General Public License for more details.
- ##
- ## You should have received a copy of the GNU General Public License
- ## along with stage0. If not, see <http://www.gnu.org/licenses/>.
- :_start
- rd_fp rs1_sp mv ; Protect stack pointer
- ; Prepare argv
- rd_a0 rs1_fp !4 addi ; ARGV_address = FP + 4
- rd_sp rs1_sp !-4 addi
- rs1_sp rs2_a0 sw ; Put argv on the stack
- ; Prepare envp
- rd_a0 rs1_fp mv ; Address we need to load from
- rd_a0 rs1_a0 lw ; Get ARGC
- rd_a0 rs1_a0 !2 addi ; OFFSET = ARGC + 2
- rd_a0 rs1_a0 rs2_x2 slli ; OFFSET = OFFSET * WORDSIZE
- rd_a0 rs1_fp rs2_a0 add ; ENVP_address = RSP + OFFSET
- rd_sp rs1_sp !-4 addi
- rs1_sp rs2_a0 sw ; Put envp on the stack
- rd_a1 ~GLOBAL__envp auipc ; Get _envp global
- rd_a1 rs1_a1 !GLOBAL__envp addi
- rs1_a1 rs2_a0 sw ; Save environment to _envp
- ; Stack offset
- rd_fp rs1_fp !4 addi
- ; Setup for malloc
- rd_ra $FUNCTION___init_malloc jal
- ; Setup for FILE*
- rd_ra $FUNCTION___init_io jal
- ; Call main function
- rd_ra $FUNCTION_main jal
- ; Put return value on the stack so that _exit gets it
- rd_sp rs1_sp !-8 addi
- rs1_sp rs2_a0 sw
- ; Exit to kernel
- :FUNCTION_exit
- rd_ra $FUNCTION___kill_io jal
- :FUNCTION__exit
- rd_a0 rs1_sp lw
- rd_a7 !93 addi ; Syscall for exit
- ecall ; Exit with code in a0
- :GLOBAL__envp
- NULL
|