ece232: hardware organization and design · basic blocks a basic block is a sequence of...

17
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Lecture 5: MIPs Decision-Making Instructions

Upload: others

Post on 07-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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

ECE232: Hardware Organization and Design

Lecture 5: MIPs Decision-Making Instructions

Page 2: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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

Page 3: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

ECE232: Decision Making Instructions 3

Example: Compiling C if-then-else

ExampleC Code if (i==j) f = g + h;

else f = g - h;

Assembly bne $s3, $s4, Elseadd $s0, $s1, $s2j Exit; # new: unconditional jump

Else: sub $s0, $s1, $s2Exit:

New Instruction: Unconditional jumpj LABEL # goto Label

Page 4: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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, 2add $t1, $t1, $s6lw $t0, 0($t1)bne $t0, $s5, Exitaddi $s3, $s3, 1j Loop

Exit: …

Page 5: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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:

Page 6: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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:

Page 7: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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:

Page 8: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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

Page 9: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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

Page 10: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

ECE232: Decision Making Instructions 10

Comparisons - What about <, , >, ?

bne, beq provide equality comparison

slt (set on less than) provides magnitude comparisonslt $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

Page 11: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

ECE232: Decision Making Instructions 11

switch statement

switch ( expr ) {

case const1: statement1

case const2: statement2

...

case constN: statementN

default: default-statement

}

Page 12: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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:

Page 13: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

ECE232: Decision Making Instructions 13

Switch Exampleswitch (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

Page 14: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

ECE232: Decision Making Instructions 14

Unconditional branches

Unconditional branch:

j L1

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

Page 15: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

ECE232: Decision Making Instructions 15

Loop: sll $t1, $s3, 2add $t1, $t1, $s6lw $t0, 0($t1)bne $t0, $s5, Exitaddi $s3, $s3, 1j 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

Page 16: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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

Page 17: ECE232: Hardware Organization and Design · Basic Blocks A basic block is a sequence of instructions with • No embedded branches (except at end) • No branch targets (except at

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