Created
October 22, 2019 21:32
-
-
Save jyn514/8fda84339d0f67dd703889f01a3f1d20 to your computer and use it in GitHub Desktop.
Fibonacci in MIPS assembly without a jump instruction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fibonacci sequence in MIPS | |
# $s0 stores the next to last item in the sequence | |
# $s1 stores the last item in the sequence | |
# this is just for initialization | |
li $t0, 1 # F_1 (since F_0 is 0, we don't need to initialize the registers) | |
seq $t1, $s1, $0 # if s1 == 0 this is the first run | |
# conditional move: if (t1) s1 = t0 | |
mult $t1, $t0 # if t2 is set, sets LO to t0, otherwise sets LO to 0 | |
mflo $t2 | |
add $s1, $t2, $s1 # s1 += t2 | |
# main loop (runs indefinitely) | |
add $t0, $s0, $s1 | |
move $s0, $s1 | |
move $s1, $t0 | |
# if you know how to get MARS to wrap the PC around, let me know! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment