CSC 230: Functions in MIPS32
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 1
Assembly Language intro
• Reminder of control-flow so far • Function calls (simple)
• The runtime stack
• Return values, parameters
• Call-by-value vs. call-by-reference • Registers vs. stack for parameters • Stack frames
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 2
Control flow so far
• Conditional branch instruction: – beq, bne
– Useofsltforcomparingregisters
• Unconditional branch possible using beq with the same register repeated
# some code …
addi $8, $0, 1
add $9, $0, $0
addi $10, $0, 12
loop12times:
add $9, $9, $8
# counter
# will eventually hold sum of first 12 integers
# termination for counter value
beq $8, $10, stop
addi $8, $8, 1
beq $0, $0, loop12times
# possibly other code
stop:
# Do something with sum available in $9
University of Victoria
CSC 230: Computer Architecture and Assembly Language
Department of Computer Science
Functions in MIPS32: Slide 3
Translating “if” structures
• Note:
– Many, many more control-flow possibilities are possible in assembler…
– … yet you understand the Java “if” statement …
– … and may want to see a more straightforward MIPS32 translation
• Whatfollowsissimplyoneapproach
– Compilers for languages such as C have templates that are used to generate needed assembly for important control-flow structures
– In essence we see here an approximation of this idea
– Rather than use C, however, we’ll use a Java-like syntax in our examples.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 4
Translating “if” structures
aà$8 bà$9 temp à $10
if (a < b) {
b++; }
slt $10, $8, $9
beq $10, $0, skip
addi $9, $9, 1
skip:
Note the way true/false appears
to be swapped around from Java
code to assembly:
University of Victoria
Department of Computer Science
if !(a < b) then skip true block
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 5
Translating “if” structures
aà$8 bà$9 temp à $10
if (a > b) {
a–; }
slt $10, $9, $8
beq $10, $0, skip
addi $8, $8, -1
skip:
Note the inverted logic that we use for
assembly:
CSC 230: Computer A
University of Victoria
if !(b < a) then skip true block
Department of Computer Science
rchitecture and Assembly Language Functions in MIPS32: Slide 6
Translating “if” structures
mà$11 nà$12 temp à $10
if (m >= n) {
m -= n; }
Note that (m >= n) is the
same thing as !(m < n)
slt $10, $11, $12
beq $10, $0, skip
skip:sub $11, $11, $12
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 7
Translating “if” structures
mà$11 nà$12
if (m == 0) {
n = 0; }
bne $11, $12, skip
add $12, $0, $0
skip:
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 8
Translating an “if-else” structure
aà$8 bà$9 temp à $10
if (a >= b) {
b++;
} else { } a++;
a -= b;
Remember (a >= b) is the
same thing as !(a < b)
slt $10, $8, $9
beq $10, $0, LABEL_Y
addi $9, $9, 1
beq $0, $0, LABEL_Z
LABEL_Y:
addi $8, $8, 1
LABEL_Z:
University of Victoria
Department of Computer Science
sub $8, $8, $9
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 9
Control flow so far
• A branch instructions has a significant constraint on the destination address
– Destination’slocationinprogrammemorymustbenomore than 2^15 instructions away relative to (i.e., before or after) the current instruction
– Recall:immediatefieldofbranchinstructionholds16bits two’s complement number.
• This constraint becomes a problem when destination is more distant in program memory
– Recall:areafortextofaMIPS32programisfrom0x0440000 to 0x0FFFFFF ...
– ...but16-bitsisfrom-0x0800to+0x7FF...
– ...whichmeanssomebranchtargetsinaprogrammightbe unreachable without repeated “hopping”
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 10
MIPS J-format instruction
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 11
op: 2 (for j), 3 (for jal)
address: 26-bit word address of destination instruction
j some_label # Assumes "some_label" is 0x0040000c
Caution!
• Not all jump instructions are J format instructions!
– jr: jump return is an R-format instruction!
– We’ll see this in action in a few slides time...
– ... but always be careful with what you think the instruction mnemonic is telling you!
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 12
Control flow beyond branches
• In higher-level languages we often use this very important form of control flow:
– call some “function” f() ...
– ... which means computer transfers control to the first instruction
in f()
– ... then have the computer execute the code corresponding to
function f() ...
– ... and when the code in this function is completed, control
resumes at the instruction right after the original call to f()
• Function-call semantics in a high-level language are very rich...
– ... input parameters ...
– ... return types ...
– but we’ll initially ignore all this in MIPS32!
• More strictly: MIPS32 often refers to procedures rather than functions
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 13
Simple example
• One of the challenges of learning to use assembly-language procedures is that:
– they are both more rudimentary...
– ... and more complicated ..
– than programming-language functions
• Let us look at a “before-and-after”
– Problem: multiply the contents of $8 with that in $9, leaving the result in $10
– Before: code without procedure
– After: code with procedure, but with no
“parameters” and with no “return value”
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 14
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 15
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 16
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 17
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 18
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 19
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 20
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 21
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 22
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 23
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 24
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 25
We now skip ahead to the end
of the loop (i.e., finished the 20th
addition of $8 into $10).
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 26
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 27
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 28
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 29
jal and jr
• Before branching to a new program-memory location indicated by the jal instruction:
– ...theprocessorsavesthecurrentPCvalueintoregister$31 the stack ...
– ...andthisvalueistheprogram-memorylocation following the jal instruction itself.
• The address stored in $31 is called the return address
• After the code in the called function is complete...
– ...thejr$31instruction(for“jumpandreturn”)isexecuted ...
– ...whichoverwritesthePCwiththevaluein$31
• Both jal and jr modify the PC...
– ...andthereforemodifythenormalsequentialflow-of- control
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 30
Now let's organize the same thing, but this time
University of Victoria
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 31
around a procedure.
Department of Computer Science
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 32
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 33
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 34
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 35
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 36
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 37
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 38
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 39
We now skip ahead to the end
of the loop (i.e., finished the 20th
University of Victoria
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 40
addition of $8 into $10).
Department of Computer Science
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 41
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 42
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 43
State of processor is shown before the
University of Victoria
CSC 230: Computer Architecture and Assembly Language
execute/writeback phase of the current instruction.
Department of Computer Science
Functions in MIPS32: Slide 44
Typical MIPS core components
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 45
.data
thing1: .word 123
thing2: .word 345
Translating an “if-else” structure
thing3: .word 57
After the code finishes,
.texatdd $8, $0, $0
jal do_something_A jal do_something_B jal do_something_C addi $2, $0, 10 syscall
do_something_A:
la $9, thing1
lw $10, 0($9)
add $8, $8, $10
jr $31
# Code continues on right...
what is the values that
is left in $8?
University of Victoria Department of Computer Science
# Code continues here...
do_something_B:
la $9, thing3
lw $10, 0($9)
add $8, $10, $0
jr $31
do_something_C:
la $9, thing2
lw $10, 0($9)
add $8, $8, $10
jr $31
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 46
Suppose...
• ... we want to turn off the rightmost contiguous sequence of 1-bits in a 32-bit value
• Perhaps easier to see this first with 8-bit values
– 0b01011000 à 0b0100000 – 0b10011011 à 0b10011000 – 0b00111111 à 0b0000000
• Formula:((x|(x-1))+1)&x
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 47
0b10011011
((x | (x - 1)) + 1) & x
x = 0b10011001
y=x-1 =0b10011000 z = (x | y) = 0b10011001 w=z+1 =0b10011010
answerw=& x = 0b10011000
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 48
Suppose...
• ...weneedtoperformtheseatcertainspotsinour code ...
• ...suchthatplacingthecodeinaloopdoesn'treally benefit us.
• Wecould:
– Copy and paste the code for the task where it is needed...
– ... but this is tedious and error prone.
• Betterapproach:
– Put the code in one place, and cause control-flow to transfer to the place and transfer back from that place.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 49
Example (w/o procedure)
# ... more operations
# Apply operation to $19, place # result back into $19, use
# $8 as a scratch register
addi $8, $19, -1 or $8, $8, $19 addi $8, $8, 1 and $8, $8, $19 add $19, $0, $8
# ... and now finish program
# ((x | (x - 1)) + 1) & x
.tex#t ... some operations
# Apply operation to $20,
# and place result back into
# $20; use $8 as a scratch register
addi $8, $20, -1 or $8, $8, $20 addi $8, $8, 1 and $8, $8, $20 add $20, $0, $8
# ... more operations
# Apply operation to $16, place # result back into $16, use
# $9 as a scratch register
addi $9, $16, -1 or $9, $9, $16 addi $9, $9, 1
and $9, $9, $16
University of Victoria
Department of Computer Science
add $16, $0, $9
This copying-and-pasting is not the
best way to program, and in fact, could
result in some nasty and hard-to-find
CSC 230: Computer Architecture and Assembly Language bugs. Better to placFuenctiohnis isn MiIPnS32a: Slide 50
procedure!
Data flowing into, out of, procedure
• For this to work, the code for the procedure must make some specific assumptions.
– In what register will the value for processing be stored?
– In what register will the result from processing be stored?
• Even more important:
– Code that calls the procedure must also follow the same assumptions.
– The value going into the procedure can be called a parameter, but the way we do this looks very different from a programming-language function
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 51
Previous example, but now with the algorithm placed into a procedure.
Example (w/ procedure)
.tex#t ... some operations
# Apply operation to $20.
# Procedure assumes parameter for # operation is in $4, with
# returned value placed in $2.
# Make sure to overwrite $20
# with result.
add $4, $0, $20
jal reset_rightmost_set_contiguous
add $20, $0, $2
# ... more operations
# Apply operation to $16, place # result back into $16.
add $4, $0, $16
jal reset_rightmost_set_contiguous
add $16, $0, $2
University of Victoria Department of Computer Science
# more operations...
# Apply operation to $19, place # result back into $19.
add $4, $0, $19
jal reset_rightmost_set_contiguous add $19, $0, $2
# Procedure assumes $8 can be used # as a scratch register at all
# times.
# And now finish program
reset_rightmost_set_contiguous:
addi $8, $4, -1 or $8, $8, $4 addi $8, $8, 1 and $8, $8, $4 add $2, $0, $8 jr $31
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 52
Important convention on registers
• Recall: MARS shows us two names for the same register – bynumber
– byname
• The names reflect a programming convention
created by the MIPS32 designers
– Idea:Ifalltoolvendorsfollowthesameconventionswith respect to purpose chosen for each register...
– ...thendifferenttoolsfromdifferentvendorswillcooperate.
– (Tool:assembler,compiler,etc.)
• These conventions only make sense once we start using procedures in our code
– Thatis,thedifferent“purposes”relatetotheroleofregisters when procedures are called and procedures are executed.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 53
Important convention on registers
• We have already seen three of these conventions:
– $0 always means zero (and cannot be changed); name is $zero
– $1 is set aside for use by the assembler (and so we should not depend upon having it for our code); name is $at
– $31 is set aside for return addresses from procedures; name is $ra
• There are also five groups of registers
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 54
Important convention on registers
• Returned value registers: – $2,$3
– with names $v0, $v1 • Argument registers:
– $4,$5,$6,$7
– with names $a0, $a1, $a2, $a3
• Temporary registers (mind the gap!)
– $8,$9,$10,$11,$12,$13,$14,$15,$24,$25
– with names $t0 through to $t9 • Saved registers (no gaps!)
– $16to$23
– with names $s0 to $s7
• Kernel reserved registers (Verboten!) – $26,$27
– with names $k0, $k1
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 55
Translating an “if-else” structure
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 56
Re-written example (w/ procedure)
.tex#t ... some operations
# Apply operation to $s4.
# Procedure assumes parameter for # operation is in $a0, with
# returned value placed in $v0. # Make sure to overwrite $s4
# with result.
add $a0, $zero, $s4
jal reset_rightmost_set_contiguous
add $s4, $zero, $v0 # ... more operations
# Apply operation to $s0, place # result back into $s0.
add $a0, $zero, $s0
jal reset_rightmost_set_contiguous
add $s0, $zero, $v0
University of Victoria Department of Computer Science
# more operations...
# Apply operation to $s3, place # result back into $s3.
add $a0, $zero, $s3
jal reset_rightmost_set_contiguous add $s3, $zero, $v0
# Procedure assumes $8 can be used # as a scratch register at all
# times.
# And now finish program
reset_rightmost_set_contiguous:
addi $t0, $a0, -1 or $t0, $t0, $a0 addi $t0, $t0, 1 and $t0, $t0, $a0 add $v0, $zero, $t0 jr $ra
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 57
Some general comments
• The assembler will not check if we break the conventions / purposes of each register!
– Ifwedecidethat$t0willholdtheparameterfora procedure, then the assembler will let us do this.
– Ifwedecidetouse$a4injr$a4,thentheassemblerwilllet us do this.
– Makingsuchchoiceswould,however,beunkindand unsociable (even to our future selves)!
• The assembler will not check if a value is stored in $v0
– Eventhoughthecallingcodeexpectstheresultin$v0...
– ...thisdoesnotcausetheassemblertoforceanerrorifthe procedure fails assign a value to $v0
• Working with procedure parameters and return values requires very careful work!
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 58
Some general comments
• When using procedures in a programming language, we depend upon a lot more than parameters
– Local variables
– Functions calling functions needs to work correctly (i.e., keep track of nested return addresses)
– If function calls itself (i.e., recursion) then multiple copies of local variables
– Etc. etc.
• All of this is something that we must write for ourselves!
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 59
A bigger issue
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value
# CPU NEVER REACHES HERE!!! addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
jal procedure_A
jal procedure_B
CSC 230: Computer Architecture and Assembly Language jr $ra Functions in MIPS32: Slide 60
University of Victoria Department of Computer Science
• Considerthecodeas seen on the right.
• Itisabitcontrived,but its control-flow (or the intent of the control flow) should not be not be hard to follow.
• Thereisaserious problem here.
– What is it? – Whyisit?
leaf vs. non-leaf procedures
• The issue can be highlighted if we look at a call graph
– Thisisborrowedfromprogramming-languagedesign
– Itisavisualizationshowingtherelationshipbetween procedures as callers and callees
• Because procedure_A and procedure_B have no edges leading from them in the call graph, they are called leaf procedures
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 61
Preserving $ra
• The error from two slides ago an example of a more general kind of error
– There are 32 general-purpose registers (GPRs)
– Some of these are meant to have a very specific purpose (such as
$ra)
• The 32 GPRs must be carefully shared amongst all lines of code...
• ... yet we must explicitly ensure this sharing does not overwrite existing- and perhaps later-needed values in some registers
• Note!
– These details are always taken care of for us when we write in a
higher-level programming language.
– Mistakes in assembly-language programming are usually caused
when we forget that this is not taken care of for us in assembly!
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 62
An idea that works, but ...
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
add $s1, $zero, $ra
jal procedure_A
jal procedure_B
add $ra, $zero, $s1
CSC 230: Computer Architecture and Assembly Language
jr $ra
Functions in MIPS32: Slide 63
"Usnivaervsiety/ofrViectosrtiaore" purpose??! Department of Computer Science
• We could add a bit of code to procedure_C
– Thissavesthevalueof$ra into $s1 upon procedure entry...
– ...andrestores$rafrom $s1 just before procedure exit
• But this solution is very fragile and will not always work when used elsewhere
– Whatifacalledprocedure already uses $s1?
– Cananyregisterseverbe safely used for this
A better idea: the stack
• A more flexible and general idea is to use a portion of data memory that we call the stack
– The“top”ofthestackis indicated by $sp
• This stack does work by magic!
• We must maintain the stack ourselves:
– Growitasneeded
– Shrinkitasneeded
– Writetoandreadfrom the stack as needed
– $sp is the same as $29 University of Victoria
Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 64
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
addi $sp, $sp, -4 sw $ra, 0($sp) jal procedure_A jal procedure_B lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 65
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
addi $sp, $sp, -4 sw $ra, 0($sp) jal procedure_A jal procedure_B lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra
This example shows the MARS default starting value in $sp; in a physical process we would need some line of code at program start to properly initialize $sp.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 66
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
addi $sp, $sp, -4 sw $ra, 0($sp) jal procedure_A jal procedure_B lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra
Stack has "grown" in size by a word.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 67
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
addi $sp, $sp, -4 sw $ra, 0($sp) jal procedure_A jal procedure_B lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra
Assuming the 0x00400010 was in $ra...
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 68
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
addi $sp, $sp, -4 sw $ra, 0($sp) jal procedure_A jal procedure_B lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 69
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
addi $sp, $sp, -4 sw $ra, 0($sp) jal procedure_A jal procedure_B lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra
$ra now has its value overwritten by 0x00400010
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 70
.text
main:add $s0, $zero, $zero jal procedure_A
jal procedure_B
jal procedure_C
# $s0 now has a value addi $v0, $zero, 10 syscall
procedure_A:
addi $s0, $s0, 20
jr $ra
procedure_B:
addi $s0, $s0, 17
jr $ra
procedure_C:
addi $sp, $sp, -4 sw $ra, 0($sp) jal procedure_A jal procedure_B lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra
Stack has "shrunk" in size by a word.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 71
Generalizing this approach
• Thestackcanbeusedtosaveandrestoremany more register values
• Example:$sNregisters
– $s1, $s2, $s3, $s4, $s5, $s6, $s7
– These are more properly called callee-saved registers (which explains the letter s)
– If a procedure writes to such a register...
– ... then it must ensure that its original value as procedure start is restored when the procedure returns
– The register can be saved/restored with help of the stack
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 72
.text ...
procedure_M:
addi $sp, $sp, -12 sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
...
addi $s0, $zero, 10 loop_M_outer:
beq $s0, $zero, finish_m
addi $s1, $zero, 12 loop_M_inner:
beq $s1, $zero, loop_M_outer
jal procedure_N
addi $s1, $s1, -1
addi $s0, $s0, -1
beq $zero, $zero, loop_M_inner
finish_m:
lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_N:
University of Victoria
Department of Computer Science
...
Some assumptions:
procedure_m is the first to use the stack.
Value in $ra at entry of procedure_m is 0x004000038
Values in $s0 and $s1 are 380 and -911 respectively.
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 73
.text ...
procedure_M:
addi $sp, $sp, -12 sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
...
addi $s0, $zero, 10 loop_M_outer:
beq $s0, $zero, finish_m
addi $s1, $zero, 12 loop_M_inner:
beq $s1, $zero, loop_M_outer
jal procedure_N
addi $s1, $s1, -1
addi $s0, $s0, -1
beq $zero, $zero, loop_M_inner
finish_m:
lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_N:
University of Victoria
Department of Computer Science
...
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 74
Stack has "grown" in size by three words.
.text ...
procedure_M:
addi $sp, $sp, -12 sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
...
addi $s0, $zero, 10 loop_M_outer:
beq $s0, $zero, finish_m
addi $s1, $zero, 12 loop_M_inner:
beq $s1, $zero, loop_M_outer
jal procedure_N
addi $s1, $s1, -1
addi $s0, $s0, -1
beq $zero, $zero, loop_M_inner
finish_m:
lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_N:
University of Victoria
Department of Computer Science
...
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 75
$ra is stored on the stack
.text ...
procedure_M:
addi $sp, $sp, -12 sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
...
addi $s0, $zero, 10 loop_M_outer:
beq $s0, $zero, finish_m
addi $s1, $zero, 12 loop_M_inner:
beq $s1, $zero, loop_M_outer
jal procedure_N
addi $s1, $s1, -1
addi $s0, $s0, -1
beq $zero, $zero, loop_M_inner
finish_m:
lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_N:
University of Victoria
Department of Computer Science
...
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 76
$s0 is stored on the stack
.text ...
procedure_M:
addi $sp, $sp, -12 sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
...
addi $s0, $zero, 10 loop_M_outer:
beq $s0, $zero, finish_m
addi $s1, $zero, 12 loop_M_inner:
beq $s1, $zero, loop_M_outer
jal procedure_N
addi $s1, $s1, -1
addi $s0, $s0, -1
beq $zero, $zero, loop_M_inner
finish_m:
lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_N:
University of Victoria
Department of Computer Science
...
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 77
$s1 is stored on the stack
.text ...
procedure_M:
addi $sp, $sp, -12 sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
...
addi $s0, $zero, 10 loop_M_outer:
beq $s0, $zero, finish_m
addi $s1, $zero, 12 loop_M_inner:
beq $s1, $zero, loop_M_outer
jal procedure_N
addi $s1, $s1, -1
addi $s0, $s0, -1
beq $zero, $zero, loop_M_inner
finish_m:
lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_N:
University of Victoria
Department of Computer Science
...
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 78
We are now depending upon procedure_N saving and restoring $ra and any $s0 registers it uses.
.text ...
procedure_M:
addi $sp, $sp, -12 sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
...
addi $s0, $zero, 10 loop_M_outer:
beq $s0, $zero, finish_m
addi $s1, $zero, 12 loop_M_inner:
beq $s1, $zero, loop_M_outer
jal procedure_N
addi $s1, $s1, -1
addi $s0, $s0, -1
beq $zero, $zero, loop_M_inner
finish_m:
lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_N:
University of Victoria
Department of Computer Science
...
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 79
Begin to restore values to $s0, $s1, and $ra to reflect what was true when procedure_M started
.text ...
procedure_M:
addi $sp, $sp, -12 sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp)
...
addi $s0, $zero, 10 loop_M_outer:
beq $s0, $zero, finish_m
addi $s1, $zero, 12 loop_M_inner:
beq $s1, $zero, loop_M_outer
jal procedure_N
addi $s1, $s1, -1
addi $s0, $s0, -1
beq $zero, $zero, loop_M_inner
finish_m:
lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_N:
University of Victoria
Department of Computer Science
...
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 80
Stack is "shrunk" by three words.
Other registers
• What about $tN registers?
– $t0, $t1, $t2, $t3, $t4, $t5, $t6, $t7
– Theseareconsideredcaller-saveregisters
– Thismeansthattheonlycodethatshouldworryabout saving them is the procedure itself that uses them.
– Putdifferently:Wecannotassumeany$tNregisterretains the same value after control returns from a jal instruction.
• What about $aN registers?
– Thecaller(i.e.,codewithinaprocedure)mustsaveand
restore these as needed.
– However,wecanotherwisemakethesameassumptionsas
with $tN registers after a jal instruction. • Put differently:
– Ourproceduresmustalwayssave/restoreused$sNregisters University of Victoria CSC 230: Computer Architecture and Assembly Language
– Non-leafproceduresmustsave/restore$sNregisterused Department of Computer Science Functions in MIPS32: Slide 81
– Allotherregistersneednotbesaved/restored.
Stacks + nesting
• The power of the stack becomes clearer as we have arbitrary nesting of procedures
• The stack grows downward as procedures are called.
• The stack shrinks upwards as procedures end.
• We write the code to ensure correct stack maintenance.
• Example: Procedures E & G both use $s0; procedure F only uses $tN registers.
– Call graph is shown on right.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 82
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 83
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Assume we are "in media res" (i.e., in the
thick of things) with the stack already in
use by other code.
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 84
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 85
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Reserved room to save $ra and $s0
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 86
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Values now saved on the stack.
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 87
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
About to call procedure_F
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 88
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Adjusted stack so there is room for $ra
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 89
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
$ra is now saved
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 90
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
About to call procedure_G
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 91
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Stack was adjusted to provide room to save $s0
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 92
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
$s0 now saved
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 93
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
About to return from procedure_G; must restore $s0 and adjust stack.
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 94
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Perform jr $ra
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 95
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Back in procedure F
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 96
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Restore $ra, adjust stack size
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 97
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
About to perform jr $ra
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 98
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Returned from procedure_F
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 99
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
Restore $s0, $ra, and then adjust stack size
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 100
... procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) ...
jal procedure_F ...
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_G ...
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
lw $s0, 0($sp)
...
addi $sp, $sp, 4
University of Victoria jDrep$arrtmaent of Computer Science
About to perform jr $ra
... procedure_X:
addi $sp, $sp, -12
Translating an “if-else” structure
sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, 0($sp) ...
jal procedure_Y ...
lw $ra, 0($sp) lw $s0, 4($sp) lw $s1, 8($sp) addi $sp, $sp, 12 jr $ra
procedure_Y:
addi $sp, $sp, -4
sw $ra, 0($sp) ...
jal procedure_Z ...
lw $ra, 0($sp)
jr $ra
procedure_Z:
addi $sp, $sp, -4
sw $s0, 0($sp)
...
sw $s0, 0($sp)
addi $sp, $sp, 4
jr $ra ...
University of Victoria Department of Computer Science
How many things are
wrong with this code?
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 101
Still a bit more to say about the stack...
• Theprincipleroleplayedbythestackisasmemory to safely save and restore register values
• Thisstackisnotmeanttobeanalgorithmic stack!
– If we need a stack as part of general problem solving, we must to do something different
• Therearestillafewthingsthestackcanmake possible for us
– We will describe these after looking more closely as parameter passing and what is possible in assembly- language
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 102
Semantics for parameter passing
• Theworldofprogramminglanguagesincludes several different parameter-passing semantics
• Pass-by-value – Java, C, C++
• Pass-by-reference – C++
• Pass-by-sharing
– Object references in Java, Python
– (Actually a variant of pass-by-value)
• Severalothers
– Pass-by-result, pass-by-value-result, pass-by-name
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 103
Code written in C++
#include
Re-written example (w/ procedure)
void swap_that_does_not_work(int m, int n)
{
inttemp=m; m=n; n=temp;
}
void swap_that_DOES_work(int &m, int &n)
{
inttemp=m m=n; n=temp;
}
int main() {
int a = 111, b = 222, c = 888, d = 999;
cout << "Swap with call-by-value:" << endl;
cout << "Before: a = " << a << ", b = " << 222 << endl; swap_that_does_not_work(a, b);
cout<<"After: a="< b?
# is b > c? # c is max
# b is max
# is a > c? # c is max
# a is max
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 111
Pass by reference
Translating an “if-else” structure
.data
aaa: .word 123 bbb: .word 345 ccc: .word 209
.text
la $a0, aaa
la $a1, bbb
la $a2, ccc
# load aaa address into $s0 # load bbb address into $s1 # load ccc address into $s2
jal max_call_by_reference # call procedure
add $v0, $zero, 10 syscall
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 112
Pass by reference
Translating an “if-else” structure
max_call_by_reference: lw $t1, 0($a0) lw $t2, 0($a1) lw $t3, 0($a2)
slt$t0,$t2,$t1 #isa>b? bne $t0, $zero, is_a_gt_c
slt$t0,$t3,$t2 #isb>c?
bne $t0, $zero, b_is_max add $v0, $zero, $t3
jr $ra
b_is_max:add $v0, $zero, $t2 jr $ra
is_a_gt_c:
slt $t0, $t3, $t1
bne $zero, $t0, a_is_max add $v0, $zero, $t3
jr $ra
a_is_max:add $v0, $zero, $t1
# c is max # b is max
# c is max
# a is max CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 113
University of Victoria
jr $ra
Department of Computer Science
More observations
• Thesharped-eyedwillnoticethereislittle difference here between call-by-reference and using pointers in C
– You would be correct…
– … and even though C only has call-by-value semantics …
– … we still traditionally name such semantics in a assembly-language as call-by-reference
• Thereisnorulethatonlyonekindofsemanticscan be used per procedure
– Some parameters might be call-by-value
– Other parameters might be call-by-reference
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 114
Observations
• The code for call-by-reference looks far more complicated than that for call-by-value
• However:
– Call-by-reference is able to provide a parameter located in any memory location in data memory (normally outside of the stack) …
– …andthatlocationmaybecomputedatrun-time! • Also:
– Datamemoryindicatedbycall-by-referencecanbe arbitrarily large…
– …suchasthatforarrays…
– …orlargerblocksofbinarydata…
– …etc.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 115
Local state
• At present all of the values used in our procedures are stored in registers
– That is, all quantities needed must be loaded or stored from registers
• However, this is somewhat fragile
– A register really is not the same as a local variable in a regular programming language
– We would, however, like to have some of the features of such local variables
• Solution: Use the runtime stack as a place to store what we consider as local “variables”
– This means allocating enough room on the stack for not only saved registers…
– … but also for these local values.
• Stack frame:
– The contiguous memory of a stack used for a procedure
– That is, words on the stack uses for saved registers and local variables
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 116
… procedure_E:
addi $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) …
jal procedure_F …
lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra
procedure_F:
addi $sp, $sp, -4
sw $ra, 0($sp)
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
procedure_G:
addi $sp, $sp, -4
sw $s0, 0($sp)
…
lw $s0, 0($sp)
addi $sp, $sp, 4
jr $raUniversity of Victoria Department of Computer Science
Stack frame for
jal procedure_G
procedure_F
…
…
…
Stack frame for
procedure_E
Stack frame for
procedure_G
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 117
Motivating example: fibonacci
/* Basic idea */
int fibonacci (int n) { if (n < 2) {
} return 1;
n = n – 1;
int a = fibonacci(n); n = n - 1;
int b = fibonacci(n);
return (a + b);
}
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 118
University of Victoria Department of Computer Science
• Recursive version of Fibonacci
– Indexfromzero(wheref0 =1,f1 =1,f2 =2,f3 =3,f4 =5,f5 =8, etc.) which is a bit different from usual math textbooks
– Inpracticewewoulduseaniterativeversion...
– ...butshowingrecursionwillhelpindicatehowstackframes are a big win
.text
# Compute the third Fibonacci number
addi $a0, $zero, 3 jal fibonacci
# Print the integer result onto the console
add $a0, $zero, $v0 addi $v0, $zero, 1 syscall
# Terminate the program
addi, $v0, $zero, 10 syscall
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 119
# fib(n) where n is in $a0
fibonacci:
addi $sp, $sp, -16
sw $ra, 12($sp)
sw $s0, 8($sp)
# We'll keep 8($sp) for the "a" variable
# and we'll keep 12($sp) for the "b" variable
# We'll copy the parameter into $s0 as we
# want to use this value between recursive calls. add $s0, $zero, $a0
# Is the parameter < 2? If so, return 1
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
# If we arrive here, it is because we must # now perform the recursive calls.
# Make the recursive call for fib(n-1)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
# Now store the return result into space we've # set aside on the stack (i.e., for "a")
sw $v0, 4($sp)
University of Victoria
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 120
Department of Computer Science
# ... continued on next slide
# ... continued from previous slide
# Make the recursive call for fib(n-2)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
# Now store the return result into space we've # set aside on the stack (i.e., for "b")
sw $v0, 0($sp)
# Now we can compute the sum of "a" and "b".
# The value of "b" is *already* in $v0, but
# the code below is written to mirror more
# closely what is shown in the programming-language # version of fibonacci
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 12($sp)
lw $ra, 8($sp) addi $sp, $sp, 16 jr $ra
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 121
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 122
Current call: fibonacci(3)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Red: register-save portion of stack frame Green: local variable a
Blue: local variable b
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 123
Getting ready to call fibonacci(3-1)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 124
Current call fibonacci(2)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 125
Getting ready to call fibonacci(2-1)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 126
Current call fibonacci(1)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 127
fibonacci(1) is a base case (notice 1 is already in $v0)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Department of Computer Science
Functions in MIPS32: Slide 128
CSC 230: Computer Architecture and Assembly Language
About to return from fibonacci(1)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 129
Returned from fibonacci(1); back in call to fibonacci(2)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 130
Getting ready to call fibonacci(2-2)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp)
lw $s0, 0($sp)
Recall: we are in fibonacci(2) if n = 2,
add $v0, $v0, $s0
then fibonacci(n) is equal to fibonacci(n-1) + fibonacci(n-2)
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 131
Current call fibonacci(0)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 132
fibonacci(0) is another base case
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
CSC 230: Computer Architecture and Assembly Language Department of Computer Science Functions in MIPS32: Slide 133
About to return from fibonacci(0)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
CSC 230: Computer Architecture and Assembly Language
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Returned from fibonacci(0); back in call to fibonacci(2)
Department of Computer Science
Functions in MIPS32: Slide 134
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Values of "a" and "b" have been added,
and stored into $v0 (i.e., 1+1=2)
About to return from fibonacci(2)
CSC 230: Computer Architecture and Assembly Language
Functions in MIPS32: Slide 135
Department of Computer Science
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Department of Computer Science
Back in fibonacci(3)
We just returned from the call to
fibonacci(3-1)
CSC 230: Computer Architecture and Assembly Language
Now we are about to make the call to
Functions in MIPS32: Slide 136
fibonacci(3-2)
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Department of Computer Science
Start fibonacci(1)
CSC 230: Computer Architecture and Assembly Language
Functions in MIPS32: Slide 137
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
We'll skip ahead to where the fibonacci(1) (as we have seen this before and know we have a base case).
Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 138
CSC 230: Computer Architecture and Assembly Language
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Returned from fibonacci(1); back in fibonacci(3)
Department of Computer Science
Functions in MIPS32: Slide 139
fibonacci:addi $sp, $sp, -16 sw $ra, 12($sp)
sw $s0, 8($sp)
add $s0, $zero, $a0
addi $v0, $zero, 1
addi $t0, $zero, 2
slt $t1, $s0, $t0
bne $t1, $zero, fibonacci_finish
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 4($sp)
addi $s0, $s0, -1 add $a0, $zero, $s0 jal fibonacci
sw $v0, 0($sp)
lw $v0, 4($sp) lw $s0, 0($sp) add $v0, $v0, $s0
fibonacci_finish:
lw $s0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
University of Victoria
Values of "a" and "b" have been added,
and stored into $v0 (i.e., 2+1=3)
About to return from fibonacci(3)
Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 140
A few comments
• Althoughtracingthecodemightbeabittrickyand confusing...
• ...oncewetrusttheformationanduseofstack frames, we can have confidence that the code will work with such recursion/nesting.
• Theonlydanger,ofcourse,isrunningoutofstack
– (Normally this is a bug as when we have buggy / infinite recursion.)
• Also:
– Such a use of stack frames is not just for recursion.
– Can also be used with any other procedure needing local variables (arrays, scalars)
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 141
Another example
void read_and_normalize() : int values[10], *v, min;
v = min for
}
v = for
&values[0];
= maximum integer value possible in 32-bit 2's complement (i = 10; i != 0; i--) {
read an integer into *v;
if that integer is < min then min = that integer
v += 4;
&values[0];
(i = 10; i != 0; i--) {
print *v - min
}
v += 4; }University of Victoria
Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 142
• Aprocedurethat:
– Reads in a few number of integers
– Determines the minimum integer...
– ... and prints out each original integer minus the min
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 143
Things to do
• Decideonnumberofcallee-saveregistersthatare needed
• Writeprocedureprologue
• Writeprocedureepilog
• Systemcalls
– Outputastring(syscall4;stringaddressin$a0) – Inputaninteger(syscall5;resultin$v0)
– Outputaninteger(syscall1;integerin$a0)
• Maintainaregisterthatholdsarray-element address within stack frame
– ... but doing so in a way that leaves $sp untouched during array access
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 144
Back to parameters, return values
• Therearefournamedregistersavailablefor procedure arguments
– $a0, $a1, $a2, and $a3
• Therearetwonamedregistersavailableforreturn
values
– $v0 and $v1
• Theseregistersareenoughformostprogramming purposes
• Butwhatifweneedmore?
– What if our procedure has five or more arguments?
– What if our procedure is to return three or more values?
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 145
Some limitations
• But what if we need more?
• Option 1: Use some of the other registers
– The $sN or $tN registers could be roped into service
– Benefit: Easy to do
– Drawbacks: There still exists a hard limit; approach does not easily generalize for variable-length argument lists (i.e., when translating C to assembly)
• Option 2: Use the runtime stack
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 146
Using frame pointer & stack pointer
• Idea:
– Calling code knows the number of parameters needed for the function it is calling.
– Calling code also knows what parameters can be stored in registers, and what must be stored on stack
• Calling code:
– Copy parameters into registers
– Enlarge stack for extra parameters
– Store extra parameters into stack
– Call the function
• Called (i.e., callee) code:
– Create stack frame
– Stores callee-save registers
– Also saves $fp register
– Then assigns to $fp the value $sp had before the callee-code
started!
CSC 230: Computer Architecture and Assembly Language
University of Victoria
Functions in MIPS32: Slide 147 – May now access stack-saved caller registers using $fp
Department of Computer Science
State of stack before caller begins
to “marshal parameters” for procedure
call.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 148
Caller knows that it must place excess
parameters onto stack, so it enlarges
the stack and assigns two word values
(for the parameters).
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 149
Callee is now running, with stack
frame constructed. But some extra
state must be saved: $fp (which is a
callee-save register).
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 150
Assuming other registers have been
already saved, diagram now shows the
result of saving $fp into the stack...
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 151
... such that we can now assign to $fp
the value of $sp before the call
happened (i.e., callee can easily
compute the value to store in $fp).
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 152
... such that we can now assign to $fp
the value of $sp before the call
happened (i.e., callee can easily
compute the value to store in $fp).
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 153
Time passes (i.e., the procedure code runs)
And now it is time to do the work necessary
to return from the callee.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 154
The $fp of the caller is now restored...
... and other registers are restored. We're
just about to perform the final jr
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 155
Now control is back into the caller...
... which must undo its changes to the stack
for the extra parameters....
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 156
Now the stack is as it was before the call to
the procedure!
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 157
Using frame pointer & stack pointer
• This is all fairly advanced
– Normallythistechniqueisusedbycompilerwriters(i.e., generating assembly from C code)
• Assembly-only programming tends to not use large numbers of parameters
• Also:
– Thereisavarietyofopiniononhowthe$fpand$sprelate.
– Thesedifferentopinionsresultinwhataretermed“calling conventions”.
– Showinthepreviousslidesisonparticularcalling convention.
– Aremeanttoensuretools“playwellwithothers”.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 158
Summary
• Introducedsyntaxforcallandreturningfrom procedures in MIPS32 architecture
• Methodsforpassingparameters
– Distinction between call-by-value and call-by-reference
– Use of registers
– Use of stack (i.e., advanced technique)
• Methodsforreturningvalues – Registers
• Stackframestosupportsavingandrestoringof registers
– Also supports local variables
– Plus enables to writing of recursive routines.
University of Victoria Department of Computer Science
CSC 230: Computer Architecture and Assembly Language Functions in MIPS32: Slide 159