Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "17 - Jumps & Branches. The PC PC marks next location in Fetch, Decode, Execute cycle."— Presentation transcript:

1 17 - Jumps & Branches

2 The PC PC marks next location in Fetch, Decode, Execute cycle

3 Jumps PC not directly addressable – Modified by instructions j : Unconditional jump to label

4 Using J Label converted to address by assembler:

5 J Type J : Jump instruction – 26 bit location

6 J Type J : Jump instruction – 26 bits devoted to address of label

7 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 Region1111 Region… … … … … … … … 0110 Region0101 Region0100 Region0011 Region0010 Region0001 Region0000

8 Decoding J Jump Instruction: 0x0810002 code address 0000 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

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

10 Delayed Branch One extra instruction always happens after jump – Shown in green in simulator

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

12 BEQ & BNE BEQ : Branch Equal BNE : Branch Not Equal beq $reg1, $reg2, label Compare 2 registers, possibly branch – Branch relative to current instruction – Range +/- ~2 19 instructions

13 If Assembly if’s are “backwards” High LevelAssembly if(i == j) { k = 1; } … Branch to cont if $i != $j $k = 1 cont: ….

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

15 If / Else If/Else implemented with branch: – Branch to skip if body for else case – If body jumps to skip else body High LevelAssembly if(i == j) { k = 1; } else { k = 2; } … Branch to else if $i != $j $k = 1 jump to endif else: $k = 2 endif: …

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

17 ASM ABS Absolute value of A from memory

18 18 – Inequalities & Conditional Sets

19 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

20 2 Register BLT is Cheating Two register inequalities are psuedo-ops – Don't use this week

21 If / Else Inequalities in assembly – Rewrite solved for 0 – Invert for skip logic High LevelAssembly if(i < j) { k = 1; } … Calculate $i - $j Branch to endif if >= 0 $k = 1 endif: …

22 If < If (i < j) k = 10;

23 Real If Real C++Real compiler output: What is slt???

24 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

25 If compiler style If (i < j) k = 10; //using slt

26 Temp Check Set bit if 30 <= temp <= 55

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

28 Counting Loop While(i < 10)

29 Sum Sum 0…9

30 Real Code Counting Loop Compilers often move test to end of loop: – Avoid separate test and jump for all iterations > 1


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

Similar presentations


Ads by Google