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 page 1 Computer Engineering BranchProcCall page 2 The Branch Instruction How It’s Done! ■ B label x B z x, y, z all have y ! absolute values. ■ “label” is an instruction address. ! ! But the quantity ■ Instruction addresses are 32 bits. z ! z - y is an offset ! (a relative value). ■ But branch instructions are immediate format, ONLY 16 BITS !? ■ If the offset z - y can be represented in 16 bits, then it will fit. Computer Engineering BranchProcCall page 3 Computer Engineering BranchProcCall page 4 But There’s One More Trick.... But there’s one more trick.... ■ z and y are valid byte addresses, both word aligned! ■ So, shift the difference (z - y) right two bits. ■ That way, if (z - y) fits in 18 bits we can save the offset in 16 bits! ■ 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 5 Computer Engineering BranchProcCall page 6 Sidan 1
How does the structure work? To calculate the branch address Instruction memory x B z When we fetch B z, Program counter y ! the program counter B Imm z - y shifted right twice ! contains the value x. ! Imm Restore the two zeroes 00 z ! That means y = PC + 4. 18 bits ! Sign extend to 32 bits 14 18 Since offset = (z - y), then PC + 4 PC + 4 + offset = y + z - y, 4 + an absolute address! Absolute branch address Computer Engineering BranchProcCall page 7 Computer Engineering BranchProcCall page 8 Zero ext . Zero ext . Branch Branch logic 0 logic 0 A A ALU ALU 4 4 B B + + 31 + 31 + Sgn/Ze Sgn /Ze extend extend B label B label Computer Engineering BranchProcCall page 9 Computer Engineering BranchProcCall page 10 Zero ext . Zero ext . Branch Branch logic 0 logic 0 A A ALU ALU 4 B 4 B + + 31 + 31 + Sgn /Ze Sgn/Ze extend extend B label Beq rs rt label Computer Engineering BranchProcCall page 11 Computer Engineering BranchProcCall page 12 Sidan 2
Zero ext . Zero ext . Branch Branch logic 0 logic 0 A A ALU ALU 4 4 B B + + 31 + 31 + Sgn /Ze Sgn /Ze extend extend Beq rs rt label Beq rs rt label Computer Engineering BranchProcCall page 13 Computer Engineering BranchProcCall page 14 Procedure call Zero ext . Branch ■ Suppose we have a program that “rings the bell” at 10 logic 0 A different locations in the code. ALU 4 B + ? ■ This sort of thing is usually complicated! 31 + Sgn /Ze many, many instructions..... extend ■ That’s a waste of space can’t we write the “bell code” once and share it? Beq rs rt label Computer Engineering BranchProcCall page 15 Computer Engineering BranchProcCall page 16 Procedure call Return address A B bell ■ We need to tell the subroutine where it should branch back to, back ! – i.e., the return address, ! each time we call it! B bell Problem: The back ! return address! ■ That means: The return address must be changeable. Bell ! ! Branch is not ! enough. B back Computer Engineering BranchProcCall page 17 Computer Engineering BranchProcCall page 18 Sidan 3
Return address B Return address C ■ We can’t use B x; ■ Suppose we use $31 for example: Bell ! ■ X is not changeable ! – we can’t write to the instruction memory... ! Jr $31 ■ But we can write to: – register file ■ Jump register $31: – data memory – Read the register file and stores the value into the PC ■ So we can write the return address there. Computer Engineering BranchProcCall page 19 Computer Engineering BranchProcCall page 20 Zero ext . Zero ext . Branch Branch logic 0 logic 0 A A ALU ALU 4 4 B B + + 31 + 31 + Sgn/Ze Sgn/Ze extend extend Jr $31 Jr $31 Computer Engineering BranchProcCall page 21 Computer Engineering BranchProcCall page 22 This works (not the best way): What’s done?? ! bell: ! La $31 x 2 instructions ! ! B bell 1 instruction La $31 x ! x: need a label B bell ! x: ! Jr $31 ! ■ We just want to get the address ! following the “call” into $31. La $31 y B bell y: ! ! Computer Engineering BranchProcCall page 23 Computer Engineering BranchProcCall page 24 Sidan 4
The return address Zero ext . Return address save return when B bell Branch address in ■ The “call” is at “PC”. is fetched logic 0 A reg $31 ALU 4 B + ■ So, the return address must be PC + 4. 31 + Sgn/Ze ■ But we compute PC + 4. extend ■ Can we cause PC + 4 to be written to the register file ($31). La $31 x B bell x: ■ Yes. Use BAL bell (branch and link) . Computer Engineering BranchProcCall page 25 Computer Engineering BranchProcCall page 26 Zero ext . Zero ext . Branch Branch logic 0 logic 0 A A ALU ALU 4 4 B B + + 31 + 31 + Sgn/Ze Sgn/Ze extend extend Bal label Bal label Computer Engineering BranchProcCall page 27 Computer Engineering BranchProcCall page 28 Zero ext . Zero ext . Branch Branch logic 0 logic 0 A A ALU ALU 4 B 4 B + + 31 + 31 + Sgn/Ze Sgn /Ze extend extend Bal label Bal label Computer Engineering BranchProcCall page 29 Computer Engineering BranchProcCall page 30 Sidan 5
Zero ext . Branch logic 0 A ALU 4 B + 31 + Sgn/Ze extend B label … next instr Computer Engineering BranchProcCall page 31 Sidan 6
Recommend
More recommend