I have this assembly project (mips) but I think I have an overflow problem....
The problem is that the answer is ok until number 99 but at 100 it prints with a - infront of the number. The projects just ask me to calculate a sum (x+a)^2 (x= 1 to 100) and a= (1+2y) (y= 1 to x)
The answer i get is:
Result: -2088573816
I post the code:
.data # data segment. We Define the Constants here
intro: .asciiz "\n\t\t\t\tCalculate Sum-Test\n\n"
msgIn: .asciiz "\nPlease give an integer between 1 - 100: "
error: .asciiz "\nInvalid Number: Integer must be between 1-100\n\n"
thx: .asciiz "\n\nThank you for using our program"
res: .asciiz "\nThe result is: "
mo: .asciiz "\nThe average is: "
rest: .asciiz "\nThe modulo is: "
######################################…
.text # text segment
.globl main
main:
#constants
li $t0, 100
li $t1, 1
li $t2, 2
#counters
li $t3, 1 #counter for the first loop
li $t4, 1 #counter for the second/nested loop
#sum
li $s1, 0 #$s1 is the a value
li $s2, 0 #$s2 holds the final result
# Print on consule the intro message
la $a0, intro # Message that indicates the title of the current program
li $v0, 4 # System code for print string
syscall # Print String
j loop1
error_msg:
# Print on consule the error message
la $a0, error # Message that indicates the error of the current program
li $v0, 4 # System code for print string
syscall # Print String
loop1:
# Print on consule the instruction message
la $a0, msgIn # Message that indicates the instructions of the current program
li $v0, 4 # System code for print string
syscall # Print String
#Read an integer from keyboard
li $v0, 5 # System code for reading an integer
syscall # Read an integer
move $s0,$v0 # Save integer in $s0
# By this time the value should be in $s0 register
######################################…
# This is code that checks if the integer is between 1 - 100
######################################…
#Checks if integer>100
bgt $s0, $t0, error_msg #pseudoinstruction, $s0>$t0 (user_int>100)
#Checks if integer<1
blt $s0, $t1, error_msg #pseudoinstruction, $s0<$t1 (user_int<1)
######################################…
# This is code generates the a value of the serie
######################################…
#This loop calculates the final result
for_loop_x:
#Checks if first_loop_counter<user_value
bgt $t3, $s0, loop_out #pseudoinstruction, $t3<$s0 (counter<user_int)
for_loop_a:
#Checks if second_loop_counter<first_loop_counter
bgt $t4, $t3, calculate #pseudoinstruction, $t4<$t3 (second_loop_counter<first_loop_…
#Calculate the a value
mulou $t5, $t2, $t4 #multiply $t5=$t2*$t4 ($t5= 2*second_loop_counter => 2*y)
addiu $s5, $t5, 1 #add immitiade $s5=$t5+1 ($s5= 1+$t4 => 1+2*y)
addu $s1, $s1, $s5 #add immitiade $s1=$s1+$s5 ($s1= $s1+$s5 => sum=prev_value+(1+2*y)) ==> This is the a value
addiu $t4, $t4, 1 #increase counter by one ($t4=$t4+1)
j for_loop_a #Check the condition again
calculate:
addu $t6, $t3, $s1 #add $t6=$t3+$s1 ($t6= first_loop_counter+a value => x+a)
mulou $t7, $t6, $t6 #calculate square root $t7=$t6*$t6 (((x+a)^2)
addu $s2, $s2, $t7 #calculate the final sum $s2=$s2+$t7
addiu $t3, $t3, 1 #increase counter by one ($t3=$t3+1)
j for_loop_x #Check the condition again
loop_out:
# Print on consule the result message
la $a0, res # Message that indicates the results of the current program
li $v0, 4 # System code for print string
syscall # Print String
#Printing results on consule
move $a0, $s2 #moves the final result to the argument 1
li $v0, 1 #System code for printing an integer
syscall #Print Integer
end:
######################################…
# Print on consule the "thank you" message
la $a0, thx
li $v0, 4
syscall
#Exiting system
li $v0, 10 # exit system call
syscall
thanks in advance.