I am trying to add jal functionality to the following but I am stuck with how does it work. I know that it stores the old PC+4
value in the $ra
register and then transfers the control to the function which transfers back the control by return $ra
but how do I implement it in the hardware?
Asked
Active
Viewed 9,956 times
3
2 Answers
2
There are two things you need to do.
- Add a mux at the input of the Registers so that the PC+4 value can be selected as the data to be written. With the appropriate control signal this will allow you to write PC+4 as an additional effect of the "jal $ra" instruction.
- Implement the return "jr $ra" instruction. You will need to add a mux to the chain of logic that selects the next PC so that "read data 1" from the register file can be selected as the next PC when the instruction is "jr xxx".

markgz
- 6,054
- 1
- 19
- 41
-
I was also thinking that there might be another mux which hardwires the $r1 reg into the write register? How about this? – Sep 20 '11 at 16:10
-
Did you mean $ra? If so, $ra i.e $31 is only used by convention. "jal $nn" is legal for any register $nn, so your logic must decode the destination register from the "jr" instruction and write to that register. – markgz Sep 20 '11 at 21:35
-
@markgz How is it possible for `jal $nn` to work? jal is a J-Type instruction -- there are 6 bits allocated for the opcode, 26 bits allocated for the target address. There is no `jr` register from which one can discern any other register to store the return address in (assuming we are discussing MIPS). – BSchlinker Apr 23 '12 at 01:52
-
@BSchlinker: my recollection was incorrect. The encoding for `JAL` is different than for `JALR`. `JAL` does hardcode `$31` for the return address. `JALR $rt, $rs` uses `$rs` for the destination address and stores the return address in `$rt`. – markgz Apr 23 '12 at 18:10
-
Looks like I made a mistake in my comment also -- by `jr` register I actually meant `rt / rs / rd` registers (there are no register references provided in a `jal` instruction, so as you noted, `$31` is hardcoded for the return address) – BSchlinker Apr 25 '12 at 07:34
0
We ad a new line that takes the PC+4 and sends it to the "Write Data" input of the register file. We need to add a multiplexer. We also need to make sure the "Write Register" field is set to the address of $RA. Register $RA is register number 31 most of the time.

ShowLove
- 899
- 1
- 12
- 21