mirror of
https://github.com/plctlab/riscv-operating-system-mooc.git
synced 2025-11-16 12:34:47 +00:00
49 lines
843 B
ArmAsm
49 lines
843 B
ArmAsm
# Calling Convention
|
|
# Demo to create a leaf routine
|
|
|
|
.text # Define beginning of text section
|
|
.global _start # Define entry _start
|
|
|
|
_start: # Label, not really required
|
|
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 10
|
|
.word 0
|
|
.endr
|
|
stack_end:
|
|
|
|
.end # End of file
|
|
|