cmput 229 - computer organization and architecture i1 cmput229 - fall 2003 topic4: procedures...

76
CMPUT 229 - Computer Org anization and Architectu re I 1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral

Upload: robyn-oconnor

Post on 19-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

CMPUT Computer Organization and Architecture I3 Calling Itself int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); }

TRANSCRIPT

Page 1: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

1

CMPUT229 - Fall 2003

Topic4: ProceduresJosé Nelson Amaral

Page 2: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

2

Reading AssignmentChapter 3 of Hennessy and

Patterson: Sections 3.6 to 3.8. Pages A-26 to A-29 of Appendix A

Page 3: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

3

Calling Itselfint fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); }

Page 4: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

4

Procedure Call1 Place parameters in a place where the procedure

can access them.2 Transfer control to procedure.3 Acquire the storage resources needed for the

procedure.4 Perform the procedure’s task.5 Place the result value in a place where the calling

program can access it.6 Return control to the point of origin.

Pat.-Hen. pp. 132

Page 5: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

5

Registers Used for Procedure Calls

$a0-$a3: four argument registers in which to pass parameters$v0-$v1: two value registers in which to return values$ra: one return address register to return to the point of origin

Pat.-Hen. pp. 132

Page 6: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

6

Jumping and ReturningThe jump-and-link instruction (jal) is used to jump to a procedureand link the returning address.

jal ProcedureAddress

stores the value of PC+4 in the register $ra and transfer the execution to the ProcedureAddress

When the procedure completes execution, it will jump backto the calling point using:

jr $ra

Pat.-Hen. pp. 133

Page 7: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

7

A Procedure that Doesn’t Call Another Procedure

int leaf_example ( int g, int h, int i, int j) { int f;

f = (g + h) - (i + j); return f; }

Parameter Passing Conventiong $a0h $a1i $a2j $a3

Assumptionf $s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Pat.-Hen. pp. 134-135

Page 8: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

8

Example of lw execution

cont($t0)cont($t1)

$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

$sp

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory High Address

Low Address

$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 9: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

9

Example of lw execution

cont($t0)cont($t1)

$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $sa1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 10: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

10

Example of lw execution

cont($t1)

cont($t0)cont($t1)

$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 11: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

11

Example of lw execution

cont($t1)cont($t0)

cont($t0)cont($t1)

$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 12: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

12

Example of lw execution

cont($t1)cont($t0)cont($s0)cont($t0)

cont($t1)$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 13: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

13

Example of lw execution

cont($t1)cont($t0)cont($s0)g+h

cont($t1)$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 14: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

14

Example of lw execution

cont($t1)cont($t0)cont($s0)g+h

i+j$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 15: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

15

Example of lw execution

cont($t1)cont($t0)cont($s0)g+h

i+j$t0$t1

ghi

$a0$a1$a2

j$a3

$t0-$t1$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 16: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

16

Example of lw execution

cont($t1)cont($t0)cont($s0)g+h

i+j$t0$t1

ghi

$a0$a1$a2

j$a3

$t0-$t1$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$t0-$t1$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 17: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

17

Example of lw execution

cont($t1)cont($t0)cont($s0)g+h

i+j$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$t0-$t1$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 18: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

18

Example of lw execution

cont($t1)cont($t0)cont($s0)cont($t0)

i+j$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$t0-$t1$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 19: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

19

Example of lw execution

cont($t1)cont($t0)cont($s0)cont($t0)

cont($t1)$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

Memory

$sp

High Address

Low Address

$t0-$t1$v0

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Processor

Page 20: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

20

Example of lw execution

cont($t1)cont($t0)cont($s0)cont($t0)

cont($t1)$t0$t1

ghi

$a0$a1$a2

j$a3

cont($s0)$s0

MIPS assembly:sub $sp, $sp, 12 # Make room in stack for 3 more itemssw $t1, 8($sp) # push $t1 into stacksw $t0, 4($sp) # push $t0 into stacksw $s0, 0($sp) # push $s0 into stackadd $t0, $a0, $a1 # $t0 g + hadd $t1, $a2, $a3 # $t1 i + jsub $s0, $t0, $t1 # f $t0 - $t1add $v0, $s0, $zero # $v0 $s0lw $s0, 0($sp) # restore $s0lw $t0, 4($sp) # restore $t0lw $t1, 8($sp) # restore $t1add $sp, $sp, 12 # adjust stack pointer

MemoryProcessor High Address

Low Address

$t0-$t1$v0

$sp

Pat.-Hen. pp. 134-135

