0

goodday, so currently my code prompts the user to enter a sum. my program then takes these stored values and outputs the sum of them. However, currently my code is not outputting the correct total and i am unsure where the problem lies.

i have attached my code below:

.text
main:
    #Prompt
    la  $a0, prompt
    li  $v0, 4
    syscall

    # Get input string
    li  $v0, 8
    la  $a0, string_input
    li  $a1, 1024
    move    $t0, $a0
    syscall

    # initialise sum
    lb  $t2, ($t0)          # Initialise the sum as the first value
    addi    $t2, $t2, -48       # Convert to decimal value
    addu    $t0, $t0, 1
    b   loop

loop:
    lb  $t3, ($t0)          # Store the sign
    lb  $t1, 1($t0)         # Store the value
    addu    $t0, $t0, 2         # increment the counter

    beq $t1, 10, end_loop       # If reached the end of the string


    # Manipulate sign value to 1 if '+'
    add $t3, $t3, 4 

    add $t2, $t2, $t1       # Add the value to the sum

    b   loop                # re-iterate

end_loop:
    # Printing the total sum
    move    $a0, $t2
    li  $v0, 1
    syscall
    
    # Exiting program
    li  $v0, 10
    syscall

    .data
string_input:   .space  1024
prompt:     .asciiz "Enter a sum:\n"

the current output should be 11 but this is what is outputted instead:

Enter a sum:
2+3+6
727
Marie
  • 1
  • 2
  • Change `addu $t0, $t0, 2` to `addiu $t0,$t0,2` – puppydrum64 Dec 02 '22 at 14:41
  • It's not clear what you're doing with `$t3`, after you add 4 to it, it's not being stored anywhere or added to `$t2` in any way so it just ends up being discarded when you loop back to `lb $t3, ($t0)`. – puppydrum64 Dec 02 '22 at 14:43

0 Answers0