Loops in C/Assembly

18
CSCI-365 Computer Organization Lecture Note : Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson & Hennessy, ©2005 Some slides and/or pictures in the following are adapted from: slides ©2008 UCB 4

description

4. Loops in C/Assembly. Compile into MIPS; A[] is an array of int s do { g = g + A [ i ]; i = i + j ; } while ( i != h ); Use this mapping: g , h , i , j , base of A $s1 , $s2 , $s3 , $s4 , $s5 (done in class). Loops in C/Assembly. - PowerPoint PPT Presentation

Transcript of Loops in C/Assembly

Page 1: Loops in C/Assembly

CSCI-365Computer Organization

Lecture

Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson & Hennessy, ©2005 Some slides and/or pictures in the following are adapted from: slides ©2008 UCB

4

Page 2: Loops in C/Assembly

Loops in C/Assembly

• Compile into MIPS; A[] is an array of ints

do {

g = g + A[i];

i = i + j;

} while (i != h);

• Use this mapping:

g, h, i, j, base of A $s1, $s2, $s3, $s4, $s5

(done in class)

Page 3: Loops in C/Assembly

Loops in C/Assembly

• There are three types of loops in C:– while

– do… while

– for

• Each can be rewritten as either of the other two, so the method used in the previous example can be applied to while and for loops as well

• Key Concept: Though there are multiple ways of writing a loop in MIPS, the key to decision making is conditional branch

Page 4: Loops in C/Assembly

Inequalities in MIPS

• Until now, we’ve only tested equalities (== and != in C). General programs need to test < and > as well

• Create a MIPS Inequality Instruction:

– “Set on Less Than”

– Syntax: slt reg1,reg2,reg3

– Meaning:

if (reg2 < reg3) reg1 = 1;

else reg1 = 0;

– “set” means “set to 1”, “reset” means “set to 0”

reg1 = (reg2 < reg3);

Same thing…

Page 5: Loops in C/Assembly

Register Zero

• The number zero (0), appears very often in code

• MIPS defines register zero ($0 or $zero) to always have the value 0; egadd $s0,$s1,$zero (in MIPS)

f = g (in C)

where MIPS registers $s0,$s1 are associated with C variables f, g

Page 6: Loops in C/Assembly

Immediates

• Immediates are numerical constants

• They appear often in code, so there are special instructions for them

• Add Immediate:addi $s0,$s1,10 (in MIPS)

f = g + 10 (in C)

where MIPS registers $s0,$s1 are associated with C variables f, g

• Syntax similar to add instruction, except that last argument is a number instead of a register

Page 7: Loops in C/Assembly

Immediates

• There is no Subtract Immediate in MIPS: Why?

• Limit types of operations that can be done to absolute minimum

– if an operation can be decomposed into a simpler operation, don’t include it

– addi …, -X = subi …, X => so no subi

• addi $s0,$s1,-10 (in MIPS)

f = g - 10 (in C)

where MIPS registers $s0,$s1 are associated with C variables f, g

Page 8: Loops in C/Assembly

Inequalities in MIPS

• How do we use this? Compile by hand:

if (g < h) goto Less; #g:$s0, h:$s1

• Answer: compiled MIPS code…

slt $t0,$s0,$s1 # $t0 = 1 if g<hbne $t0,$0,Less # goto Less # if $t0!=0 # (if (g<h)) Less:

• Branch if $t0 != 0 (g < h)

• Register $0 always contains the value 0, so bne and beq often use it for comparison after an slt instruction.

• A slt bne pair means if(… < …)goto…

Page 9: Loops in C/Assembly

Immediates in Inequalities

• There is also an immediate version of slt to test against constants: slti

– Helpful in for loops

if (g >= 1) goto Loop

Loop: . . .

slti $t0,$s0,1 # $t0 = 1 if # $s0<1 (g<1)beq $t0,$0,Loop # goto Loop # if $t0==0

# (if (g>=1))

C

MIPS

Page 10: Loops in C/Assembly

Inequalities in MIPS

• Now, we can implement <, but how do we implement >, ≤ and ≥ ?

• We could add 3 more instructions, but:– MIPS goal: Simpler is Better

• Can we implement ≤ in one or more instructions using just slt and the branches? (done in class)

• What about >? (done in class)

• What about ≥? (done in class)

Page 11: Loops in C/Assembly

 

Compiling if-then-else Statements

Show a sequence of MIPS instructions corresponding to:

if (i<=j) x = x+1; z = 1; else y = y–1; z = 2*z

// j: $s2, i:$s1, x:$t1, z:$t3, y:$t2,

Solution

Similar to the “if-then” statement, but we need instructions for the“else” part and a way of skipping the “else” part after the “then” part.

