Basics
Registers
Pseudo Instructions
li
is used to load an immediate value into a register.la
is used to load an address into a register. (also can load a label address)mv
is used to move a value from one register to another. (inclusive of addresses)ret
is used to return from a function using thera
register.
Print the prompt and read an integer
|
|
Branch will not store return address(ra)
blt, bge, bltu, bgeu, etc. will not also set the ra
register. If you want to use the return address, you need to save it before the branch instruction. or use labels and jal
instruction.
eg. the following code is a function demostrates 1
times a2
to a3 + 1
:
|
|
in line 6, where I invoke the raise_int_max_exceeded
function, I save the return address in t3
register. and in the raise_int_max_exceeded
function, I use jr t3
to return to the caller.
Difference between jal
, jalr
, j
, jr
jal
andjalr
is used to jump to a label and save the return address inra
register.jalr
is used to jump to a register and save the return address inra
register.j
is used to jump to a label.jr
is used to jump to a register.
Use stack to save registers
eg.
|
|
for more info, visit this blog post: RISC-V: Use Stack to Store Return Address