int leaf_example ( int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

Page 21: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

21

Register Saving Convention

To avoid saving and restoring registers that might not contain anyuseful value, MIPS adopts the following software convention:

Registers $t0-$t9 are not preserved by the callee on a procedure call.

Registers $s0-$s7 must be preserved on a procedure call.

We also say that registers $t0-$t9 are caller saved, and thatregisters $s0-s7 are callee saved. These names indicates whichprocedure (the caller or the callee) is responsible for preservingthe value stored in the register at the procedure boundary.

Page 22: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

22

Linking a Recursive Procedure

int fact ( int n ) { if (n < 1) return(1); else return(n * fact(n-1)); } MIPS assembly:

fact:sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

Page 23: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

23

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

$t0

$v0

3$a0

Processor

0x10002000$sp

$ra

$spMemory High Address

Low Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Pat.-Hen. pp. 136-138and A-26/A-29

Page 24: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

24

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

$t0

$v0

3$a0

Processor

0x10002000$sp

0x1000 4004$ra

$spMemory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 25: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

25

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

$t0

$v0

3$a0

Processor

0x1000 1FF8$sp

0x1000 4004$ra

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 26: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

26

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

$t0

$v0

3$a0

Processor

0x1000 1FF8$sp

0x1000 4004$ra

0x1000 4004

3$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 27: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

27

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

$v0

3$a0

Processor

0x1000 1FF8$sp

0x1000 4004$ra

0x1000 4004

3$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 28: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

28

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

3$a0

Processor

0x1000 1FF8$sp

0x1000 4004$ra

0x1000 4004

3$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 29: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

29

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

3$a0

Processor

0x1000 1FF8$sp

0x1000 4004$ra

0x1000 4004

3$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 30: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

30

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

2$a0

Processor

0x1000 1FF8$sp

0x1000 4004$ra

0x1000 4004

3$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 31: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

31

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

2$a0

Processor

0x1000 1FF8$sp

0x1000 6FEC$ra

0x1000 4004

3$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 32: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

32

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

2$a0

Processor

0x1000 1FF0$sp

0x1000 6FEC$ra

0x1000 4004

3

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 33: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

33

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

2$a0

Processor

0x1000 1FF0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 34: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

34

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

1$a0

Processor

0x1000 1FF0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 35: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

35

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

1$a0

Processor

0x1000 1FF0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 36: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

36

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

1$a0

Processor

0x1000 1FE8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 37: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

37

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

0$t0

$v0

1$a0

Processor

0x1000 1FE8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 38: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

38

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

$v0

0$a0

Processor

0x1000 1FE8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 39: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

39

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

$v0

0$a0

Processor

0x1000 1FE8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

Pat.-Hen. pp. 136-138and A-26/A-29

Page 40: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

40

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

$v0

0$a0

Processor

0x1000 1FE0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0$sp

Pat.-Hen. pp. 136-138and A-26/A-29

Page 41: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

41

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

$v0

0$a0

Processor

0x1000 1FE0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0$sp

Pat.-Hen. pp. 136-138and A-26/A-29

Page 42: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

42

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

0$a0

Processor

0x1000 1FE0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0$sp

Pat.-Hen. pp. 136-138and A-26/A-29

Page 43: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

43

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

0$a0

Processor

0x1000 1FE8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

$sp

Pat.-Hen. pp. 136-138and A-26/A-29

Page 44: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

44

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

0$a0

Processor

0x1000 1FE8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

$sp

Pat.-Hen. pp. 136-138and A-26/A-29

Page 45: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

45

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

1$a0

Processor

0x1000 1FE8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

$sp

Pat.-Hen. pp. 136-138and A-26/A-29

Page 46: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

46

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

1$a0

Processor

0x1000 1FE8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

$sp

Pat.-Hen. pp. 136-138and A-26/A-29

Page 47: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

47

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

1$a0

Processor

0x1000 1FF0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 48: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

48

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

1$a0

Processor

0x1000 1FF0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 49: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

49

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

1$a0

Processor

0x1000 1FF0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 50: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

50

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

2$a0

Processor

0x1000 1FF0$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 51: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

51

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

1$v0

2$a0

Processor

0x1000 1FF8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 52: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

52

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

2$v0

2$a0

Processor

0x1000 1FF8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 53: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

53

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

2$v0

2$a0

Processor

0x1000 1FF8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 54: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

54

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

2$v0

3$a0

Processor

0x1000 1FF8$sp

0x1000 6FEC$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 55: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

55

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

2$v0

3$a0

Processor

0x1000 1FF8$sp

0x1000 4004$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$sp

Memory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 56: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

56

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

2$v0

3$a0

Processor

0x1000 2000$sp

0x1000 4004$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$spMemory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 57: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

57

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

6$v0

3$a0

Processor

0x1000 2000$sp

0x1000 4004$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$spMemory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 58: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

58

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

6$v0

3$a0

Processor

0x1000 2000$sp

0x1000 4004$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$spMemory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 59: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

59

Example fact(3)

MIPS assembly:fact:

sub $sp, $sp, 8 # Make room in stack for 2 more itemssw $ra, 4($sp) # save the return addresssw $a0, 0($sp) # save the argument nslt $t0, $a0, 1 # if ($a0<1) then $t01 else $t0 0beq $t0, $zero, L1 # if n 1, go to L1add $v0, $zero, 1 # return 1add $sp, $sp, 8 # pop two items from the stackjr $ra # return to the instruction after jal

L1: sub $a0, $a0, 1 # subtract 1 from argumentjal fact: # call fact(n-1)lw $a0, 0($sp) # just returned from jal: restore nlw $ra, 4($sp) # restore the return addressadd $sp, $sp, 8 # pop two items from the stackmul $v0, $a0, $v0 # return n*fact(n-1)jr $ra # return to the caller

1$t0

6$v0

3$a0

Processor

0x1000 2000$sp

0x1000 4004$ra

0x1000 4004

3

0x1000 6FEC

2

0x1000 6FEC

1

$spMemory High Address

0x1000 3FFB add $a0,$zero,30x1000 4000 jal fact0x1000 4004 ….

Low Address

0x1000 6FEC

0

Pat.-Hen. pp. 136-138and A-26/A-29

Page 60: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

60

Other Data Stored in the Stack

$sp

High Address

Low Address

$fp

Before procedurecall

$sp

High Address

Low Address

$fp

After procedurecall

$sp

High Address

Low Address

$fp

During procedurecall

Saved argumentregisters

Saved return reg.

Savedregisters

Local arraysand structures

Patt.-Hen. pp 139

Page 61: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

61

ASCII CodeCode Char Code Char Code Char Code Char Code Char Code Char 32 Space 48 0 64 @ 80 P 96 ` 112 p 33 ! 49 1 65 A 81 Q 97 a 113 q 34 “ 50 2 66 B 82 R 98 b 114 r 35 # 51 3 67 C 83 S 99 c 115 s 36 $ 52 4 68 D 84 T 100 d 116 t 37 % 53 5 69 E 85 U 101 e 117 u 38 & 54 6 70 F 86 V 102 f 118 v 39 ‘ 55 7 71 G 87 W 103 g 119 w 40 ) 56 8 72 H 88 X 104 h 120 x 41 ( 57 9 73 I 89 Y 105 i 121 y 42 * 58 : 74 J 90 Z 106 j 122 z 43 + 59 ; 75 K 91 [ 107 k 123 { 44 , 60 < 76 L 92 \ 108 l 124 | 45 - 61 = 77 M 93 ] 109 m 125 } 46 . 62 > 78 N 94 ̂ 110 n 126 ~ 47 / 63 ? 79 O 95 _ 111 o 127 DEL

Patt.-Hen., pp 142

Page 62: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

62

A Procedure that Doesn’t Call Another Procedure

void strcpy ( char x[ ], char y[ ]) { int i;

i = 0; while ((x[i] = y[i]) != 0) i = i + 1; }

Parameter Passing Conventionbase of array x[ ] $a0base of array y[ ] $a1

Assumptioni $s0

MIPS assembly:strcpy

subi $sp, $sp, 4 # Room in stack for 1 more itemsw $s0, 0($sp) # save $s0 into stackadd $s0, $zero, $zero # i 0

L1: add $t1, $a1, $s0 # $t1 address of y[i]lb $t2, 0($t1) # $t2 y[i]add $t3, $a0, $s0 # $t3 address of x[i]sb $t2, 0($t3) # x[i] y[i]beq $t2, $zero, L2 # if y[i] = 0, goto L2add $s0, $s0, 1 # i i + 1j L1 # go to L1

L2: lw $s0, 0($sp) # restore $s0addi $sp, $sp, 4 # pop one word off stackjr $ra # return

Patt.-Hen., pp 143

Page 63: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

63

A Procedure that Doesn’t Call Another Procedure

Parameter Passing Conventionbase of array x[ ] $a0base of array y[ ] $a1

Assumptioni $s0

MIPS assembly:strcpy

subi $sp, $sp, 4 # Room in stack for 1 more itemsw $s0, 0($sp) # save $s0 into stackadd $s0, $zero, $zero # i 0

L1: add $t1, $a1, $s0 # $t1 address of y[i]lb $t2, 0($t1) # $t2 y[i]add $t3, $a0, $s0 # $t3 address of x[i]sb $t2, 0($t3) # x[i] y[i]beq $t2, $zero, L2 # if y[i] = 0, goto L2add $s0, $s0, 1 # i i + 1j L1 # go to L1

L2: lw $s0, 0($sp) # restore $s0addi $sp, $sp, 4 # pop one word off stackjr $ra # return

save $s0 in stackvoid strcpy ( char x[ ], char y[ ]) { int i;

i = 0; while ((x[i] = y[i]) != 0) i = i + 1; }

Patt.-Hen., pp 143

Page 64: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

64

A Procedure that Doesn’t Call Another Procedure

Parameter Passing Conventionbase of array x[ ] $a0base of array y[ ] $a1

Assumptioni $s0

MIPS assembly:strcpy

subi $sp, $sp, 4 # Room in stack for 1 more itemsw $s0, 0($sp) # save $s0 into stackadd $s0, $zero, $zero # i 0

L1: add $t1, $a1, $s0 # $t1 address of y[i]lb $t2, 0($t1) # $t2 y[i]add $t3, $a0, $s0 # $t3 address of x[i]sb $t2, 0($t3) # x[i] y[i]beq $t2, $zero, L2 # if y[i] = 0, goto L2add $s0, $s0, 1 # i i + 1j L1 # go to L1

L2: lw $s0, 0($sp) # restore $s0addi $sp, $sp, 4 # pop one word off stackjr $ra # return

save $s0 in stack

i 0

void strcpy ( char x[ ], char y[ ]) { int i;

i = 0; while ((x[i] = y[i]) != 0) i = i + 1; }

Patt.-Hen., pp 143

Page 65: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

65

A Procedure that Doesn’t Call Another Procedure

Parameter Passing Conventionbase of array x[ ] $a0base of array y[ ] $a1

Assumptioni $s0

MIPS assembly:strcpy

subi $sp, $sp, 4 # Room in stack for 1 more itemsw $s0, 0($sp) # save $s0 into stackadd $s0, $zero, $zero # i 0

L1: add $t1, $a1, $s0 # $t1 address of y[i]lb $t2, 0($t1) # $t2 y[i]add $t3, $a0, $s0 # $t3 address of x[i]sb $t2, 0($t3) # x[i] y[i]beq $t2, $zero, L2 # if y[i] = 0, goto L2add $s0, $s0, 1 # i i + 1j L1 # go to L1

L2: lw $s0, 0($sp) # restore $s0addi $sp, $sp, 4 # pop one word off stackjr $ra # return

save $s0 in stack

i 0

x[i] y[i]

void strcpy ( char x[ ], char y[ ]) { int i;

i = 0; while ((x[i] = y[i]) != 0) i = i + 1; }

Patt.-Hen., pp 143

Page 66: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

66

A Procedure that Doesn’t Call Another Procedure

Parameter Passing Conventionbase of array x[ ] $a0base of array y[ ] $a1

Assumptioni $s0

MIPS assembly:strcpy

subi $sp, $sp, 4 # Room in stack for 1 more itemsw $s0, 0($sp) # save $s0 into stackadd $s0, $zero, $zero # i 0

L1: add $t1, $a1, $s0 # $t1 address of y[i]lb $t2, 0($t1) # $t2 y[i]add $t3, $a0, $s0 # $t3 address of x[i]sb $t2, 0($t3) # x[i] y[i]beq $t2, $zero, L2 # if y[i] = 0, goto L2add $s0, $s0, 1 # i i + 1j L1 # go to L1

L2: lw $s0, 0($sp) # restore $s0addi $sp, $sp, 4 # pop one word off stackjr $ra # return

save $s0 in stack

y[i] = 0?

i 0

x[i] y[i]

void strcpy ( char x[ ], char y[ ]) { int i;

i = 0; while ((x[i] = y[i]) != 0) i = i + 1; }

Patt.-Hen., pp 143

Page 67: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

67

A Procedure that Doesn’t Call Another Procedure

Parameter Passing Conventionbase of array x[ ] $a0base of array y[ ] $a1

Assumptioni $s0

MIPS assembly:strcpy

subi $sp, $sp, 4 # Room in stack for 1 more itemsw $s0, 0($sp) # save $s0 into stackadd $s0, $zero, $zero # i 0

L1: add $t1, $a1, $s0 # $t1 address of y[i]lb $t2, 0($t1) # $t2 y[i]add $t3, $a0, $s0 # $t3 address of x[i]sb $t2, 0($t3) # x[i] y[i]beq $t2, $zero, L2 # if y[i] = 0, goto L2add $s0, $s0, 1 # i i + 1j L1 # go to L1

L2: lw $s0, 0($sp) # restore $s0addi $sp, $sp, 4 # pop one word off stackjr $ra # return

i i + 1

save $s0 in stack

no

y[i] = 0?

i 0

x[i] y[i]

void strcpy ( char x[ ], char y[ ]) { int i;

i = 0; while ((x[i] = y[i]) != 0) i = i + 1; }

Patt.-Hen., pp 143

Page 68: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

68

A Procedure that Doesn’t Call Another Procedure

Parameter Passing Conventionbase of array x[ ] $a0base of array y[ ] $a1

Assumptioni $s0

MIPS assembly:strcpy

subi $sp, $sp, 4 # Room in stack for 1 more itemsw $s0, 0($sp) # save $s0 into stackadd $s0, $zero, $zero # i 0

L1: add $t1, $a1, $s0 # $t1 address of y[i]lb $t2, 0($t1) # $t2 y[i]add $t3, $a0, $s0 # $t3 address of x[i]sb $t2, 0($t3) # x[i] y[i]beq $t2, $zero, L2 # if y[i] = 0, goto L2add $s0, $s0, 1 # i i + 1j L1 # go to L1

L2: lw $s0, 0($sp) # restore $s0addi $sp, $sp, 4 # pop one word off stackjr $ra # return

i i + 1

save $s0 in stack

no

y[i] = 0?yes

restore $s0

return

i 0

x[i] y[i]

void strcpy ( char x[ ], char y[ ]) { int i;

i = 0; while ((x[i] = y[i]) != 0) i = i + 1; }

Patt.-Hen., pp 143

Page 69: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

69

Immediate Operands

Often times we need to add a known constant to a register

addi $sp, $sp, 4 # $sp $sp + 4

5 29 29 4OpCode rs rt immediate

000101 11101 11101 0000 0000 0000 010031 26 15 020 1625 21

R29 = $sp

Patt.-Hen., pp 145

Page 70: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

70

Comparing with Non-Zero Constants

We also may need to compare the value stored in a register witha non-zero constant. For this we use the set-less-than immediateinstruction:

slti $t0, $s2, 10 # if $s2 < 10 then $t0 1 else $t0 0

Patt.-Hen., pp 146

Page 71: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

71

Operating with Bigger constants

addi $t0, $zero, <constant> # $t0 0 + <constant>

If we want to load a constant into register $t0, we can use the following addi instruction

Which is represented by the following binary representation

5 0 8 <constant>OpCode rs rt immediate

000101 00000 01000 sccc cccc cccc cccc31 26 15 020 1625 21

What do we do when we want to load a constant whose value needs more than 16 bits to be represented?

The constant will be sign extend, thus after this instruction, $t0 willcontain ssss ssss ssss ssss sccc cccc cccc cccc

Patt.-Hen., pp 147

Page 72: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

72

Load Upper ImmediateFor this situation MIPS has the load upper immediate instruction that loads a 16 bit constant into the upper half word of a register:

lui $t0, 255 # ($t0 is register R8)

Which is represented by the following binary representation

5 0 8 255OpCode rs rt immediate

000101 11101 01000 0000 0000 1111 111131 26 15 020 1625 21

After this lui instruction, the register $t0 constains:

0000 0000 1111 1111 0000 0000 0000 0000

upper half lower half

Patt.-Hen., pp 147

Page 73: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

73

Example

Write two MIPS instruction that will load the constant 641024+1 = 65536 +1 = 65537 into register $t0.

Page 74: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

74

Pseudo-InstructionA pseudo-instruction is an instruction that are accepted by theMIPS assembler, but that does not exist in the MIPS architecture.

Example: The MIPS architecture does not have an instruction to move the content of register $t1 into register $t0.

But the MIPS assembler accepts the following instruction:

move $t0, $t1 # $t0 $t1

Whenever the MIPS assembler encounters this instructionin an assembly program, it automatically transforms it into:

add $t0, $zero, $t1 # $t0 $t1

Patt.-Hen., pp 157

Page 75: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

CMPUT 229 - Computer Organization and Architecture I

75

Pseudo-InstructionOther examples include the bgt, bge, and ble branch instructions.

For instance the instruction:

blt $s5, $t2, Exit

Is converted into the following instructions:

slt $at, $s5, $t2bne $at, $zero, Exit

Where $at is an assembler temporary register reserved for theuse of the assembler for conversion of pseudo-instructions intoreal instructions.

Page 76: CMPUT 229 - Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José…

Patt-Hen., pp. 152