Top Banner
17 - Jumps & Branches
30

17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Dec 14, 2015

Download

Documents

Anastasia Bruce
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

17 - Jumps & Branches

Page 2: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

The PC

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

Page 3: 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

Page 4: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Using J

• Label converted to address by assembler:

Page 5: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

J Type

• J : Jump instruction

– 26 bit location

Page 6: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

J Type

• J : Jump instruction

– 26 bits devoted to address of label

Page 7: 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

Page 8: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

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

Page 9: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Delayed Branch

• MIPS delay's branches– One extra op happens after branch/jump– Must enable in MARS settings

Page 10: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Delayed Branch

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

Page 11: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

NOP

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

Page 12: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

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

Page 13: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

If

• Assembly if’s are “backwards”

High Level Assembly

if(i == j) { k = 1;}…

Branch to cont if $i != $j $k = 1cont: ….

Page 14: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

If

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

Page 15: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

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:…

Page 16: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

• If/Else implemented with branch:– Branch to skip if body for else case– If body jumps to skip else body

If / Else

Page 17: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

ASM ABS

• Absolute value of A from memory

Page 18: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

18 – Inequalities & Conditional Sets

Page 19: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

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

Page 20: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

2 Register BLT is Cheating

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

Page 21: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

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:

Page 22: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

If <

• If (i < j) k = 10;

Page 23: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Real If

Real C++ Real compiler output:

What is slt???

Page 24: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

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

Page 25: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

If compiler style

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

Page 26: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Temp Check

• Set bit if 30 <= temp <= 55

Page 27: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Loop = jump backwards

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

Page 28: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Counting Loop

• While(i < 10)

Page 29: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Sum

• Sum 0…9

Page 30: 17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle.

Real Code Counting Loop

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

test and jumpfor all iterations > 1