MIPS Assembly Language: CPSC 321 Computer Architecture Andreas Klappenecker
MIPS Assembly Language: CPSC 321 Computer Architecture Andreas Klappenecker
MIPS Assembly Language: CPSC 321 Computer Architecture Andreas Klappenecker
main: . . .
li $a0, 10 # we want to print 10
jal print_int # print integer in $a0
Write your own procedures
.data
linebrk: .asciiz “\n”
.text
print_eol: # prints "\n"
li $v0, 4 #
la $a0, linebrk #
syscall #
jr $ra # return
main: . . .
jal print_eol # printf(“\n”)
Write your own procedures
.data
main:
li $s0, 1 # $s0 = loop ctr
li $s1, 10 # $s1 = upperbnd
loop: move $a0, $s0 # print loop ctr
jal print_int #
jal print_eol # print "\n"
addi $s0, $s0, 1 # loop ctr +1
ble $s0, $s1, loop # unless $s0>$s1…
Non-leaf procedures
Suppose that a procedure procA calls
another procedure jal procB
Problem: jal stores return address of
procedure procB and destroys return
address of procedure procA
Save $ra and all necessary variables
onto the stack, call procB, and retore
Stack
The stack can be high address 8($sp)
used for
parameter passing
4($sp)
0, 1, 1, 2, 3, 5, 8, 13, 21,…
Fibonacci
li $a0, 10 # call fib(10)
jal fib #
move $s0, $v0 # $s0 = fib(10)