1

I'm working on an assignment for class where I'm supposed to find the sum 0 - n of a number n (example: sum 5 = 1+2+3+4+5). We had already done this for a previous assignment, and now the task is to implement a function call add2(int num1, int num2) into our previous solution. When I load my solution into QtSpim, I get an error saying there is a syntax error for the line

move  $s1, $v1                 # Store returned sum in $s1

and when I attempt to step through the program I get an error saying my Add2 procedure isn't defined. I've tried moving my procedure to the top of the program to fix this error (Had to do this with my .data section to be able to load anything from it) but that does nothing. Not really sure what I'm doing wrong here. Here's my code:

.data
msg1:   .asciiz "Please enter a value for n: \n"
msg2:   .asciiz "The sum from 1 to "
msg3:   .asciiz " is:  "
newline:    .asciiz "\n"

.text

.globl main

main:
    # Print string msg1
    li    $v0, 4                  # print_string syscall code = 4
    la    $a0, msg1               # Load msg1 into arg register
    syscall

    # Get and store n value from user
    li    $v0, 5                  # read_int syscall code = 5
    syscall
    move  $s0, $v0                # Move syscall results to saved register
    li    $s1, 0                  # $s1 value keeps total
    li    $s2, 1                  # $s2 = 1 used to increment loop

    LOOP:
    move  $a1, $s1                 # Load current sum into $a1
    move  $a2, $s2                 # Load current counter into $a1
    jal Add2                      # Add both and return in $v1 and $v2
    move  $s1, $v1                 # Store returned sum in $s1
    move  $s2, $v2                 # Store incremented counter in $s2
    bne $s0, $v2, LOOP            # If $v2 not equal to n value branch to loop
    add $s1, $s1, $s0             # Add final value to $s1

    # Print final message and total

    # First half of message
    li    $v0, 4                  # print_string syscall code = 4
    la    $a0, msg2               # Load msg2 into $a0
    syscall

    # Print n value
    li    $v0, 1                  # print_int syscall code = 1
    move    $a0, $s0              # Move $s0 into $a0
    syscall

    # Second half of message
    li    $v0, 4                  # print_string syscall code = 4
    la    $a0, msg3               # Load msg3 into $a0
    syscall

    # Print total
    li    $v0, 1                  # print_int syscall code = 1
    move    $a0, $t0              # Move total in $t0 to $a0
    syscall

    li    $v0, 10                 # exit syscall code = 10
    syscall

    # Add2 function
    Add2:
    add $v1, $a1, $a2            # Add $a1 and $a2 and save in $v1
    addi $v2, $a2, 1             # Increment $a2 by one and store in $v2
    jr $ra

1 Answers1

1

For me, the first line QtSpim is complaining about is actually move $s2, $v2.

There is no $v2 register on MIPS processors, so you'll have to find some other register to use in its place.

Michael
  • 57,169
  • 9
  • 80
  • 125