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

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

Post on 14-Dec-2015

215 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

  • Slide 1

17 - Jumps & Branches Slide 2 The PC PC marks next location in Fetch, Decode, Execute cycle Slide 3 Jumps PC not directly addressable Modified by instructions j : Unconditional jump to label Slide 4 Using J Label converted to address by assembler: Slide 5 J Type J : Jump instruction 26 bit location Slide 6 J Type J : Jump instruction 26 bits devoted to address of label Slide 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 Slide 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 Slide 9 Delayed Branch MIPS delay's branches One extra op happens after branch/jump Must enable in MARS settings Slide 10 Delayed Branch One extra instruction always happens after jump Shown in green in simulator Slide 11 NOP NOP : no op Prevent unwanted work after branch Slide 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 Slide 13 If Assembly ifs are backwards High LevelAssembly if(i == j) { k = 1; } Branch to cont if $i != $j $k = 1 cont: . Slide 14 If If implemented with branch: Skip ahead if NOT doing if body Slide 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: Slide 16 If/Else implemented with branch: Branch to skip if body for else case If body jumps to skip else body If / Else Slide 17 ASM ABS Absolute value of A from memory Slide 18 18 Inequalities & Conditional Sets Slide 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 Slide 20 2 Register BLT is Cheating Two register inequalities are psuedo-ops Don't use this week Slide 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: Slide 22 If < If (i < j) k = 10; Slide 23 Real If Real C++Real compiler output: What is slt??? Slide 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 Real Code Counting Loop Compilers often move test to end of loop: Avoid separate test and jump for all iterations > 1