0

I am trying to learn the MIPS assembly language for my CS class. I am struggling syntactically trying to write this program. I have looked at the other example my professor gave us and I followed it closely. I am getting an error on my branch not equal condition. The code is as follows.

# Main program that will call subroutine fib to calculate the fibonacci and
# print the result
.text
.globl  main
sub $sp,$sp,4
sw $ra,0($sp)

li $a0,3
jal fib
la $a0,str
syscall

sw $a0,0($v0)
syscall

lw $ra,0($sp)
add $sp,$sp,4
jr $ra

.data
str:
    .asciiz "fib = "

.text
fib:

sw $a1,0($a0)
bne $a0,$0,1    # Branch if the argument is not equal to zero
add $v0,$0          # If it is equal to 0,add 0 to the return value.
addi $t0,$0,1   # Create temporary equal to 1.
bne $a0,$t0,1       # Branch if argument is not equal to 1.
addi $v0,1              # If it is,add one to the return value.
addi $sp,$sp,-4     # Create space on the stack for the return value.
sw $ra,0($sp)           # Save old return address to the stack
addi $a0,-1
jal  fib                #jump to fib and save address
addi $a0,$a1,-2     #subtract 2 from the starting number.
jal fib
lw $ra,0($sp)
add $sp,$sp,4
jr $ra

I know that this code will not work properly as of right now. However, I am not asking for help programming this function, I would just like to be able to run it so I can see what is going on. Before I can even load the file I am getting a syntax error on the following line.

bne $a0,$0,1 # Branch if the argument is not equal to zero

The error reads as follows.

spim: (parser) syntax error on line 35 of file ...
    bne $a0,$0,1   #Branch if the argument is not equal to zero
          ^

Any and all help would be greatly appreciated with this! Thank you so much!

Chris McCole
  • 59
  • 2
  • 15

1 Answers1

0

bne will branch to a label which is specified by the third argument when the first argument is not equal to the second argument, in your case, you are telling it to branch if $a0 is not equal to $0, but you are not giving it the label to jump to, the last argument to bne is a label defined anywhere in the source file, what you have is bne $t0, $0, 1, what is 1? Also, 1 is not a valid label's name, labels must only contain letters, numbers, and underscores, they must begin with a letter, and they can't be any of the special instructions in the MIPS instruction set

Trash Can
  • 6,608
  • 5
  • 24
  • 38
  • I know that I can use a label to tell bne to jump to, but I was under the impression that I could also use that last argument as a displacement of lines. Since in MIPS by the time it is done reading the current line the PC will be on the next line already and then I want to go one more space after that. Does this not work? Let me know if this is actually something possible. – Chris McCole Oct 08 '17 at 06:32
  • You are correct, but the last argument is offset from `PC+4` which is the instruction right below your `bne`, the last argument is **not** relative with respect to the byte address of the `bne`, so in this case if `bne` is true, this line `addi $t0,$0,1` will be executed. Also, MIPS is weird sometimes, I got an error when tried to do `sw $t0, 0($ra)`, but it worked when I changed to `move $ra, $t0`. So try changing the `1` in your `bne` to a label and see if it works – Trash Can Oct 08 '17 at 07:49
  • Okay, originally I wanted that line to execute. However, I started implementing it a different way and now that I am using the labels it works like a charm. I am still not exactly sure why, but at least it works. Thanks for all your help! – Chris McCole Oct 09 '17 at 02:17