17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.
-
Upload
anastasia-bruce -
Category
Documents
-
view
241 -
download
3
Transcript of 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.
J Type
• 26 bit location 32 bit address– Shift left 2
Word addresses only– Steal left 4 bits from PC• Direct jump only to
same region of memory
Region 1111
Region …
Region …
Region …
Region …
Region …
Region …
Region …
Region …
Region 0110
Region 0101
Region 0100
Region 0011
Region 0010
Region 0001
Region 0000
Decoding J
• Jump Instruction:0x0810002code address0000 1000 0001 0000 0000 0000 0010
• Shift: 0000 0100 0000 0000 0000 1000
• Copy first 4 bits of current address: (0x0040000c)0000 0000 0100 0000 0000 0000 1000
• 0x0040008 – effective address to jump to
Delayed Branch
• MIPS delay's branches– One extra op happens after branch/jump– Must enable in MARS settings
BEQ & BNE
• BEQ : Branch Equal• BNE : Branch Not Equal
beq $reg1, $reg2, label
• Compare 2 registers, possibly branch– Branch relative to current instruction– Range +/- ~219 instructions
If
• Assembly if’s are “backwards”
High Level Assembly
if(i == j) { k = 1;}…
Branch to cont if $i != $j $k = 1cont: ….
If / Else
• If/Else implemented with branch:– Branch to skip if body for else case– If body jumps to skip else body
High Level Assembly
if(i == j) { k = 1;}else { k = 2;}…
Branch to else if $i != $j $k = 1 jump to endifelse: $k = 2endif:…
• If/Else implemented with branch:– Branch to skip if body for else case– If body jumps to skip else body
If / Else
Other Branches / Sets
• Single register branches– bltz : register less than zero– blez : register less than or equal to zero– bgtz : register greater than zero– bgez : register greater than or equal to zero
If / Else
• Inequalities in assembly– Rewrite solved for 0– Invert for skip logic
High Level Assembly
if(i < j) { k = 1;}…
Calculate $i - $jBranch to endif if >= 0 $k = 1endif:…
If: Skip:
Set
• Set : changes a register 1 or 0 depending on condition of test– slt $a, $b, $c
set $A if $B < $C– sltu $a, $b, $c
set $A if $B < $C compare as unsigned values– slti $a, $b, value
set $A if $B <= immediate– sltiu $a, $b, value
set $A if $B <= immediate compare as unsigned