0% found this document useful (0 votes)
27 views8 pages

Nested Subroutine Call

1) The fact procedure uses recursion to calculate the factorial of a number n. It stores argument n and return address ra on the stack before recursively calling itself with n-1. 2) As recursion unfolds, the stack grows with multiple frames for each call, with a0 holding the current n and ra the return address. 3) On return, the fact procedure restores n from the stack, calculates the result as n * fact(n-1), restores ra and returns.

Uploaded by

doomachaley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views8 pages

Nested Subroutine Call

1) The fact procedure uses recursion to calculate the factorial of a number n. It stores argument n and return address ra on the stack before recursively calling itself with n-1. 2) As recursion unfolds, the stack grows with multiple frames for each call, with a0 holding the current n and ra the return address. 3) On return, the fact procedure restores n from the stack, calculates the result as n * fact(n-1), restores ra and returns.

Uploaded by

doomachaley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Nested subroutine call

Handling recursive procedure calls


Example. Compute factorial (n)
int fact (int n)
{
if (n < 1) return (1);
else return (n * fact(n-1))
}

(Plan) Put n in $a0. Result should be available in $v0.

{Structure of the fact procedure}


fact:

subi $sp, $sp, 8


sw

$ra, 4($sp) {why?}

sw

$a0, 0($sp)

$sp
a0
ra

$fp
OLD

NEW

$sp (current top of the stack)

calling program

procedure fact

4000

push ra

996

a0 = n (3)

4004

push a0

1000

jal fact (4000)

1004

if n<1 then {v0=1


Return to ra}

read fact(n) from v0

a0=n-1
jal fact (4000)

4024 v0=old a0* fact(n-1)


return to old ra

$sp
a0 = 1

n=3

a0

result

v0

ra = 4024
a0 = 2
ra= 4024
a0 = 3
ra = 1004

The growth of the stack as the recursion unfolds

Now test if n < 1 (i.e. n = 0). In that case return 0 to $v0.

slti $t0, $a0, 1

# if n 1 then goto L1

beq $t0, $zero, L1


addi $v0, $zero, 1

# return 1 to $v0

addi $sp, $sp, 8

# pop 2 items from stack

jr

# return

$ra

L1: addi $a0, $a0, -1


jal

fact

# decrement n
# call fact with (n 1)

Now, we need to compute n * fact (n-1)

lw

$a0, 0($sp)

# restore argument n

lw

$ra, 4($sp)

# restore return address

addi $sp, $sp, 8

# pop 2 items

mult $v0, $a0, $v0

# return n * fact(n-1)

jr

# return to caller

$ra

Run time environment of a MIPS program

Stack pointer

Low address
Temporary local
variables
Return address
Saved argument
registers beyond
a0-a3

Growth of stack

Frame
pointer
High address

A translation hierarchy

HLL program

COMPILER
Assembly language program

ASSEMBLER
Machine language module

LINKER

Library routine

Executable machine language program

LOADER
Memory

What are Assembler directives?


Instructions that are not executed, but they tell the
assembler about how to interpret something. Here are
some examples:

. text
{Program instructions here}

. data
{Data begins here}
. byte 84, 104, 101
. asciiz The quick brown fox
. float f1,. . . , fn
. word w1, . . . . wn
. space n {reserve n bytes of space}

How does an assembler work?


In a two-pass assembler

PASS 1: Symbol table generation


PASS 2: Code generation

Follow the example in the class.

You might also like