Sidan 1 How does the structure work? To calculate the branch - - PDF document

sidan 1
SMART_READER_LITE
LIVE PREVIEW

Sidan 1 How does the structure work? To calculate the branch - - PDF document

Example 2 Example tal 3 3 + 5 + 7 La $t0 tal Lw $t1 0($t0) 5 Lw $t2 4($t0) 7 Lw $t3 8($t0) Addiu $t1 $r0 3 Addu $t4 $t1 $t2 Addiu $t1 $t1 5 Addu $t4 $t4 $t3 Addiu $t1 $t1 7 Sw $t4 12($t0) Computer Engineering BranchProcCall


slide-1
SLIDE 1

Sidan 1

Computer Engineering BranchProcCall page 1

3 + 5 + 7

Addiu $t1 $r0 3 Addiu $t1 $t1 5 Addiu $t1 $t1 7

Example

Computer Engineering BranchProcCall page 2

La $t0 tal Lw $t1 0($t0) Lw $t2 4($t0) Lw $t3 8($t0) Addu $t4 $t1 $t2 Addu $t4 $t4 $t3 Sw $t4 12($t0) 3 5 7 tal

Example 2

Computer Engineering BranchProcCall page 3

The Branch Instruction

■ B label ■ “label” is an instruction address. ■ Instruction addresses are 32 bits. ■ But branch instructions are

immediate format, ONLY 16 BITS !?

Computer Engineering BranchProcCall page 4

How It’s Done!

x B z x, y, z all have y ! absolute values. ! ! But the quantity z ! z - y is an offset ! (a relative value).

■ If the offset z - y can be represented in

16 bits, then it will fit.

Computer Engineering BranchProcCall page 5

But There’s One More Trick....

■ z and y are valid byte addresses, both word aligned! ■ That means that:

z = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx00 y = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx00

so

z - y = xxxxxxxxxxxxxx00

■ Why waste space representing two

bits which are always zero?

Computer Engineering BranchProcCall page 6

But there’s one more trick....

■ So, shift the difference (z - y) right two bits. ■ That way, if (z - y) fits in 18 bits we can save the

  • ffset in 16 bits!
slide-2
SLIDE 2

Sidan 2

Computer Engineering BranchProcCall page 7

How does the structure work?

x B z When we fetch B z, y ! the program counter ! contains the value x. ! z ! That means y = PC + 4. ! Since offset = (z - y), then PC + 4 + offset = y + z - y, an absolute address!

Computer Engineering BranchProcCall page 8

To calculate the branch address

Program counter Instruction memory B Imm z - y shifted right twice Imm 00 Restore the two zeroes

18 bits 14 18

Sign extend to 32 bits 4 + Absolute branch address PC + 4

Computer Engineering BranchProcCall page 9 Branch logic Sgn/Ze extend Zero ext.

B label

ALU A B 31 4 + + Computer Engineering BranchProcCall page 10 Branch logic Sgn/Ze extend Zero ext.

B label

ALU A B 31 4 + + Computer Engineering BranchProcCall page 11 Branch logic Sgn/Ze extend Zero ext.

B label

ALU A B 31 4 + + Computer Engineering BranchProcCall page 12 Branch logic Sgn/Ze extend Zero ext.

Beq rs rt label

ALU A B 31 4 + +

slide-3
SLIDE 3

Sidan 3

Computer Engineering BranchProcCall page 13 Branch logic Sgn/Ze extend Zero ext.

Beq rs rt label

ALU A B 31 4 + + Computer Engineering BranchProcCall page 14 Branch logic Sgn/Ze extend Zero ext.

Beq rs rt label

ALU A B 31 4 + + Computer Engineering BranchProcCall page 15 Branch logic Sgn/Ze extend Zero ext.

Beq rs rt label

ALU A B 31 4 + +

?

Computer Engineering BranchProcCall page 16

Procedure call

■ Suppose we have a program that “rings the bell” at 10

different locations in the code.

■ This sort of thing is usually complicated!

many, many instructions.....

■ That’s a waste of space

can’t we write the “bell code” once and share it?

Computer Engineering BranchProcCall page 17

Procedure call

B bell back ! ! B bell Problem: The back ! return address! Bell ! ! Branch is not ! enough. B back

Computer Engineering BranchProcCall page 18

Return address A

■ We need to tell the subroutine

where it should branch back to,

– i.e., the return address,

each time we call it!

■ That means: The return address must be changeable.

slide-4
SLIDE 4

Sidan 4

Computer Engineering BranchProcCall page 19

Return address B

■ We can’t use B x; ■ X is not changeable

– we can’t write to the instruction memory...

■ But we can write to:

– register file – data memory

■ So we can write the return address there.

Computer Engineering BranchProcCall page 20

Return address C

■ Suppose we use $31 for example:

Bell ! ! ! Jr $31

■ Jump register $31:

– Read the register file and stores the value into the PC

Computer Engineering BranchProcCall page 21 Branch logic Sgn/Ze extend Zero ext.

Jr $31

ALU A B 31 4 + + Computer Engineering BranchProcCall page 22 Branch logic Sgn/Ze extend Zero ext.

Jr $31

ALU A B 31 4 + + Computer Engineering BranchProcCall page 23

This works (not the best way):

! bell: ! ! ! La $31 x ! B bell ! x: ! Jr $31 ! ! La $31 y B bell y: ! !

Computer Engineering BranchProcCall page 24

What’s done??

La $31 x 2 instructions B bell 1 instruction x: need a label

■ We just want to get the address

following the “call” into $31.

slide-5
SLIDE 5

Sidan 5

Computer Engineering BranchProcCall page 25

save return

address in

reg $31

Branch logic Sgn/Ze extend Zero ext.

La $31 x B bell x:

ALU A B 31 4 + +

Return address when B bell is fetched

Computer Engineering BranchProcCall page 26

The return address

■ The “call” is at “PC”. ■ So, the return address must be PC + 4. ■ But we compute PC + 4. ■ Can we cause PC + 4 to be written to the register file

($31).

■ Yes. Use BAL

bell (branch and link) .

Computer Engineering BranchProcCall page 27 Branch logic Sgn/Ze extend Zero ext.

Bal label

ALU A B 31 4 + + Computer Engineering BranchProcCall page 28 Branch logic Sgn/Ze extend Zero ext.

Bal label

ALU A B 31 4 + + Computer Engineering BranchProcCall page 29 Branch logic Sgn/Ze extend Zero ext.

Bal label

ALU A B 31 4 + + Computer Engineering BranchProcCall page 30 Branch logic Sgn/Ze extend Zero ext.

Bal label

ALU A B 31 4 + +

slide-6
SLIDE 6

Sidan 6

Computer Engineering BranchProcCall page 31 Branch logic Sgn/Ze extend Zero ext.

B label … next instr

ALU A B 31 4 + +