Recursion - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Recursion

Description:

Recursion – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 21
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • Chapter 10

2
Outline
  • Procedure Calls
  • Call/Return, Save/Restore, Stack Frames
  • Recursion
  • Same thing, but a bit trickier

3
Procedure Calls - Caller
  • What do we do to make a call
  • Pass parameters
  • Save some registers (caller-saved)
  • JSR
  • What do we do when call returns
  • Restore registers
  • Examine return value

4
Procedure Calls - Callee
  • When procedure is called
  • Save some registers (callee-saved)
  • Examine parameters
  • When procedure is about to return
  • Restore registers
  • Prepare return value
  • RET

5
Caler and Calee Saving
  • Caller-Saved Registers
  • When you call a procedure,it has every right to
    clobber these registers
  • If you need whats in a caller-saved register,
    save it before making a procedure call
  • Calee-saved registers
  • When you call a procedure,it must not clobber
    these registers
  • If you need to use these registers in a
    procedure, must save them first and restore them
    before returning

6
Calling Convention
  • To use or write a function, need to know
  • Where to put parameters
  • Where to put return address
  • Which registers are caller-saved
  • Which registers are callee-saved
  • Example
  • Parameters in R2, R3 (what if 3 params?)
  • Return value in R1
  • R0, R2, R3, R7 caller saved
  • R4, R5, R6 calee saved

7
Simple Procedure
  • Takes one parameter (call it X), returns -X
  • (X is in r2, return value in r1)
  • neg not r2,r2
  • add r2,r2,1 r2 is now -X
  • add r1,r2,0 Move return value to r1
  • ret

Note we clobbered r2 and didnt restore it. Why?
8
Not-so-simple Procedure
  • Takes two params (X and Y), returns X-Y
  • (Params X and Y are in r2, r3, return value in
    r1)
  • sub
  • Save r7, jsr will clobber it
  • Save r2, need to use it to call neg
  • add r2,r3,0 Put 1st param for neg in r2
  • jsr neg Puts Y in r1
  • Restore r2 before we use it
  • add r1,r2,r1 r1 is now X-Y
  • Restore r7, we need it to go back
  • ret

9
Real code for sub procedure
  • Takes two params (X and Y), returns X-Y
  • (Params X and Y are in r2, r3, return value in
    r1)
  • sub add r6,r6,-2 Make space
    for 2 words on the stack
  • str r7,r6,2 Save return address, neg
    clobbers it
  • str r2,r6,1 Save first param, need r2 to
    call neg
  • add r2,r3,0 Put Y as 1st param for neg
  • jsr neg Returns Y in r1
  • ldr r2,r6,1 Get first param of sub back
    into r2
  • add r1,r2,r1 r1 is now X-Y
  • ldr r7,r6,2 Restore return address
  • add r6,r6,2 Pop 2 words from stack
  • ret

10
Another procedure
  • Compares two zero-terminated ASCII strings, S1
    and S2
  • Parameters are addresses of S1 (in r2) and of
    S2 (in r3)
  • Returns positive number if S1 before S2,
  • 0 if equal, negative number if S2 before S1
  • strcmp ldr r1,r2,0 Load char from S1 into r1
  • add r2,r2,1 Point r2 to next char in S1
  • ldr r0,r3,0 Load car from S2 into r0
  • add r3,r3,1 Point r3 to next char in S2
  • not r1,r1 Subtract S1 char from S2 char
  • add r1,r1,1
  • add r1,r1,r0
  • brnp done If different, comparison is done
  • and r0,r0,r0 Why this?
  • brp strcmp If not end of S2, keep comparing
  • done ret Note that r1 is already set up

11
strcmp using sub?
  • Compares two zero-terminated ASCII strings, S1
    and S2
  • Parameters are addresses of S1 (in r2) and of
    S2 (in r3)
  • Returns a positive number if S1 before S2, 0 if
    equal, negative number if S2 before S1
  • strcmp add r6,r6,-4 Reserve 4 words on the
    stack
  • str r7,r6,4 Save return address, sub will
    clobber
  • loop str r2,r6,3 Save S1 pointer
  • str r3,r6,2 Save S2 pointer
  • ldr r2,r2,0 Get S1 char into r2
  • str r2,r6,1 Save S1 char
  • ldr r3,r3,0 Get S2 char into r3
  • jsr sub Call sub with the two chars
  • and r1,r1,r1 Set flags according to result of
    sub
  • brnp done If chars different, were done
  • ldr r0,r6,1 Get S1 char into r0
  • brz done If end of S1 (and S2), were done
  • ldr r2,r6,3 Restore S1 pointer into r2
  • add r2,r2,1 Advance S1 pointer to next char
  • ldr r3,r6,2 Restore S2 pointer into r3
  • add r3,r3,1 Advance S2 pointer to next char

12
Questions?
13
Recursion
  • What is it?
  • When a procedure calls itself
  • This sounds stupid Why do that?
  • Makes your life easier sometimes

14
strcmp with recursion
  • Compares two zero-terminated ASCII strings, S1
    and S2
  • Parameters are addresses of S1 (in r2) and of
    S2 (in r3)
  • Returns a positive number if S1 before S2, 0 if
    equal, negative number if S2 before S1
  • strcmp add r6,r6,-4 Reserve 4 words on the
    stack
  • str r7,r6,4 Save return address, sub will
    clobber
  • loop str r2,r6,3 Save S1 pointer
  • str r3,r6,2 Save S2 pointer
  • ldr r2,r2,0 Get S1 char into r2
  • str r2,r6,1 Save S1 char
  • ldr r3,r3,0 Get S2 char into r3
  • jsr sub Call sub with the two chars
  • and r1,r1,r1 Set flags according to result of
    sub
  • brnp done If chars different, were done
  • ldr r0,r6,1 Get S1 char into r0
  • brz done If end of S1 (and S2), were done
  • ldr r2,r6,3 Restore S1 pointer into r2
  • add r2,r2,1 Advance S1 pointer to next char
  • ldr r3,r6,2 Restore S2 pointer into r3
  • add r3,r3,1 Advance S2 pointer to next char

15
Factorial
  • x! is 1, if x is zero
  • x (x-1)!, otherwise
  • 0!1
  • 1! 10! 1
  • 2! 22! 2
  • 3! 32! 6
  • 4! 43! 24
  • 5! 54! 120
  • 6! 65! 720

16
Factorial in Java
  • int factorial(int x)
  • if(x0)
  • return 1
  • else
  • return xfactorial(x)

17
Recursion in LC-3
  • How is it done?
  • Like a function that calls another function
  • When writing LC-3 assebly code,dont think about
    it as recursion,just think about calling a
    function

18
Factorial
  • Takes one param (X), returns X!
  • (Param is in r2, return value in r1)
  • fact and r1,r2,r2 Determine flags for X
  • brp recurse If Xgt0, do recursion
  • add r1,r1,1 X is 0, r1 is now 1
  • ret
  • recurse add r6,r6,-2 Make two spaces on the
    stack
  • str r7,r6,2 Save return address, jsr
    clobbers it
  • str r2,r6,1 Save X, called function can
    clobber r2
  • add r2,r2,-1 X-1 in r2
  • jsr fact Returns (X-1)! in r1
  • ldr r3,r6,1 Get X into r3
  • add r2,r1,0 Move (X-1)! Into r2
  • jsr mul Now call mul with X and (X-1)!
  • ldr r7,r6,2 Restore return address
  • add r6,r6,2 Pop 2 words from stack
  • Note that r1 already holds X(X-1)! (result of
    mul)
  • ret

19
Do we really need recursion
  • No. Always can be avoided. But
  • Recursive code can be very easy to
    write,non-recursive equivalent code can be a
    mess
  • For strcmp and fact code is similar, but
  • Things like quicksort or mergesort
  • With recursion (pretty easy)
  • Without recursion (major pain)

20
Questions?
Write a Comment
User Comments (0)
About PowerShow.com