0

I'm trying to execute this MIPS assembly code for fibonacci sequence (trying to find 10th fibonacci number, fib(10) in main. in QtSpim and it keeps throwing errors such as: "Exception occured at PC=0x7ffff774" and "Bad Address in text read..."

I'm not sure what I'm doing wrong, I think the error might be in the main statement.

.data
num: .word 10
ans: .word -1

.text

.globl main
main:
lw $a3, num
jal fib
sw $v1, ans
li $v0, 1
move $a0, $v1
syscall

fib: addi $sp, $sp, -12
   sw $a0, 8($sp)
   sw $ra, 4($sp)
   sw $s0, 0($sp)
   slti $t0, $a0, 2
   beq $t0, $0, else
   addi $v0, $a0, 0
   j exit

else: addi $a0, $a0, -1
  jal fib
  add $s0, $v0, $0
  addi $a0, $a0, -1
  jal fib
  add $v0, $v0, $s0

exit: lw $a0, 8($sp)
  lw $ra, 4($sp)
  lw $s0, 0($sp)
  addi $sp, $sp, 12
  jr $ra
  • Why would you jump to `exit` from `main`? Your `main` "routine" didn't push `$ra` (or anything else) on the stack, so trying to pop it and then jump to wherever it happens to point to is incorrect. – Michael Feb 22 '16 at 06:53
  • So how do I make the main function end or return an output in the console? – Díva Agarwal Feb 22 '16 at 12:13
  • Use system call 10 to end execution of your program. – Michael Feb 22 '16 at 12:14
  • So instead of "j exit" just syscall? I tried to do that and it didn't compile. – Díva Agarwal Feb 22 '16 at 12:31
  • _"So instead of `j exit` just syscall?"_ No. You have to specify that you want system call 10. See https://www.doc.ic.ac.uk/lab/secondyear/spim/node8.html – Michael Feb 22 '16 at 12:47
  • I made some changes to my program if you look above. I tried to run it again and the console was blank and then QtSpim stopped responding. Not really sure what I'm doing wrong now. – Díva Agarwal Feb 22 '16 at 19:21
  • The only system call you're performing after `fib` returns is system call 1 (`print_int`). As I said in my second comment you also need to use system call 10 to terminate the program. – Michael Feb 22 '16 at 20:27
  • I am getting 0 as the output... – Díva Agarwal Feb 23 '16 at 04:56

0 Answers0