Published On Feb 26, 2019
Tutorial by Ruvim
about functions and stacks in mips assembly language
to push into stack
increase the size of the stack
#load our data into the stack
get data from stack pointer
load data from the stack to whatever register we want
decrease the size of the stack by adding the amount we need to decrease by.
.text
main: nop
li $v0, 4 #loads 4 into v0, 4 means print statement
la $a0, test # prints our test string below
syscall
jal function
li $v0, 4 #loads 4 into v0, 4 means print statement
la $a0, test # prints our test string below
syscall
li $v0, 10
syscall
function: nop
############################## save current ra
need to push to the stack
sub $sp , $sp, 4
sw $ra, ($sp)
###############################
li $v0, 4 #loads 4 into v0, 4 means print statement
la $a0, func # prints our func string below
syscall
calls the inner function
jal innerFunc
############## retrieve the correct RA needed
need to pop the stack and store the value into current ra
lw $ra, 0($sp)
addi $sp, $sp , 4
#############################
jr $ra
innerFunc: nop
############################## save current ra
need to push to the stack
sub $sp , $sp, 4
sw $ra, ($sp)
###############################
li $v0, 4 # loads 4 into v0, a 4 in v0 means print statement
la $a0, inner # prints out string, see below
syscall
############## retrieve the correct RA needed
need to pop the stack and store the value into current ra
lw $ra, 0($sp)
addi $sp, $sp , 4
#############################
jr $ra
.data
#random test strings
test: .asciiz " \n This is a test string \n "
func: .asciiz "\n inside the first function \n"
func2: .asciiz "\n still inside function \n"
inner: .asciiz "\n we are in the inner function now \n"