Conditional Instructions

Download Report

Transcript Conditional Instructions

COMPUTER ARCHITECTURE &
OPERATIONS I
Instructor: Yaohang Li
Review

Last Class




Binary Integers

Unsigned

Signed

Signed Extension
Representation of MIPS Instructions

R-format

I-format
This Class

Conditional Instructions

Assignment 4
Next Class

Conditional Instructions (Cont.)
Instructions for Making Decisions

High-level programming language
C/C++:
if … else … (conditional)
goto (unconditional)
for, while, until (loops)

Assembly Languages
MIPS:
beq (branch if equal)
bne (branch if not equal)
j (unconditional jump)
Conditional Operations

Branch to a labeled instruction if a
condition is true


beq rs, rt, L1


if (rs == rt) branch to instruction labeled L1;
bne rs, rt, L1


Otherwise, continue sequentially
if (rs != rt) branch to instruction labeled L1;
j L1

unconditional jump to instruction labeled L1
Compiling If Statements

C code:
if (i==j) f = g+h;
else f = g-h;
Compiling If Statements

C code:
if (i==j) f = g+h;
else f = g-h;


f ($s0), g ($s1), h($s2), i($s3), j($s4)
Compiled MIPS code:
bne
add
j
Else: sub
Exit: …
$s3, $s4, Else
$s0, $s1, $s2
Exit
$s0, $s1, $s2
Assembler calculates addresses
Compiling Loop Statements

C code:
while (save[i] == k) i = i+1;


i in $s3, k in $s5, address of save in $s6
Flowchart?
Compiling Loop Statements

C code:
while (save[i] == k) i = i+1;


i in $s3, k in $s5, address of save in $s6
Compiled MIPS code:
Loop: sll
add
lw
bne
addi
j
Exit: …
$t1,
$t1,
$t0,
$t0,
$s3,
Loop
$s3, 2
$t1, $s6
0($t1)
$s5, Exit
$s3, 1
Summary


Conditional Instructions

beq

bne

j
Converting a C Program to MIPS
What I want you to do


Review Chapter 2
Work on your assignment 4