Basics
Registers

Pseudo Instructions
liis used to load an immediate value into a register.lais used to load an address into a register. (also can load a label address)mvis used to move a value from one register to another. (inclusive of addresses)retis used to return from a function using theraregister.
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
jalandjalris used to jump to a label and save the return address inraregister.jalris used to jump to a register and save the return address inraregister.jis used to jump to a label.jris 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