CONTROL INSTRUCTIONS Mahdi Nazm Bojnordi Assistant Professor - - PowerPoint PPT Presentation

control instructions
SMART_READER_LITE
LIVE PREVIEW

CONTROL INSTRUCTIONS Mahdi Nazm Bojnordi Assistant Professor - - PowerPoint PPT Presentation

CONTROL INSTRUCTIONS Mahdi Nazm Bojnordi Assistant Professor School of Computing University of Utah CS/ECE 3810: Computer Organization Overview Homework 3 due on Jan 31 st (midnight) HW 1 and 2 graded and will be posted soon This


slide-1
SLIDE 1

CONTROL INSTRUCTIONS

CS/ECE 3810: Computer Organization

Mahdi Nazm Bojnordi

Assistant Professor School of Computing University of Utah

slide-2
SLIDE 2

Overview

¨ Homework 3 due on Jan 31st (midnight)

¤ HW 1 and 2 graded and will be posted soon

¨ This lecture

¤ Control Instructions

slide-3
SLIDE 3

Recall: MIPS Instruction Format

¨ Instructions are represented as 32-bit numbers with

multiple fields

¨ MIPS Instruction Types

¤ R-type ¤ I-type ¤ J-type

  • p

rs rt rd shamt func 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

  • p

rs rt constant or address 6 bits 5 bits 5 bits 16 bits

  • p

target address 6 bits 26 bits

slide-4
SLIDE 4

Control Instructions

¨ We need decision making instructions to control the

execution flow

¤ Example C code

slide-5
SLIDE 5

Control Instructions

¨ We need decision making instructions to control the

execution flow

¤ Example C code array swap d

slide-6
SLIDE 6

Control Instructions

¨ We need decision making instructions to control the

execution flow

¤ Example C code array swap d ($t1) ($t0)

slide-7
SLIDE 7

Control Instructions

¨ We need decision making instructions to control the

execution flow

¤ Example C code array swap d ($t1) ($t0) lw $t1, 0($t0)

slide-8
SLIDE 8

Control Instructions

¨ We need decision making instructions to control the

execution flow

¤ Example C code array swap d ($t1) ($t0) lw $t1, 0($t0) lw $t2, 4($t0) sw $t2, 0($t0)

slide-9
SLIDE 9

Control Instructions

¨ We need decision making instructions to control the

execution flow

¤ Example C code array swap d ($t1) ($t0) lw $t1, 0($t0) lw $t2, 4($t0) sw $t2, 0($t0) sw $t1, 4($t0)

slide-10
SLIDE 10

Control Instructions

¨ We need decision making instructions to control the

execution flow

¤ Example C code lw $t1, 0($t0) lw $t2, 4($t0) sw $t2, 0($t0) sw $t1, 4($t0) How to handle loops and if statements?

slide-11
SLIDE 11

Control Instructions

¨ Determine which instruction to be executed next

¤ Conditional branch: Jump to instruction L1 if register1

equals register2

n beq

register1, register2, L1

¤ Unconditional branch: Jump to instruction L1

n J L1

slide-12
SLIDE 12

Control Instructions

¨ Determine which instruction to be executed next

¤ Conditional branch: Jump to instruction L1 if register1

equals register2

n beq

register1, register2, L1

n bne, slt (set-on-less-than), slti

¤ Unconditional branch: Jump to instruction L1

n J L1 n Jr $s0 (jump table; long jumps and case statements)

slide-13
SLIDE 13

Example: If-Else

¨ Convert to assembly

¤ if (i == j)

n f = g + h;

¤ else

n f = g – h;

slide-14
SLIDE 14

Example: If-Else

¨ Convert to assembly

¤ if (i == j)

n f = g + h;

¤ else

n f = g – h;

slide-15
SLIDE 15

Example: If-Else

¨ Convert to assembly

¤ if (i == j)

n f = g + h;

¤ else

n f = g – h;

bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit:

slide-16
SLIDE 16

Example: Do-While

¨ Convert to assembly

¤ do {

n sum = sum + i; n i = i – 1;

¤ }while (i != 0);

slide-17
SLIDE 17

Example: Do-While

¨ Convert to assembly

¤ do {

n sum = sum + i; n i = i – 1;

¤ }while (i != 0);

i==0? sum = sum + i; i = i – 1;

i != 0 Do:

slide-18
SLIDE 18

Example: Do-While

¨ Convert to assembly

¤ do {

n sum = sum + i; n i = i – 1;

¤ }while (i != 0); Do: add $s0, $s0, $t0 subi $t0, $t0, 1 bne $t0, $zero, Do

i==0? sum = sum + i; i = i – 1;

i != 0 Do:

slide-19
SLIDE 19

Example: For-Loop

¨ Convert to assembly

¤ for(i=0; i<10; i=i+1) {

n sum = sum + i;

¤ }

slide-20
SLIDE 20

Example: For-Loop

¨ Convert to assembly

¤ for(i=0; i<10; i=i+1) {

n sum = sum + i;

¤ } For:

i = 0; i<10? sum = sum + i; i = i + 1;

Exit:

slide-21
SLIDE 21

Example: For-Loop

¨ Convert to assembly

¤ for(i=0; i<10; i=i+1) {

n sum = sum + i;

¤ } For:

i = 0; i<10? sum = sum + i; i = i + 1;

Exit: addi $t0, $zero, 0 For: slti $t1, $t0, 10 beq $t1, $zero, Exit add $s0, $s0, $t0 addi $t0, $t0, 1 j For Exit: