This code is supposed to store the numbers in an array, and then print the total of positive numbers, and total of negative numbers. I am not sure what I am doing wrong! It prints out the messages without the total. I am using RISC-V assembly language in RARS simulator
This is my current outcome:
Positive Integers Total:
Negative Integers Total:
Negative Integers Total:
-- program is finished running (0) --
Does not display the total values. How can I fix it?
#----Calculate the Sum of Positive and Negative Numbers In An Array----#
########################################################################
.data
array: .word 22,-35,48,10,-15,-30,25,20,-1,-26,-18,1,2,3,4,5,6,7,-9,1,-4,-3,-2,1,6,0 #array input
positiveSum: .word 0
negativeSum: .word 0
#outcome messages
position: .word 0 #index posisiont of array
length: .word 9
posTotal: .ascii "\nPositive Integers Total: "
negTotal: .ascii "\nNegative Integers Total: "
.text
main:
la t0,array #load array to register t0
lw t1,position
lw s1, positiveSum #load the positive sum to s1, and negative sum to s2
lw s2,negativeSum
loop:
#calculate the index position in terms of memory
#each integer needs 4 bytes
#so shift left the value in t1 and store it in t3
slli t3,t1,2
add t3,t3,t0 #add the base address and the above result
lw t4,0(t3) #load the word from above address into t4
beqz t4,exit #exit loop when reaching 0
j checkSign #if value is not 0 jump to check integer sign
checkSign:
bltz t4,addNegative #check if array value is negative
j addPositive # if yes goto addnegative
#if not goto addpositive
addPositive:
add s1,s1,t4 #add all positive integers to s1 register
j increment #increment
addNegative:
add s2,s2,t4 #add the negative integers to the s2 register
j increment #increment
increment:
addi t1,t1,1 #increment the current index in t1 by 1
j loop
exit: #end of program
la a0,posTotal
li a7,4 #print the positive sum
ecall
la a0,negTotal
li a7,4 #print the negative sum
ecall
ori a7, zero, 10 #program exit system call
ecall # exit program
You are printing only posTotal and negTotal. You need to print also positiveSum and negativeSum after converting them to ascii format.
I don't known how your ecall works , but you need to add two calls to ecall when you put the ascii from of your Sums in a0 (this solution will always work), or if the ecall can take more than one argument place the Sum as the second argument by using a1.
You got Negative Integers Total:
printed twice because you did not add \\0
at the end of your strings, so the first time you print the first string but continue printing until you found an \\0.
I recommend you add an alignment directive between your strings like .balign 4
.
posTotal: .ascii "\nPositive Integers Total: \0"
.balign 4
negTotal: .ascii "\nNegative Integers Total: \0"
the j checkSign
and the second j increment
are useless.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.