Jump Instruction - muneeb-mbytes/computerArchitectureCourse GitHub Wiki
Jump Instruction in MIPS
-
Using J instruction, we cannot jump anywhere in the memory space
-
We can specify a range given in 26 bits
-
This 26 bit address is taken as a word address
-
So effectively in word it will be 28 bits
-
Hence jump within a segment will be 2 power 28 i.e 256MB
-
With 32 bits we can get 2 power 32 i.e 4GB of memory
In a 32-bit memory address system, each memory address is represented using 32 bits. This allows for a total of 2^32 unique memory addresses, which equals 4 gigabytes (GB) of addressable memory space.
To ensure that memory accesses are aligned to 4-byte boundaries, the last two bits of the memory address are set to zero
-
4GB space is divided into segments of size 256MB each
- Say for example if the current instruction is in segment 2, then we can jump to any instruction within that segment 2.
- An exception for this includes whenever there is a jump for the last word in the segment
- Because as the initial value PC=PC+4, jump will occur to the next segment
Jump Instruction in RISC V
There are 2 types of jump instructions in RISC V
1) Jump and link (jal)
- For conditional statements like if else and for looping statements like while loop we use branch instructions
- But, for function calls we don't use branch instructions
- We may be jumping to some far off location using function calls
Hence we will be using Jump and link (jal) which is a UJ format instruction
So how it works?
Lets us assume that the 20 bits stored in the instruction be: 10000100000110011001
using these 20 bits we need to create a 32 bit immediate address
The LSB 8 bits are 10011001 are placed in position [19:12] of the 32 bit immediate address
next 1 bit i.e 1 is placed in position 11
next 10 bits i.e 0000100000 are placed in position [10:1]
and the last bit i.e 1 is placed in position 20
This is the way how we will create a 32 bit immediate address in the case of jal instruction from the 20 bits that are stored in the instruction
The opcode for jal instruction is: 1101111
Functionality of jal instruction
Example: jal x5, Imm[20:1]
- Initially the value of PC will be 0
- Here it computes the return address PC+4 and store it in register x5 (i.e x5 <- PC+4)
- Then PC gets changed to PC=PC+40 so that we can move from jal to I5 (which is the function call)
- Function gets executed in I5 and the return address will take the value of x5. PC <-x5 (i.e PC=4)
2) Jump and link register (jalr)
This is a special instruction which belongs to I-format which is mainly used to implement the return address.
The return address ret is a sudo instruction.
What is sudo instruction? Sudo instruction is not a actual instruction, It is implemented by some other actual instruction i.e jalr
The opcode for jalr instruction is: 1100111
Here the immediate instruction will be 12 bits There is no multiplication by 2 (no shifting of bits)
- Writes PC+4 to register destination rd (i.e rd <- PC+4)
- Sets PC=[contents of rs]+ {32 bit sign extended Immediate address}
Example: jalr x0, ra, 0
- Here at first x0 <- PC+4 (i.e the return address will be zero)
- PC=[contents of ra] + 0 (i.e PC <- ra)
Jalr instruction works similar to jal instruction but the only difference would be
jal | jalr |
---|---|
It follows UJ format Instruction | It follows I format Instruction |
opcode= 1101111 | opcode = 1100111 |
It stores return address (PC+4) in its destination register and PC is PC+ offset, where offset =(sign extended to 32 bits)[31:21]imm[20:1]0 | It's functionality is it writes PC+4 to rd and it sets PC= [rs1]+imm (sign extended to 32 bits) |
Multiplication by 2 (logical shift) | No logical shift |