slt $t0,$s2,$s1 # j<i? (inverse condition) bne $t0,$zero,else # if j<i goto else part addi $t1,$t1,1 # begin then part: x = x+1 addi $t3,$zero,1 # z = 1 j endif # skip the else part

else: addi $t2,$t2,-1 # begin else part: y = y–1 add $t3,$t3,$t3 # z = z+z

endif:...

Page 12: Loops in C/Assembly

Example: The C Switch Statement

• Choose among four alternatives depending on whether k has the value 0, 1, 2 or 3. Compile this C code:

switch (k) { case 0: f=i+j; break; /* k=0 */ case 1: f=g+h; break; /* k=1 */ case 2: f=g–h; break; /* k=2 */ case 3: f=i–j; break; /* k=3 */}

Page 13: Loops in C/Assembly

Example: The C Switch Statement

• This is complicated, so simplify

• Rewrite it as a chain of if-else statements, which we already know how to compile:

if(k==0) f=i+j; else if(k==1) f=g+h; else if(k==2) f=g–h; else if(k==3) f=i–j;

• Use this mapping:

f:$s0, g:$s1, h:$s2,i:$s3, j:$s4, k:$s5

Page 14: Loops in C/Assembly

Example: The C Switch Statement

• Final compiled MIPS code:

bne $s5,$0,L1 # branch k!=0 add $s0,$s3,$s4 # k==0 so f=i+j j Exit # end of case so ExitL1: # $t0=k-1 # branch k!=1 # k==1 so f=g+h # end of case so ExitL2: # $t0=k-2 # branch k!=2 # k==2 so f=g-h # end of case so ExitL3: # $t0=k-3 # branch k!=3 #k==3 so f=i-j Exit:

Page 15: Loops in C/Assembly

Example: The C Switch Statement

• Final compiled MIPS code:

bne $s5,$0,L1 # branch k!=0 add $s0,$s3,$s4 # k==0 so f=i+j j Exit # end of case so ExitL1: addi $t0,$s5,-1 # $t0=k-1 bne $t0,$0,L2 # branch k!=1 add $s0,$s1,$s2 # k==1 so f=g+h j Exit # end of case so ExitL2: addi $t0,$s5,-2 # $t0=k-2 bne $t0,$0,L3 # branch k!=2 sub $s0,$s1,$s2 # k==2 so f=g-h j Exit # end of case so ExitL3: addi $t0,$s5,-3 # $t0=k-3 bne $t0,$0,Exit # branch k!=3 sub $s0,$s3,$s4 #k==3 so f=i-j Exit:

Page 16: Loops in C/Assembly

The For Example

for (i = 0; i < size; i += 1) array[i] = 0; // i:$t0, array starts at $a0, size:$a1

addi $t0,$zero,0 # i = 0loop1: sll $t1,$t0,2 # $t1 = i * 4 add $t2,$a0,$t1 # $t2 = &array[i] sw $zero, 0($t2) # array[i] = 0 addi $t0,$t0,1 # i = i + 1 slt $t3,$t0,$a1 # $t3 = (i < size) bne $t3,$zero,loop1 # if (…) goto loop1

Page 17: Loops in C/Assembly

 

Finding the Maximum Value in a List of Integers

Solution

Scan the list, holding the largest element identified thus far in $t0.

# initialize maximum to A[0] # initialize index i to 0

loop: # increment index i by 1 # if all elements examined,

quit # compute 2i in $t2 # compute 4i in $t2 # form address of A[i] in $t2 # load value of A[i] into $t3 # maximum < A[i]? # if not, repeat with no

change # if so, A[i] is the new

maximum # change completed; now

repeat done: ... # continuation of the program

List A is stored in memory beginning at the address given in $s1. List length is given in $s2. Find the largest integer in the list and copy it into $t0.

Page 18: Loops in C/Assembly

 

Finding the Maximum Value in a List of Integers

Solution

Scan the list, holding the largest element identified thus far in $t0.

lw $t0,0($s1) # initialize maximum to A[0]addi $t1,$zero,0 # initialize index i to 0

loop: add $t1,$t1,1 # increment index i by 1beq $t1,$s2,done # if all elements examined,

quitadd $t2,$t1,$t1 # compute 2i in $t2add $t2,$t2,$t2 # compute 4i in $t2 add $t2,$t2,$s1 # form address of A[i] in

$t2 lw $t3,0($t2) # load value of A[i] into

$t3slt $t4,$t0,$t3 # maximum < A[i]?beq $t4,$zero,loop # if not, repeat with no

changeaddi $t0,$t3,0 # if so, A[i] is the new

maximum j loop # change completed; now

repeat done: ... # continuation of the program

List A is stored in memory beginning at the address given in $s1. List length is given in $s2. Find the largest integer in the list and copy it into $t0.