RISC-V has no push and pop instructions to my surprise, so we need a way to make it easier to push registers and pop again
# macros's # ----------------------------------------------------------------------- # ***PUSH reg # *** example PUSH t0 # ----------------------------------------------------------------------- .macro PUSH reg addi sp, sp, -4 sw \reg, 0(sp) .endm # ----------------------------------------------------------------------- # *** POP reg # *** example POP t0 # ----------------------------------------------------------------------- .macro POP reg lw \reg, 0(sp) addi sp, sp, 4 .endm # ----------------------------------------------------------------------- # *** PUSH2 reg1, reg2 # *** example PUSH2 t0 t1 # ----------------------------------------------------------------------- .macro PUSH2 reg1, reg2 addi sp, sp, -8 sw \reg1, 0(sp) sw \reg2, 4(sp) .endm # ----------------------------------------------------------------------- # *** POP2 reg1, reg2 # *** example POP2 t0 t1 # ----------------------------------------------------------------------- .macro POP2 reg1, reg2 lw \reg2, 4(sp) lw \reg1, 0(sp) addi sp, sp, 8 .endm ...
I am still figuring pout what would be what would be most intuitive to use.
... PUSH2 t0 t1 # do something POP2 t0 t1 # We write the registers in the same order as the push ... # OR ... PUSH2 t0 t1 # do something POP2 t1 t0 # We write the registers in the reverse order as the stack ...
Pop same order as the push For me this is the most intuitive way and you can immediately spot if you made a error when you choose the registers to restore.
...
PUSH2 t0 t1
# do something
POP2 t0 t1 # We write the registers in the same order as the push
...
We extend the macros so we can for example push and pop all the arguments
# ----------------------------------------------------------------------- # *** PUSH all arguments # ----------------------------------------------------------------------- .macro PUSH_ARG_ALL PUSH8 a0, a1, a2, a3, a4, a5, a6, a7 .endm # ----------------------------------------------------------------------- # *** POP all arguments # ----------------------------------------------------------------------- .macro POP_ARG_ALL POP8 a0, a1, a2, a3, a4, a5, a6, a7 .endm
Example usage.to save all argument registers a0..a7, but we can also do that for temp registers because it has its own macro.
PUSH_ARG_ALL
# Your code here
POP_ARG_ALL
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.