0% found this document useful (0 votes)
43 views

Stack

The document discusses the MIPS stack and procedure calls. It explains that the stack grows from high to low memory addresses, with the stack pointer ($sp) indicating the current top of the stack. When calling a procedure, registers are used to pass arguments and return values, the return address is saved on the stack, and local variables may also be saved on the stack. Frame pointers ($fp) are used to reference values within a stack frame for a given call.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Stack

The document discusses the MIPS stack and procedure calls. It explains that the stack grows from high to low memory addresses, with the stack pointer ($sp) indicating the current top of the stack. When calling a procedure, registers are used to pass arguments and return values, the return address is saved on the stack, and local variables may also be saved on the stack. Frame pointers ($fp) are used to reference values within a stack frame for a given call.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MIPS 

Stack

Giovani Gracioli
[email protected]
http://www.lisha.ufsc.br/~giovani

Set 2007

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 1
Memory Usage

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 2
The Stack
High Addresses

$sp
 The stack grows from High
Addresses to Low Addresses
 Stack pointer ($sp)

Low Addresses

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 3
Registers and Instructions for 
Procedures Calls

Registers $a0­$a3 are used to pass arguments to 
procedures

Register $v0­$v1 are used to return values from 
procedures

$ra – return address register

jal procedure (Jump And Link)

jr $ra (Jump Register)

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 4
Calling a Procedure
 Six steps are executed during the procedure 
call:
1. to put the parameters in a place that can be 
accessed by procedure;
2. to transfer the control to the procedure;
3. to guarantee the memory resources;
4. to execute the task;
5. to put the return value in a place that can be 
accessed by the program;
6. to return the control to the source point.

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 5
Saving the Return Address

push $ra = 
sub $sp, $sp – 4
sw $ra, 0($sp)

pop $ra = 
lw $ra, 0($sp)
addi $sp, $sp, 4

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 6
Returning a value

int function(int a, int b) {


return = a + b;
}

.globl function
function:
add $v0, $a0, $a1
jr $ra

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 7
MIPS Stack Functionalities
 Save arguments regs
(if necessary)
 Save the return
address register ($ra) $fp Saved Arguments Regs
 Save the old value of
Saved Return Address
$fp
 Save regs $s0-$s7 (if Saved old $fp
necessary)
Saved Regs $s0­$s7
 Pass more than 4
arguments Local Variables and
 Declare Local         Structures 
Variables and $sp
Stuctures (if exists)
Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 8
MIPS Stack Functionalities

$fp Saved Arguments Regs
 $fp points to the Saved Return Address
first word of frame
Saved old $fp
 $sp points to the

last word of frame Saved Regs $s0­$s7

Local Variables and
        Structures 
$sp

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 9
Example: local variables
 Save the local variables or  .globl func_a
structures on the stack  func_a:
sub $sp, $sp, 12
sw $ra, 8($sp)
void func_a(void) {
li $t8, 10
int a = 10; sw $t8, 4($sp)
int b = 20; li $t8, 20
return; sw $t8, 0($sp)
} lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra
Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 10
Example: saving the $s* register
.globl func_a
func_a:
 The registers $s0­$s7  sub $sp, $sp, 20
must be saved inside the  sw $ra, 16($sp)
procedure stack if they  sw $s0, 12($sp)
sw $s5, 8($sp)
are used by the program li $t0, 10
void func_a(void) { sw $t0, 4($sp) 
int a = 10; li $t0, 20
sw $t0, 0($sp)
int b = 20; ....

lw $ra, 16($sp)
...
lw $s0, 12($sp)
return; lw $s5, 8($sp)
} addi $sp, $sp, 20
Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani)jr $ra 11
Example: frame pointer
 stack frames or activation records are the stack 
segments that have the saved registers and local 
variables

Saved Arguments Regs
Saved Return Address

stack frame Saved old $fp
Saved Regs $s0­$s7

Local Variables and
        Structures 

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 12
Example: frame pointer

int func_a(int a, int b) {


int var_local;
var_local = func_b();
...
return var_local;
}
int func_b(void) {
int var = 20;
...
return var;
}

Set 2007 Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 13
Example: frame pointer
.globl func_a .globl func_b
func_a: func_b:
sub $sp, $sp, 12 sub $sp, $sp, 12
sw $ra, 8($sp) sw $ra, 8($sp)
sw $fp, 4($sp) sw $fp, 4($sp)
move $fp, $sp # $fp = $sp move $fp, $sp
jal func_b li $t0, 20
sw $vo, 0($fp) sw $t0, 0($fp)
.... ....
lw $v0, 0($fp) lw $v0, 0($fp)
move $sp, $fp # $sp = $fp move $sp, $fp
lw $fp, 4($sp) lw $fp, 4($sp)
lw $ra, 8($sp) lw $ra, 8($sp)
addi $sp, $sp 12 addi $sp, $sp, 12
Set 2007
jr $ra jr $ra
Giovani Gracioli (http://www.lisha.ufsc.br/~giovani) 14

You might also like