You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code was partially generated with GCC using the -Os option. The return from main was replaced with a syscall to halt. Branch and load instructions were moved around as needed to undo GCC reorganization of instructions. GCC did instruction reorganization to fill the delay slots. Our emulator does not implement delay slots.
This code should result in 108 in register $2. Currently this code executes forever. There is some fishy bug that needs to be fixed.
main: addiu$sp,$sp,-4 sw$ra,0($sp) ori$a0,$zero,6 # n = 6 jal fib # Answer will be in$v0. lw$ra,0($sp) addiu$sp,$sp,4syscall # End program.fib: addiu$sp,$sp,-20 sw$s0,0($sp) #$s0 stores the result of fib(n -1). sw$s1,4($sp) #$s1 stores the result of fib(n -2). sw$s2,8($sp) #$s2 stores a copy of n. sw$a0,12($sp) #$a0 stores n. sw$ra,16($sp) # Copy n to$s2.or$s2,$zero,$a0 # If n <= 1 (or n < 2), return. ori$at,$zero,2 slt$t0,$s2,$at bne$t0,$zero, return_base_case # fib(n -1)or$a0,$zero,$s2 addiu$a0,$a0,-1 jal fib # Save result in$s0.or$s0,$zero,$v0 # fib(n -2)or$a0,$zero,$s2 addiu$a0,$a0,-2 jal fib # Save result in$s1.or$s1,$zero,$v0 # Add the two results together. addu$v0,$s0,$s1 b return_fibreturn_base_case: # Copy n to the return value register.or$v0,$zero,$s2return_fib: lw$ra,16($sp) lw$a0,12($sp) lw$s2,8($sp) lw$s1,4($sp) lw$s0,0($sp) addiu$sp,$sp,20 jr$ra
At the end of this program, $v0 (or $2) will contain 8, for the 6th Fibonacci number.
The following code was partially generated with GCC using the -Os option. The return from main was replaced with a syscall to halt. Branch and load instructions were moved around as needed to undo GCC reorganization of instructions. GCC did instruction reorganization to fill the delay slots. Our emulator does not implement delay slots.
This code should result in 108 in register $2. Currently this code executes forever. There is some fishy bug that needs to be fixed.
Here's the initial GCC generated code:
Bugs to look out for:
The text was updated successfully, but these errors were encountered: