0

goodday, so currently my code prompts the user to enter the size of the array and then prompts them to enter a series of numbers until the array has been filled. my program then takes these stored values and outputs the sum of them. however, i would like for the user to simply enter an addition calculation of whatever length they choose such as; "2+4+7+1" and the code calculates and outputs the total. I dont not know how to read in the integers and remove the + sign, any help would be greatly appraciated.

.data

instring1:  .asciiz "please enter the size of the sum:\n"
instring2:  .asciiz "enter values\n"
outstring:  .asciiz "Sum:"
    .text
    .globl main
main:
    li  $v0,4
    la  $a0,instring1
    syscall
    
    li  $v0,5
    syscall
    move    $s0,$v0

    mul $t1,$s0,4
    li  $v0,9
    move    $a0,$t1
    syscall
    move    $s1,$v0

    li  $v0,4
    la  $a0,instring2
    syscall

    li  $s2,0

Start_Input:
    bge $s2,$s0,End_Input

    li  $v0,5
    syscall

    mul $t0,$s2,4
    add $t1,$s1,$t0
    sw  $v0,($t1)
    addi    $s2,$s2,1
    j   Start_Input

End_Input:
    add $t0,$zero,$zero
    add $s2,$zero,$zero
    beq $s0,$zero,print_sum

Loop:
    mul $t1,$t0,4
    add $t2,$s1,$t1
    lw  $t3,($t2)
    add $s2,$s2,$t3

    addi    $t0,$t0,1
    slt $t1,$t0,$s0
    bne $t1,$zero,Loop

print_sum:
    li  $v0,4
    la  $a0,outstring
    syscall

    li  $v0,1
    move    $a0,$s2
    syscall

Exit:
    li  $v0,10
    syscall

my current I/O:

please enter the size of the sum:
5
enter values:
4
5
6
7
3
Sum:
25

This is how i would like my I/O to be:

Enter a sum:
4+5+7+1+6
Sum:
25

please let me know if you need anymore clarity on my question and sorry for my english

Marie
  • 1
  • 2
  • parse your string up to `+` or `\n` collect digits, convert them to numbers then sum them up – A. Herlas Oct 14 '22 at 08:33
  • 1
    Does this answer your question? [How to i fix my error when trying to calculate the sum using mips?](https://stackoverflow.com/questions/74067950/how-to-i-fix-my-error-when-trying-to-calculate-the-sum-using-mips) – A. Herlas Oct 14 '22 at 16:14

0 Answers0