ECE232: Hardware Organization and Design Lecture 5: MIPs - - PowerPoint PPT Presentation

ece232 hardware organization and design
SMART_READER_LITE
LIVE PREVIEW

ECE232: Hardware Organization and Design Lecture 5: MIPs - - PowerPoint PPT Presentation

ECE232: Hardware Organization and Design Lecture 5: MIPs Decision-Making Instructions Adapted from Computer Organization and Design , Patterson & Hennessy, UCB Overview Computers need to make decisions Microprocessors support


slide-1
SLIDE 1

Adapted from Computer Organization and Design, Patterson & Hennessy, UCB

ECE232: Hardware Organization and Design

Lecture 5: MIPs Decision-Making Instructions

slide-2
SLIDE 2

ECE232: Decision Making Instructions 2

Overview

  • Computers need to make decisions
  • Microprocessors support “conditional” operations
  • Operations typically called branch operations
  • Connections with common Java and C language constructs
  • If, else, switch, for loops, do loops
  • Conditional instructions have a specific format
slide-3
SLIDE 3

ECE232: Decision Making Instructions 3

Example: Compiling C if-then-else

  • Example

C Code if (i==j) f = g + h; else f = g - h; Assembly bne $s3, $s4, Else add $s0, $s1, $s2 j Exit; # new: unconditional jump Else: sub $s0, $s1, $s2 Exit:

  • New Instruction: Unconditional jump

j LABEL # goto Label

slide-4
SLIDE 4

ECE232: Decision Making Instructions 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: …

slide-5
SLIDE 5

ECE232: Decision Making Instructions 5

while statement

while ( condition ) { statements } while_start_label: # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, while_end_label # MIPS code for the statements j while_start_label while_end_label:

slide-6
SLIDE 6

ECE232: Decision Making Instructions 6

do-while statement

do { statements } while ( condition ); do_start_label: # MIPS code for the statements do_cond_label: # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, do_end_label j do_start_label do_end_label:

slide-7
SLIDE 7

ECE232: Decision Making Instructions 7

for loop

for ( init ; condition ; incr ) { statements } # MIPS code for the init expression for_start_label: # MIPS code for the condition expression

#(if condition satisfied set $t0=1)

beq $t0, $zero, for_end_label # MIPS code for the statements # MIPS code for the incr expression j for_start_label for_end_label:

slide-8
SLIDE 8

ECE232: Decision Making Instructions 8

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

slide-9
SLIDE 9

ECE232: Decision Making Instructions 9

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

slide-10
SLIDE 10

ECE232: Decision Making Instructions 10

Comparisons - What about <,  , >,  ?

  • bne, beq provide equality comparison
  • slt (set on less than) provides magnitude comparison

slt $t0,$s3,$s4 # if $s3<$s4 $t0=1; # else $t0=0;

  • Combine with bne or beq to branch:

slt $t0,$s3,$s4 # if (a<b) bne $t0,$zero,Less # goto Less;

  • Why not include a blt instruction in hardware?
  • Supporting in hardware would lower performance
  • Assembler provides this function if desired

(by generating the two instructions) condition register

slide-11
SLIDE 11

ECE232: Decision Making Instructions 11

switch statement

switch ( expr ) { case const1: statement1 case const2: statement2 ... case constN: statementN default: default-statement

}

slide-12
SLIDE 12

ECE232: Decision Making Instructions 12

MIPS code for switch statement

# MIPS code for $t0=expr beq $t0, const1, switch_label_1 beq $t0, const2, switch_label_2 ... beq $t0, constN, switch_label_N j switch_default switch_label_1: # MIPS code to compute statement1 switch_label_2: # MIPS code to compute statement2 ... switch_default: # MIPS code to compute default-statement switch_end_label:

slide-13
SLIDE 13

ECE232: Decision Making Instructions 13

Switch Example

switch (i) { //Assume i is in $s1 and j is in $s2; case 0: j = 3; break; case 1: j = 5; break; case 2: ; case 3: j = 11; break; case 4: j = 13; break; default: j = 17; } main: add $t0, $zero, $zero # $t0 = 0, temp. variable beq $t0, $s1, case0 # go to case0 addi $t0, $t0, 1 # $t0 = 1 beq $t0, $s1, case1 # go to case1 addi $t0, $t0, 1 # $t0 = 2 beq $t0, $s1, case2 # go to case2 addi $t0, $t0, 1 # $t0 = 3 beq $t0, $s1, case3 # go to case3 addi $t0, $t0, 1 # $t0 = 4 beq $t0, $s1, case4 # go to case4 j default # go to default case case0: addi $s2, $zero, 3 # j = 3 j finish # exit switch block

slide-14
SLIDE 14

ECE232: Decision Making Instructions 14

Unconditional branches

  • Unconditional branch:

j L1 jr $s5 (useful for large case statements and big jumps)

slide-15
SLIDE 15

ECE232: Decision Making Instructions 15

Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit:

Example 2

  • Convert to assembly:
  • while (save[i] == k)

i += 1;

  • i and k are in $s3 and $s5

and

  • base of array save[] is in

$s6

slide-16
SLIDE 16

ECE232: Decision Making Instructions 16

SPIM Example

switch (i) { //Assume i is in $s1 and j is in $s2;

case 0: j = 3; break; case 1: j = 5; break; case 2: ; case 3: j = 11; break; case 4: j = 13; break; default: j = 17;

}

main: add $t0, $zero, $zero # $t0 = 0, temp. variable used in switch beq $t0, $s1, case0 # go to case0 addi $t0, $t0, 1 # $t0 = 1 beq $t0, $s1, case1 # go to case1 addi $t0, $t0, 1 # $t0 = 2 beq $t0, $s1, case2 # go to case2 addi $t0, $t0, 1 # $t0 = 3 beq $t0, $s1, case3 # go to case3 addi $t0, $t0, 1 # $t0 = 4 beq $t0, $s1, case4 # go to case4 j deflt # go to default case case0: addi $s2, $zero, 3 # j = 3 j fin # exit switch block

slide-17
SLIDE 17

ECE232: Decision Making Instructions 17

Summary

  • Conditional operations affect program flow based on data

values

  • Microprocessor makes decision based on results of arithmetic

and logic operation

  • Know the difference between branch and jump
  • Useful for C/Java loops, if-else, and switch statements