#import "riscv.typ"

#set page(width: auto, height: auto, margin: 1cm)

#riscv.explain-instruction-encoding("lui x0, 0x12")

#riscv.explain-instruction-encoding("add sp, a0, a1")

#pagebreak()

#riscv.riscv-to-bin(```
0x00000300 main:   jal ra, simple # call
0x00000304         add s0, s1, s1
0x0000051c simple: jalr x0, ra, 0 # return
```)

#pagebreak()

#riscv.riscv-to-bin(```
f1:
  addi sp, sp, -20 # make space on stack for 5 words
  sw a0, 16(sp)
  sw a1, 12(sp)
  sw ra, 8(sp)     # save ra on stack
  sw s4, 4(sp)
  sw s5, 0(sp)
  jal ra, f2
  lw ra, 8(sp)     # restore ra (and other regs) from stack
  addi sp, sp, 20  # deallocate stack space
  jalr zero, ra, 0

# f2 (leaf-function) only uses s4 and calls no functions
f2:
  addi sp, sp, -4  # make space on stack for 1 word
  sw s4, 0(sp)
  lw s4, 0(sp)
  addi sp, sp, 4   # deallocate stack space
  jalr zero, ra, 0
```)