17 - jumps & branches. the pc pc marks next location in fetch, decode, execute cycle

30
17 - Jumps & Branches

Upload: anastasia-bruce

Post on 14-Dec-2015

236 views

Category:

Documents


3 download

TRANSCRIPT

17 - Jumps & Branches

The PC

• PC marks next location in Fetch, Decode, Execute cycle

Jumps

• PC not directly addressable– Modified by instructions

• j : Unconditional jump to label

Using J

• Label converted to address by assembler:

J Type

• J : Jump instruction

– 26 bit location

J Type

• J : Jump instruction

– 26 bits devoted to address of label

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

Delayed Branch

• One extra instruction always happens after jump– Shown in green in simulator

NOP

• NOP : no –op – Prevent unwanted work after branch

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

• If implemented with branch:– Skip ahead if NOT doing if body

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

ASM ABS

• Absolute value of A from memory

18 – Inequalities & Conditional Sets

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

2 Register BLT is Cheating

• Two register inequalities are psuedo-ops– Don't use this week

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:

If <

• If (i < j) k = 10;

Real If

Real C++ Real compiler output:

What is slt???

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

If compiler style

• If (i < j) k = 10; //using slt

Temp Check

• Set bit if 30 <= temp <= 55

Loop = jump backwards

int i = 0;while(i < 10) { //do stuff i++;}

Counting Loop

• While(i < 10)

Sum

• Sum 0…9

Real Code Counting Loop

• Compilers often movetest to end of loop:– Avoid separate

test and jumpfor all iterations > 1