mirror of
https://github.com/plctlab/riscv-operating-system-mooc.git
synced 2025-12-25 08:47:33 +00:00
- https://gitee.com/unicornx/riscv-operating-system-mooc/issues/I4QLTP - https://gitee.com/unicornx/riscv-operating-system-mooc/issues/I4PJTQ - https://gitee.com/unicornx/riscv-operating-system-mooc/issues/I49VW5 Signed-off-by: Wang Chen <wangchen20@iscas.ac.cn>
60 lines
892 B
ArmAsm
60 lines
892 B
ArmAsm
# Calling Convention
|
|
# Demo to create a leaf routine
|
|
#
|
|
# void _start()
|
|
# {
|
|
# // calling leaf routine
|
|
# square(3);
|
|
# }
|
|
#
|
|
# int square(int num)
|
|
# {
|
|
# return num * num;
|
|
# }
|
|
|
|
.text # Define beginning of text section
|
|
.global _start # Define entry _start
|
|
|
|
_start:
|
|
la sp, stack_end # prepare stack for calling functions
|
|
|
|
li a0, 3
|
|
call square
|
|
|
|
# the time return here, a0 should stores the result
|
|
stop:
|
|
j stop # Infinite loop to stop execution
|
|
|
|
# int square(int num)
|
|
square:
|
|
# prologue
|
|
addi sp, sp, -8
|
|
sw s0, 0(sp)
|
|
sw s1, 4(sp)
|
|
|
|
# `mul a0, a0, a0` should be fine,
|
|
# programing as below just to demo we can contine use the stack
|
|
mv s0, a0
|
|
mul s1, s0, s0
|
|
mv a0, s1
|
|
|
|
# epilogue
|
|
lw s0, 0(sp)
|
|
lw s1, 4(sp)
|
|
addi sp, sp, 8
|
|
|
|
ret
|
|
|
|
# add nop here just for demo in gdb
|
|
nop
|
|
|
|
# allocate stack space
|
|
stack_start:
|
|
.rept 12
|
|
.word 0
|
|
.endr
|
|
stack_end:
|
|
|
|
.end # End of file
|
|
|