CS 232 - PowerPoint PPT Presentation

About This Presentation
Title:

CS 232

Description:

Definition: Recursion: If you still don't get it, see: – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 10
Provided by: Howar127
Category:
Tags:

less

Transcript and Presenter's Notes

Title: CS 232


1
CS 232
  • Definition
  • Recursion
  • If you still don't get it, see "Recursion".
  • Pick up the handout on your way in!!

2
Control flow in high-level languages
  • The instructions in a program usually execute one
    after another, but its often necessary to alter
    the normal control flow.
  • Conditional statements execute only if some test
    expression is true.
  • // Find the absolute value of a0
  • v0 a0
  • if (v0 lt 0)
  • v0 -v0 // This might not be executed
  • v1 v0 v0
  • Loops cause some statements to be executed many
    times.
  • // Sum the elements of a five-element array a0
  • v0 0
  • t0 0
  • while (t0 lt 5)
  • v0 v0 a0t0 // These statements will
  • t0 // be executed five times

3
MIPS control instructions
  • In section, we introduced some of MIPSs
    control-flow instructions
  • j // for unconditional jumps
  • bne and beq // for conditional branches
  • slt and slti // set if less than (w/ and w/o an
    immediate)
  • And how to implement loops
  • And branch pseudo instructions
  • blt t0, t1, L1 // Branch if t0 lt t1
  • ble t0, t1, L2 // Branch if t0 lt t1
  • bgt t0, t1, L3 // Branch if t0 gt t1
  • bge t0, t1, L4 // Branch if t0 gt t1
  • Today, well talk about if/else

4
Translating an if-then statement
  • We can use branch instructions to translate
    if-then statements into MIPS assembly code.
  • v0 a0
  • if (v0 lt 0)
  • v0 -v0
  • v1 v0 v0
  • Sometimes its easier to invert the original
    condition.
  • In this case, we changed continue if v0 lt 0 to
    skip if v0 gt 0.
  • This saves a few instructions in the resulting
    assembly code.

lw v0, 0(a0) bge v0, 0, skip sub v0,
zero, v0 skip add v1, v0, v0
5
Translating an if-then-else statements
  • If there is an else clause, it is the target of
    the conditional branch
  • And the then clause needs a jump over the else
    clause
  • // increase the magnitude of v0 by one
  • if (v0 lt 0) bge v0, 0, E
  • v0 -- sub v0, v0, 1
  • j L
  • else
  • v0 E add v0, v0, 1
  • v1 v0 L move v1, v0
  • Dealing with else-if code is similar, but the
    target of the first branch will be another if
    statement.

6
Recursion in MIPS
  • Last time we talked about function calls
  • Recursion is just a special case of function
    calls
  • Two parts
  • Base case no recursion, often doesnt call any
    functions
  • Recursive body calls itself

7
Recursion in MIPS (single function call)
  • Suggestions for simply implementing recursive
    function calls in MIPS
  • Handle the base case first
  • Before you allocate a stack frame if possible
  • Allocate stack frame
  • Save return address
  • Recursive Body
  • Save any registers needed after the call
  • Compute arguments
  • Call function
  • Restore any registers needed after the call
  • Consume return value (if any)
  • Deallocate stack frame and return.

8
Recursion in MIPS (multiple function calls
caller save)
  • Suggestions for simply implementing recursive
    function calls in MIPS
  • Handle the base case first
  • Before you allocate a stack frame if possible
  • Allocate stack frame
  • Save return address
  • For each function call (suggestion use s
    registers if gt1 call)
  • Save any registers needed after the call
  • Compute arguments
  • Call function
  • Restore any registers needed after the call
  • Consume return value (if any)
  • Deallocate stack frame and return.

9
Recursion in MIPS (multiple function calls
callee save)
  • Suggestions for simply implementing recursive
    function calls in MIPS
  • Handle the base case first
  • Before you allocate a stack frame if possible
  • Allocate stack frame
  • Save return address
  • Save enough s registers to hold your local
    variables
  • Copy your local variables to s registers
  • For each function call
  • Save any registers needed after the call
  • Compute arguments
  • Call function
  • Restore any registers needed after the call
  • Consume return value (if any)
  • Restore s registers
  • Deallocate stack frame and return.
Write a Comment
User Comments (0)
About PowerShow.com