# ==========================================================
# Print the first 12 values in the Fibonacci Sequence
# ==========================================================
.data
COMMASPACE: .asciz ", "
INITIALVALUES: .asciz "1, 1"
NEWLINE: .asciz "\n"
.eqv LOOPMAX 12
.text
.global main
main:
# ---- set initial conditions
li t0, 1 # initial Fibonacci value
li t1, 1 # initial Fibonacci value
li t3, LOOPMAX # print count
# ---- print the initial 2 Fibonacci values
li a7, 4 # syscall code, print string
la a0, INITIALVALUES # load address of string
ecall # syscall
addi t3, t3, -2 # decrement print count
# ---- print the remaining Fibonacci sequence
print_loop:
# ---- end of print loop?
ble t3, zero, exit_program # branch if t3 <= zero
# ---- print comma and space
li a7, 4 # syscall code, print string
la a0, COMMASPACE # load address of string
ecall # syscall
# ---- calculate the next next Fibonacci value
# ---- print the next Fibonacci value
add t2, t1, t0 # t2 = t1 + t0
li a7, 1 # syscall code, print integer
mv a0, t2 # next Fibonacci value
ecall # syscall
addi t3, t3, -1 # decrement print count
# ---- get the next two values for the calculation
# ---- and do it again
mv t0, t1
mv t1, t2
j print_loop
# ---- exit program
exit_program:
li a7, 4 # syscall code, print string
la a0, NEWLINE # load address if string
ecall # syscall
li a7, 93 # syscall code, exit program
li a0, 0 # returned value
ecall # syscall