Jumps and such
Some material is from page 113-116, part is from my memory.
First remember (or learn) that all instructions are word aligned.
This means that the word at byte 0 takes up bytes 0,1,2,3
So the next word starts at 4 and takes up bytes 4,5,6,7
and 8,9, a
16
, b
16
and c
16, d
16
e
16
, f
16
Note all addresses 0000, 0100, 1000, 1100 end in 00
So we can really specify our branch address in words, not bytes
Thus the shift left on the picture on 263 (for example)
We are now using 18 of the 32 bit branch address.
From where should the other 14 bits come?
The solution is a PC-relative branch
Ie, how far away are we likely to branch?
Branches are mostly used for goto, if, while.
We will see function calls somewhere else.
We will discuss the "Principle of Locality" in more detail later but for now.
The principle states that if we used an instruction, we are likely to use the instructions around it.
This is applied to data
Both in time and space.
But instructions, to us at least, are just data.
So we can assume that if we need to branch, we will branch "close to home".
Remember we are still branching within 2
15
-1 words (32,767 instructions each way).
So beq, bne say PC= PC+4+BranchAddr
What is the +4?
Write this program and see if it does what you expect
.text main: li $t0, 0 top: beq $t0,10, out addi $t0, $t0, 1 b top out: li $v0, 10 SYSCALL
Jumps
There are three of these. (J, JAL, JR)
JAL and JR are for function calls, so we will ignore these for now.
But JR along with LUI and ORI could be the answer to everything.
The green card says PC = JumpAddr (but this is not very useful)
Remember, we get 2 bits free for word aligned.
And there are 26 bits in the jump address (J type instruction)
So we are short by 4 bits. Where should these come from?
The answer is the top for bits of the PC
Look at page 271.