程序代写代做代考 assembler algorithm flex mips computer architecture c++ C assembly graph compiler Java kernel CSC 230: Functions in MIPS32

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 using namespace std;
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="<