0

If we are interested in storing the current program counter in the return address register ($ra), what path would we take? enter image description here

I am aware that this can either be a jal/jalr command, as I don't think there is another way to store the PC value in $ra otherwise. However, I am having difficulties understanding what is truly happening in the CPU.

  • 1
    MIPS also has a `bal` instruction (branch and link), so you can do it with a relative destination. (e.g. implemented as `bgezal $zero, target`: [Why are bgezal & bltzal basic instructions and not pseudo-instructions in MIPS?](https://stackoverflow.com/a/52683479)). Using `jal` would be silly because you already need to know the absolute address as a jump target, in which case you already know the current PC value and don't need to retrieve it. (Unless you have a function at a known location in this 1/16th of the address space which just returns... But that would be unlikely.) – Peter Cordes Aug 16 '21 at 18:32
  • 1
    Or even better, use a not-taken `bltzal $zero, anywhere` to still write `$ra = PC` without branching. – Peter Cordes Aug 16 '21 at 18:35

0 Answers0