Presentation is loading. Please wait.

Presentation is loading. Please wait.

DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.

Similar presentations


Presentation on theme: "DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations."— Presentation transcript:

1 DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations

2 Chapter 2 — Instructions: Language of the Computer — 2 Conditional Operations Branch to a labeled instruction if a condition is true  Otherwise, continue sequentially beq rs, rt, L1  if (rs == rt) branch to instruction labeled L1; bne rs, rt, L1  if (rs != rt) branch to instruction labeled L1; j L1  unconditional jump to instruction labeled L1 §2.7 Instructions for Making Decisions

3 Chapter 2 — Instructions: Language of the Computer — 3 Compiling If Statements C code: if (i==j) f = g+h; else f = g-h;  f, g, h, i, j in $s0, $s1, $s2, $s3, $s4 Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: … Assembler calculates addresses

4 Chapter 2 — Instructions: Language of the Computer — 4 Compiling Loop Statements C code: while (save[i] == k) i += 1;  i in $s3, k in $s5, address of save in $s6 Compiled MIPS code: Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: … #$t1=4*i #$t1=&save[i] #$t0=save[i] #check save[i]==k #i++

5 Chapter 2 — Instructions: Language of the Computer — 5 Basic Blocks A basic block is a sequence of instructions with  No embedded branches (except at end)  No branch targets (except at beginning) A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks

6 MIPS Control Flow Instructions MIPS conditional branch instructions  Ex:  Instruction Format (I format): bne $s0, $s1, L1 #go to L1 if $s0≠$s1 beq $s0, $s1, L1 #go to L1 if $s0=$s1 If (i==j) h=i+j bne $s0, $s1, L1 add $s3, $s0, $s1 L1: … 0x05161716 bit offset

7 Specifying Branch Destinations Use a register (like in lw and sw) added to the 16-bit offset  Which register? Instruction Address Register (the PC)  Its use is automatically implied by instruction  PC gets updated (PC+4) during the fetch cycle so that it holds the address of the next instruction  Limits the branch distance to -2 15 to 2 15 -1 (word) instructions from the (instruction after the) branch instruction. But most branches are local anyway  -2 15 to 2 15 -1 (word) => -2 17 to 2 17 -1 (byte)

8 Chapter 2 — Instructions: Language of the Computer — 8 More Conditional Operations Set result to 1 if a condition is true  Otherwise, set to 0 slt rd, rs, rt  if (rs < rt) rd = 1; else rd = 0; slti rt, rs, constant  if (rs < constant) rt = 1; else rt = 0; Use in combination with beq, bne slt $t0, $s1, $s2 # if ($s1 < $s2) bne $t0, $zero, L # branch to L

9 More Branch Instructions Can use slt, beq, bne and the fixed value of 0 in regitster $zero to create other conditions  Less than  Less than or equal to  Greater than  Greater than or equal to Such branches are included in the instruction set as pseudo-instructions. These are recognized and expanded) by the assembler  Its why the assembler needs a reserved register ($at) blt $s1, $s2, Label slt $at, $s1, $s2 #$at set to 1 if bne $at, $zero, Label #$s1<$s2 ble $s1, $s2, Label bgt $s1, $s2, Label bge $s1, $s2, Label

10 Chapter 2 — Instructions: Language of the Computer — 10 Signed vs. Unsigned Signed comparison: slt, slti Unsigned comparison: sltu, sltui Example  $s0 = 1111 1111 1111 1111 1111 1111 1111 1111  $s1 = 0000 0000 0000 0000 0000 0000 0000 0001  slt $t0, $s0, $s1 # signed  –1 < +1  $t0 = 1  sltu $t0, $s0, $s1 # unsigned  +4,294,967,295 > +1  $t0 = 0

11 Chapter 2 — Instructions: Language of the Computer — 11 Branch Instruction Design Why not blt, bge, etc? Hardware for <, ≥, … slower than =, ≠  Combining with branch involves more work per instruction, requiring a slower clock  All instructions penalized! beq and bne are the common case This is a good design compromise

12 Bounds Check Shortcut Treating signed numbers as if they were unsigned gives a low cost way of checking if 0≤x<y (index out of bounds for arrays) The key is that negative integers in 2’s complement look like large numbers in unsigned notation. Thus, an unsigned comparison of x<y also checks if x is negative as well as if x is less than y. sltu $t0, $s1, $s2 #$t0 set to 1 if beq $t0, $zero, IOOB #go to IOOB if $t0=0 #$s1 > $s2 (max) #or $s1 <0 (min)


Download ppt "DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations."

Similar presentations


Ads by Google