Lecture Mips Functions
Lecture Mips Functions
Philipp Koehn
2 October 2019
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
1
pseudo instruction
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Assembler 2
Address Instruction
loop ...
...
j loop
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Pseudo Instructions 3
li $s0, 32648278h
• Requires 2 instructions
lui $s0, 3264h
ori $s0, $s0, 8278h
• Pseudo instruction
– available in assembly
– gets compiled into 2 machine code instructions
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Syntactic Sugar 4
• Move
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Reserved Register 5
lw $s0, 32648278h
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Another Example 6
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
7
code example
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Factorial 8
• Compute n! = n × n − 1 × n − 2 × ... × 2 × 1
• Iterative loop
– initialize sum with n
– loop through n-1, n-2, ..., 1
– multiple sum with loop variable
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Implementation 9
• Registers
– $a0: n (loop variable)
– $v0: sum
• Initialize
move $v0, $a0 # initialize sum with n
• Loop setup
loop:
addi $a0, $a0, -1 # decrement n
beq $a0, $zero, exit # = 0? then done
...
j loop
• Multiplication
mul $v0, $v0, $a0 # sum = sum * n
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Code 10
.text
main:
li $a0, 5 # compute 5!
move $v0, $a0 # initialize sum with n
loop:
addi $a0, $a0, -1 # decrement n
beq $a0, $zero, exit # = 0? then done
mul $v0, $v0, $a0 # sum = sum * n
j loop
exit:
jr $ra # done
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
11
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Jump 12
• MIPS instruction
j address
jr $register
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Jump and Link: Subroutines 13
• MIPS instructions
jal address
jalr $register
jr $ra
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Register Conventions 14
• Conceptually
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Example 15
main:
li $a0, 10
li $a1, 21
li $a2, 33
jal add3
add3:
add $v0, $a0, $a1
add $v0, $v0, $a2
jr $ra
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Another Example 16
• Subroutine for a + b - c
main:
li $a0, 10
li $a1, 21
li $a2, 33
jal add-and-sub
add-and-sub:
add $a0, $a0, $a1
move $a1, $a2
jal my-sub
jr $ra
my-sub:
sub $v0, $a0, $a1
jr $ra
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Safekeeping 17
• Note
– all this is by convention
– you have to do this yourself
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
18
stack
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Stack 19
• Recall: 6502
– JSR stored return address on stack
– RTS retrieved return address from stack
– special instructions to store accumulator, status register
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Alternate Idea 20
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Store Return Address on Stack 21
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Retrieve Return Address from Stack 22
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Multiple Registers 23
• Load
lw $ra 0($sp)
lw $s0 4($sp)
lw $s1 8($sp)
addi $sp, $sp, 12
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Frame Pointer 24
• Example
– subroutine stores return address and some save registers on stack
– some code does something
(maybe even store more things on stack)
– subroutine wants to consult stored return address
• Solution
– store entry stack pointer in frame pointer $fp (30th register)
move $fp, $sp
– retrieve return address using frame pointer
lw $s0, 0($fp)
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
25
example
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Recall: Factorial 26
.text
li $a0, 5 # compute 5!
move $v0, $a0 # initialize sum with n
loop:
addi $a0, $a0, -1 # decrement n
beq $a0, $zero, exit # = 0? then done
mul $v0, $v0, $a0 # sum = sum * n
j loop
exit:
jr $ra # done
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Implemented as a Function 27
main:
li $a0, 5 # compute 5!
jal fact # call function
exit:
jr $ra # done
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Scaffolding 28
.text
main:
li $a0, 5 # compute 5!
jal fact # call function
jr $ra # done
fact:
(old code)
exit:
jr $ra # done
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Complete Code 29
.text
main:
li $a0, 5 # compute 5!
jal fact # call function
jr $ra # done
fact:
move $v0, $a0 # initialize sum with n
loop:
addi $a0, $a0, -1 # decrement n
beq $a0, $zero, exit # = 0? then done
mul $v0, $v0, $a0 # sum = sum * n
j loop
exit:
jr $ra # done
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Recursive Implementation 30
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Termination Condition 31
• Check if argument is 0
fact:
beq $a0, $zero, final # = 0? then done
(common case)
final:
li $v0, 1
jr $ra # done
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Core Recursion 32
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Save and Restore Registers 33
• Save registers
• Restore registers
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019
Complete Code 34
fact:
beq $a0, $zero, final # = 0? then done
addi $sp, $sp, -8
sw $ra 0($sp) # return address on stack
sw $a0 4($sp) # argument on stack
addi $a0, $a0, -1 # decrement n
jal fact # recursive call -> $v0 is f(n-1)
lw $ra 0($sp) # return address from stack
lw $a0 4($sp) # argument from stack
addi $sp, $sp, 8
mul $v0, $v0, $a0 # f(n-1) * n
jr $ra # done
final:
li $v0, 1
jr $ra # done
Philipp Koehn Computer Systems Fundamentals: MIPS Pseudo Instructions and Functions 2 October 2019