Control Flow - Computer Systems - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Computer Systems which includes Writing to Cache, Memory Access, Simple Direct-Mapped Cache, Inconsistent Memory, Write-Through Caches, Write-Back Caches, Finishing Write Back, Write Misses etc.Key important points are: Control Flow, Decisions

Typology: Slides

2012/2013

Uploaded on 03/27/2013

agarkar
agarkar 🇮🇳

4.3

(26)

372 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 410
Computer Systems
Lt
5
CtlFl Dii &L
L
ec
t
ure
5
C
on
t
ro
l
Fl
ow:
D
ec
i
s
i
ons
&
L
oops
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Control Flow - Computer Systems - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

CSE 410Computer SystemsL^ t^5 C

t^ l Fl^ D^ i i

& L

Lecture 5 – Control Flow: Decisions & Loops

Control flow in high

-level languages

Control flow in high level languages•^ The instructions in a program usually execute one after another,b^ i ’^ f^

l^ h^ l^ l fl but it’s often necessary to alter the normal control flow.• Conditional statements execute only if some test expression istrue.^ // Find the absolute value of a0v0 = a0;if (v0 < 0)v0 = -v0;^

// This might not be executed v1 = v0 + v0;^0 0; • Loops cause some statements to be executed many times. //^ Sum the elements of^

a five-element array^ a //^

y v0 = 0;t0 = 0;while (t0 < 5) {v0 = v0 + a0[t0];^

// These statements willt0++; // be executed five times }

MIPS control instructionsMIPS^ control instructions•^ MIPS’s control-flow instructions:j^

// for unconditional jumpsbne and beq // for conditional branchesslt and slti // set if less than (reg. & immediate)

-^ Usage:j there^

// next instruction at label “there”

beq $t0 $t1 xyz^

// if $t0==$t1 next instr at “xyz”

beq $t0, $t1, xyz^

// if $t0==$t1, next instr. at

xyz

slt^ $t3, $a1, $s0 // if $a1<$s0, $t3=1 else $t3=

Pseudo-branches• The MIPS processor only supports two branchinstructions, beq and bne, but to simplify your life theassembler provides the following other branches:^ blt^ $t0,^ $t1,

L1^ //^ Branch^

if^ $t0^ <^ $t ble^ $t0,^ $t1,^ L

//^ Branch^ if^

$t0^ <=^ $t bgt^ $t0,^ $t1,^ L

//^ Branch^ if^

$t0^ >^ $t b^ $^0 $^1

//^ h^ if^

$^0 $^1 bge^ $t0,^ $t1,^ L

//^ Branch^ if^

$t0^ >=^ $t

There are also immediate versions of these branches• There are also immediate versions of these branches,where the second source is a constant instead of aregister

Translating an if

-then statement

Translating an if then statement• We can use branch instructions to translate if-then^ statements into MIPS assembly code.^ v0^ =^ a0;^

move^ $v0^ $a if^ (^0 0)^

b^ $ 0^ $0^ L^ b^ l if^ (v0^ <^ 0)^

bge^ $v0,^ $0,^ Label v0^ =^ -v0;^

sub^ $v0,^ 0,^ $v v1^ =^ v0^ +^ v0;^

Label:^ add^ $v1,^ $v0,

$v

-^ Sometimes it’s easier to

invert^ the original condition.

—^ In this case, we changed “continue if

v0 < 0” to “skip if

v0 >= 0”v0 >= 0^. — This saves a few instructions in the resultingassembly code.

Translating an if-then-else statementg • If there is an else clause, it is the target of theconditional branch^ —^ And the then clause needs a jump over the elseclauseclause^ // increase the magnitude of v0 by oneif (v0 < 0)^

bge^ $v0, $0, E v0^ ;^

sub^ $v0^ $v0^1 v0 --;^

sub^ $v0, $v0, 1j^ L elsev0 ++;^

E:^ add^ $v0, $v0, 1 v1^ = v0;^

L:^ move $v1, $v v1^ v0;^

L:^ move $v1,^ $v

—^ Drawing the control-flow graph can help you out.

Example: for (

i=0; i<10; i++) s[i] = i;

Example: for (i 0; i<10; i++) s[i]

i;

# assume: $s0=addr(s), and let $t1=i

move^ $t1,$zero

# i = 0

loop:sll^ $t0,$t1,

# t0 = i*

addu^ $t0,$s0,$t

# t0 = addr(s[i])$ $ #^ i^ i

sw^ $t1,0($t0)

# s[i] = i

addu^ $t1,$t1,

# i++

slti^ $t0 $t1 10

# if (i<10) $t0=

slti^ $t0,$t1,

#^ if (i<10) $t0=

bnez^ $t0,loop

# loop if (i<10)

Example: Count Characters in StringExample:^ Count Characters in String•^ Assume: $a0 points to a string of ASCII characters with0x00 indicating the end•^ Set $v0 = # of characters in string (exclude 0x00)^ li^ $v0,

0 #^ length

=^0

loop:lb^ $t0^ 0($a0)

#^ load^ char lb^ $t0,^ 0($a0)

#^ load^ char beq^ $t0,^ $zero,

done #^ done^ if^

==^ 0x addi^ $v0,^ $v0,

1 #^ length++ addi^ $a0,^ $a0,

1 #^ next^ char j^ loop^

#^ repeat done:^ #^ $v0^ =^

string^ length^ here$ g^ g