27
Loops Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 1 / 26

Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

LoopsLecture 9

Section 2.7

Robb T. Koether

Hampden-Sydney College

Fri, Sep 13, 2019

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 1 / 26

Page 2: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

1 Interative Structures in C

2 Loops in MIPSWhile Loops in MIPSImproving the While LoopDo-While Loops in MIPS

3 For Loops in MIPS

4 Assignment

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 2 / 26

Page 3: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Outline

1 Interative Structures in C

2 Loops in MIPSWhile Loops in MIPSImproving the While LoopDo-While Loops in MIPS

3 For Loops in MIPS

4 Assignment

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 3 / 26

Page 4: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

while Loops

while Loopswhile (condition){

action}

The while statement is the most basic iterative structure in C.It is identical to an if statement except that at the end of theaction, execution returns to the condition.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 4 / 26

Page 5: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

do-while Loops

do-while Loopsdo{

action} while (condition)

The do-while structure is identical to the while look except thatthe condition occurs at the end of the action rather than thebeginning.This means that the action will be executed at least once,whereas the action in a while loop may not be executed at all.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 5 / 26

Page 6: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

for Loops

for Loopfor (i = 0; i < limit; i++){

action}

Equivalent while Loopi = 0;while (i < limit){

actioni++;

}

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 6 / 26

Page 7: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Outline

1 Interative Structures in C

2 Loops in MIPSWhile Loops in MIPSImproving the While LoopDo-While Loops in MIPS

3 For Loops in MIPS

4 Assignment

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 7 / 26

Page 8: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Outline

1 Interative Structures in C

2 Loops in MIPSWhile Loops in MIPSImproving the While LoopDo-While Loops in MIPS

3 For Loops in MIPS

4 Assignment

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 8 / 26

Page 9: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

While Loops in MIPS

While Loops in MIPSloop_beg:

(branch instr) # Quit if cond is true(action) # Perform actionj loop_beg # Return to condition

loop_end:

The condition is the condition to quit the loop.Note that the form is the same as for a one-way decision structureexcept that a jump statement is added at the end of the action,which necessitates a label at the top of the loop.Note that the label loop_end comes after the jump.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 9 / 26

Page 10: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

While Loops in MIPS

Example (While Loop)Write a MIPS program that will read a series of integers until 0 is read,and then print the total.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 10 / 26

Page 11: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Outline

1 Interative Structures in C

2 Loops in MIPSWhile Loops in MIPSImproving the While LoopDo-While Loops in MIPS

3 For Loops in MIPS

4 Assignment

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 11 / 26

Page 12: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Better While Loops

One drawback of the previous code is that the loop includes twobranch statements (a conditional branch and an unconditionalbranch), each one executed on every iteration.This is unnecessarily inefficient.A better design is to place the conditional branch at the bottom ofthe loop.However, it needs to be tested before the first iteration of the loop!Precede the loop with a once-executed unconditional jump to thebottom of the loop.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 12 / 26

Page 13: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Better While Loops

Better While Loopsj loop_end # Jump to condition

loop_beg:(action) # Perform action

loop_end:(br to loop_beg) # Cont loop if cond is true

Note that the condition in the branch is now the condition to stay inthe loop.Also note that the label loop_end comes before the branch.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 13 / 26

Page 14: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Better While Loops

Example (Better While Loop)Rewrite the previous MIPS program with the conditional branch at thebottom of the loop.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 14 / 26

Page 15: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Outline

1 Interative Structures in C

2 Loops in MIPSWhile Loops in MIPSImproving the While LoopDo-While Loops in MIPS

3 For Loops in MIPS

4 Assignment

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 15 / 26

Page 16: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Do-While Loops in MIPS

Do-While Loops in MIPSloop_beg:

(action) # Perform action(branch to loop_beg) # Cont loop if true

The condition is placed after the action has been executed.This is identical to the improved while loop, except without theinitial jump to the bottom.There is only one label and only one branch. statement.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 16 / 26

Page 17: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Do-While Loops in MIPS

Do-While Loops in MIPS

j loop_end

loop_beg:(action) # Perform action

loop_end:

(branch to loop_beg) # Cont loop if true

Note that this could be converted to a standard while loop byadding an initial jump to the branch statement.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 17 / 26

Page 18: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Do-While Loops in MIPS

Do-While Loops in MIPSj loop_end

loop_beg:(action) # Perform action

loop_end:(branch to loop_beg) # Cont loop if true

Note that this could be converted to a standard while loop byadding an initial jump to the branch statement.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 17 / 26

Page 19: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Outline

1 Interative Structures in C

2 Loops in MIPSWhile Loops in MIPSImproving the While LoopDo-While Loops in MIPS

3 For Loops in MIPS

4 Assignment

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 18 / 26

Page 20: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

For Loops in MIPS

Equivalent while Loopi = 0;while (i < limit){

(action)i++;

}

Recall the while loop equivalent of a for loop.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 19 / 26

Page 21: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

For Loops in MIPS

Equivalent Loop in Unstructured Ci = 0;

loop_beg:if (i >= limit) goto loop_end;{

(action)i++;goto loop_beg;

}loop_end:

Recall the while loop equivalent of a for loop.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 20 / 26

Page 22: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

For Loops in MIPS

Equivalent Loop in Unstructured Ci = 0;goto loop_end;

loop_beg:{

(action)i++;

}loop_end:

if (i < limit) goto loop_beg;

The improved while loop.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 21 / 26

Page 23: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

For Loops in MIPS

For Loops in MIPSli $t0, 0 # i = 0li $s0, 10 # limit = 10j loop_end # Jump to test

loop_beg:(action) # Perform actionaddi $t0, $t0, 1 # i = i + 1

loop_end:blt $t0, $s0, loop_beg # Branch if i < 10

Assume that the counter is in $t0 and the limit is in $s0.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 22 / 26

Page 24: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

For Loops in MIPS

For Loops in MIPS (Count Down)li $t0, 10 # i = 10j loop_end # Jump to test

loop_beg:(action) # Perform actionaddi $t0, $t0, -1 # i = i - 1

loop_end:bgtz $t0, loop_beg # Continue if i > 0

Sometimes it is more convenient to count down instead of up in afor loop.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 23 / 26

Page 25: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

For Loops in MIPS

ExamplesRun count_to_10.asm example.Run nap_time.asm example.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 24 / 26

Page 26: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Outline

1 Interative Structures in C

2 Loops in MIPSWhile Loops in MIPSImproving the While LoopDo-While Loops in MIPS

3 For Loops in MIPS

4 Assignment

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 25 / 26

Page 27: Lecture 9 Section 2.7 Robb T. Koetherpeople.hsc.edu/faculty-staff/robbk/Coms361/Lectures... · Lecture 9 Section 2.7 Robb T. Koether Hampden-Sydney College Fri, Sep 13, 2019 Robb

Assignment

AssignmentRead Section 2.7.

Robb T. Koether (Hampden-Sydney College) Loops Fri, Sep 13, 2019 26 / 26