Why is this value not the value that should be in the array MIPS

Im creating this program which asks the user to enter the amount of items they are purchasing then input the cost per item. The cost is then saved into an array which is used for later. Then the program asks if the user has any coupons. The user then enters the amount of money the coupon knocks off. If the amount of the coupon is greater than the cost of the item it should show an error.

Where my problem lies is, when i try to compare the cost of the item that i have saved in arr1, it shows the cost is zero.

Im not sure why it's not displaying the correct number. and to make sure i knew the number i had it display the number that is there in the fillCouponArray function.

how can i retrieve the element of the array for comparison.

This is like my second time with arrays so please be understanding.

.data
    prompt1: .asciiz "How many items are you purchasing? \n"
    prompt2: .asciiz "\nPlease enter the price for item "
    prompt3: .asciiz ": "
    prompt4: .asciiz "Please enter the number of coupons that you want to use:\n"
    prompt5: .asciiz "Please enter the amount of coupon "
    prompt6: .asciiz "\nYour total charge is: $"
    prompt7: .asciiz "\nThank you for shopping with us."
    
    error1: .asciiz "There are too many items that you are purchasing\n"
    error2: .asciiz "Price must be greater than 0"
    error3: .asciiz "Too many coupons!!\n"
    error4: .asciiz "This coupon is unacceptable\n"
    
    arr1: .word 0,0,0,0,0,0,0,0,0,0
    arr2: .word 0,0,0,0,0,0,0,0,0,0
    
.text   

main:
    la $a2, arr1
    la $a3, arr2
    
    li $v0, 4
    la $a0, prompt1     #prints prompt1
    syscall
    
    li $v0, 5
    syscall         #asks for user input
    
    add $s0, $v0, $0    #store input to $s0
    add $a1, $s0, $0    #pass the argument to $a1
    bgt $s0, 20, error  #branch if greater than 20
    blt $s0, 0, error   #branch if less than 0
    
    jal loop        #jump and link to loop
    
coupon:
    li $v0, 4
    la $a0, prompt4     #print prompt2
    syscall 
    
    li $v0, 5
    syscall
    
    add $s1, $v0, $0        #save input to $s1
    add $a1, $s1, $0        #pass argument to $a1
    bgt $a1, $s0, couponError2  #if its greater than total products, then error
    
    jal loop2
    
    jal subtract
    
    li $v0, 10
    syscall
    
error:
    li $v0, 4
    la $a0, error1
    syscall         #print error message
    
    j main
    
priceError:
    
    li $v0, 4
    la $a0, error2
    syscall         #print error message
    
    j fillPriceArray

couponError:
    li $v0, 4
    la $a0, error4
    syscall         #print error message
    
    j fillCouponArray
    
couponError2:
    li $v0, 4
    la $a0, error3
    syscall         #print error message
    
    j coupon
    
    
loop:
    add $t1, $0, $a1    #put $a1 to $t1
    addi $t0, $0, 1     #create a counter $t0
    addi $t3, $0, 0

fillPriceArray: beq $t3, $t1, exit
    
    li $v0, 4
    la $a0, prompt2     #print prompt2
    syscall 
    
    li $v0, 1
    move $a0, $t0       #print number
    syscall
    
    li $v0, 4
    la $a0, prompt3     #print prompt 3
    syscall
    
    li $v0, 5
    syscall         #ask for user input
    
    add $t2, $v0 ,$0
    
    blt $t2, 0, priceError
    
    sw $t2, 0($a2)  #store input to arr1
    addi $a2, $a2, 4
    
    add $s4, $t2, $s4
    
    addi $t0, $t0, 1
    addi $t3, $t3, 1
    
    j fillPriceArray
    
loop2:      #using this to re-initialize all values so i can use them again
    move $t1, $0        
    move $t2, $0
    move $t3, $0        
    move $t0, $0
    
    addi $t3, $0, 0
    addi $t0, $0, 1 
    add $t1, $0, $a1
    
    
    j fillCouponArray
    
fillCouponArray: beq $t3, $t1, exit

    la $t5, 0($a2)
    addi $a2, $a2, 4
    
    li $v0, 1
    lw $a0, ($a2)
    syscall
    
    li $v0, 4
    la $a0, prompt5     #print prompt5
    syscall 
    
    li $v0, 1
    move $a0, $t0       #print number
    syscall
    
    li $v0, 4
    la $a0, prompt3     #print prompt 3
    syscall
    
    li $v0, 5
    syscall         #user input
    
    add $t2, $v0, $0    #save user input to $t2
    
    bgt  $t2, $t5, couponError  #if $t2 is greater than $t5 error
    
    sw $t2, 0($a3)      #save to element in array
    addi $a3, $a3, 4    #add to get to next element
    addi $t3, $t3, 1
    
    add $s5, $t2, $s5
    
    addi $t0, $t0, 1        #add for counter
    
    j fillCouponArray

subtract:
    sub $t6, $s4,$s5
    
    li $v0, 4
    la $a0, prompt6
    syscall
    
    li $v0, 1
    move $a0, $t6
    syscall
    
    li $v0, 4
    la $a0 , prompt7
    syscall

exit:
    jr $ra


Read more here: https://stackoverflow.com/questions/64397883/why-is-this-value-not-the-value-that-should-be-in-the-array-mips

Content Attribution

This content was originally published by Piklez at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: