# A very simple, pointless function that adds two numbers.
# Demonstrates how to use arguments (ax registers) and returns (v0).
add_numbers:
add v0, a0, a1
jr ra
# Adds three numbers, but demonstrates saving ra and s0.
add_three_numbers:
push ra
push s0 # must be pushed because we use it below.
# We want to keep the value of a2 around after a jal,
# but we can't rely on that because of the calling convention.
# So we will save a2 into s0.
move s0, a2
# We can call add_numbers right now since the first two
# numbers to add are already in a0, a1.
# This is like v0 = add_numbers(a0, a1)
jal add_numbers
# Now the sum of the first two numbers is in v0, and the
# third number is in s0. So we have to shuffle the values
# into the right registers before calling it again.
# This is like v0 = add_numbers(v0, s0)
move a0, v0
move a1, s0
jal add_numbers
# Now the sum of all three is in v0, so we can just return.
pop s0
pop ra
jr ra
.globl main
main:
# call add_three_numbers(5, 8, 13)
li a0, 5
li a1, 8
li a2, 13
jal add_three_numbers
# Now the sum (26) is in v0, so we can print it
move a0, v0
li v0, 1
syscall