User ask how many input you want to take and then show their sum in mips - loops

I want to make a program in mips, in which the user will enter the number of inputs he wants to take. Finally the program will print the sum of the inputs.
Here is my code:
.data
myMessage: .asciiz "ENTER numbers you want to sum\n"
value: .asciiz "ENTER Value \n"
sum : .word 0
.text
li $v0, 4
la $a0, myMessage
syscall
li $v0, 5
syscall
move $t0, $v0 #num of time user will enter num
la $t1, 0 #count value first initiallize to 0
see:
bne $t1,$t0,add #checking if count is less than the num of value
li $v0, 1 #printing sum finally
la $a0, ($s2)
add:
li $v0,4
la $a0,value
syscall
li $v0,5
syscall
move $t3,$v0
la $a1, sum #load address of 'bal' in '$a1'
lw $s3, 0($a1) #load sum from '$a1' to '$s2' (initially 0)
add $s3, $s3, $t3 #adding the sum
sw $s2, 0($a1) #load latest sum ('$s2') in .word balance ('$a1')
addi $t1,$t1,1 inc in count
j see
The problem is that the program does not stop after the wished number of inputs and continues to ask for new input.

.data # Data declaration section
instring1: .asciiz "ENTER numbers you want to sum\n"
instring2: .asciiz "Enter value\n"
outstring: .asciiz "Sum: "
.text#
main:
#Print instring1
li $v0, 4 # system call code for printing string = 4
la $a0, instring1 # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
#Taking n as input
li $v0, 5 # read int
syscall
move $s0, $v0 # the result of the syscall is stored in v0
# we move it to t0 to prevent overwriting
#Creation heap memory
mul $t1, $s0, 4
li $v0, 9
move $a0, $t1
syscall
move $s1, $v0
#Print instring2
li $v0, 4 # system call code for printing string = 4
la $a0, instring2 # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
li $s2, 0 # $s2 is the index, and loop induction variable
Start_Input:
bge $s2, $s0, End_Input
li $v0, 5 # Read integer values
syscall
mul $t0, $s2, 4 # $t0 is the offset
add $t1, $s1, $t0 # $t1 is the address of desired index
sw $v0, ($t1) # store the value in the array
addi $s2, $s2, 1 # increment the index
j Start_Input
End_Input:
add $t0, $zero, $zero # i is initialized to 0, $t0 = 0
add $s2, $zero, $zero # sum = 0
beq $s0, $zero, print_sum # if number = 0 then goto print_sum
Loop: #stuff
mul $t1, $t0, 4
add $t2, $s1, $t1
lw $t3, ($t2)
add $s2, $s2, $t3
addi $t0, $t0, 1 # i ++
slt $t1, $t0, $s0 # $t1 = 1 if i < n
bne $t1, $zero, Loop # go to Loop if i < n
#Print Sum
print_sum:
li $v0, 4 # system call code for printing string = 4
la $a0, outstring # load address of string to be printed into $a0
syscall # call operating system to perform operation in $v0
# syscall takes its arguments from $a0, $a1,
li $v0 1 # print int
move $a0 $s2
syscall
#Exit Syscall
EXIT:
li $v0 10# exit
syscall
This is working code. Comment provided to understand code.

.data
myMessage:.asciiz "ENTER numbers you want to sum\n"
value:.asciiz "ENTER Value: "
show: .asciiz "\nSum is: "
.text
li $v0,4
la $a0,myMessage
syscall
li $v0,5
syscall
move $t0,$v0 #num of time user will enter num
la $t1, 0 #count value first initiallize to 0
la $t5, 0
see:
bne $t1,$t0,add #checking if count is less than the num of value
li $v0, 4
la $a0, show
syscall #print message "Sum is: "
li $v0, 1
move $a0, $t5
syscall #print the result
j end
add:
li $v0,4
la $a0,value
syscall
li $v0, 5
syscall
add $t5, $t5, $v0
sub $t0, $t0, 1
j see
end:

Related

MIPS array assign infinity loop

I am trying to fix this problem for hours!!!!!
When I enter 8 integers for the array and then enter 0 to finish reading, when the loop section starts it goes to infinity loop!!
I can't find where's the problem, when I remove the addiu from the $s2 array it works fine but doesn't store the $t1 on the array!
Please HELP!!
.data
array: .space 32
percentage_Array: .space 32
newLine: .asciiz "\n"
.text
main:
la $s1, array
la $s2, percentage_Array
read_numbers:
li $v0, 5
syscall
sw $v0, 0($s1)
addiu $s1, $s1, 4
beqz $v0, continue
j read_numbers
continue:
la $s1, array
loop:
lw $t0, 0($s1)
addiu $s1, $s1, 4
beqz $t0, exit
move $a1, $t0
jal calculate_Ones_and_Bits
move $s6, $v0
move $s7, $v1
#put the total number of ones
add $s0, $s0, $s7
#calculate the percentage
addi $t5, $zero, 100
mult $s7, $t5
mflo $t1
div $t1, $s6
mflo $t1
#-----I THINK HERE IS MY PROBLEM------
#Put the percentages on percentage_Array array
sw $a1, 0($s2)
addiu $s2, $s2, 4
#Check which percentages are greater than 50 and print the numbers
which fulfill the condition
slti $t2, $t1, 50
beq $t2, 1, loop
li $v0, 1
move $a0, $t0
syscall
#lines 66,67,68 are extra just for printing in new line more clearly
li $v0, 4
la $a0, newLine
syscall
j loop
exit:
#lines 73,74,75 are also extra just to print the value of s0, it is
printed here so it
#will be executed after the loop has finished
li $v0, 1
move $a0, $s0
syscall
#tell the OS to exit/finish
li $v0, 10
syscall

Array issue in MIPS using mars

Here is my code and the error is in either reading or writing in the array. This has stumped me for quite awhile now, slowly getting better but not perfect.
One example output is the following:
please enter a number to test it's perfectness, or enter 0 to quit
8
Factors are:
124665658
The sum is:
7
This value is
Please enter a number to test it's perfectness, or enter 0 to quit
.data
isPerfect: .asciiz "\nThis Value is Perfect!\n"
isNotPerfect: .asciiz "\nThis value is not perfect :(\n"
negNum: .asciiz "\nPlease only enter positive values \n"
enterNum: .asciiz "\nPlease enter a number to test it's perfectness, or enter 0 to quit \n"
factors: .asciiz "\nFactors are: \n"
sum: .asciiz "\n The sum is: \n"
intVal: .word 0
.text
#DOCUMENTATION OF REGISTERS
#######################################################################################
#s0- user input
#s1- iterator
#s2 - iteration amount
#s3 - s0/2
#s4 - Remainder
#s5 - total
#Find the factors and store them in array add them up and compare them to $s0
#######################################################################################
#main
#prompts the user then goes to loop
#Also declares variables that should be reintialized upon a new user value
.globl main
main: move $s6, $zero
la $t0, factorArray
#variables declared prior to loops
li $s2, 1
li $s3, 1
li $s5, 0
li $v0, 4
la $a0, enterNum
syscall
###########################################
#post main Values
# $s6 - 0
# $t0 - address of factorArray
# s2, s3, s5 look at code
###########################################
loop1:
#retrieve input from user and store it in $s0
li $v0, 5
li $s1, 1
syscall
move $s0, $v0
div $s3, $s0, 2
li $s1, 0
#Branch on greater than zero(Breaks down factors)
bgtz $s0, factor
#Brench on less than zero (Tells user they are bad and jumps back to loop)
bltz $s0, negative
#Branch on zero (End)
b end
###########################################
#Post loop1 Values
#S1 - 1
#s0 - user input number
###########################################
factor:
add $s1, $s1, $s2 #iterate
rem $s4, $s0, $s1 #Get the remainder of the iterator and the user number
beq $s4, $zero, storeValue #if remainder is 0 store the value in the array
ble $s1,$s3, factor #branch if iterator is less than s0/2
b print
###########################################
#post factor values
#$s4 is remainder of User inputer and iterator $s1
#$s3 Division User input, $s0 and 2 because no factors are < 2
###########################################
#shove factors in array
storeValue:
add $s5, $s1, $s5
sw $s1, 100($t0)
addi $t0, $t0, 4
addi $s6, $s6, 1
b factor
###########################################
#post Storage values
#s5 is our total
#store iteration from $s1 in array
#incriment $t0 and %s6 both being address and place holder incrementations
###########################################
#to print the Array
print:
la $t0, factorArray
li $v0, 4
la $a0, factors
syscall
###########################################
#post print values
#$t0 - address of array
#drop our factors prompt
###########################################
for: blt $s0, $s1, printSum
li $v0, 1
lw $a0, 100($t0)
syscall
addi $t0, $t0, 4
addi $s6, $s6, 1
addi $s1, $s1, 1
b for
###########################################
#checks if $s0 and $s1 are equal
#Increments t0 and s6 and s1
###########################################
# Prints sum and decides if the number is perfect and branches accordingly #
printSum:
li $v0, 4
la $a0, sum
syscall
#print total here
li $v0, 1
sw $s5, intVal
lw $a0, intVal
syscall
#perfect branch
beq $s5, $s0, isPerfectImpl
#not Perfect Branch
b isNotPerfectImpl
###########################################
#post printSum values
#Loads contents of $t2 into inval
#needs re worked
###########################################
isPerfectImpl:
li $v0, 4
la $a0, isPerfect
syscall
b main
isNotPerfectImpl:
li $v0, 4
la $a0, isNotPerfect
syscall
b main
#Done
#Tels the user they are bad and jumps back to main to re prompt the user
negative:
li $v0, 4
la $a0, negNum
syscall
b main
end: li $v0, 10
syscall
My output is the following and i am not really sure why

MIPS - Error when printing data into array

So im attempting to put numbers 1-200 in an array. The array is at the bottom of the code. I keep getting errors when i try to print this, can anyone spot the error with me putting in the numbers? The numbers in the array are supposed to go from 1-200, so $t3 increments each time. Any advice would be greatly appreciated.
main:
.text
lw $t0, numbers_size # loads the size of the array into the loop
la $t1, numbers # $t1 gets the number array
li $t2, 0 # loop control variable
li $t3, 1 # value stored inside numbers
li $v0, 4
la $a0, test_2 # print before loop
syscall
number_for:
bge $t2, $t0, end_for
sw $t3, ($t1)
add $t1, $t1, 4
add $t2, $t2, 1
add $t3, $t3, 1
j number_for
end_for:
lw $t0, numbers_size # loads the size of the array into the loop
la $t1, numbers # $t1 gets the number array
li $t2, 0 # loop control variable
j print_numbers
.data
test: .asciiz "Printing numbers:"
test_2: .asciiz "Before loop:"
numbers:
.word 0:200
numbers_size:
.word 200
This may help you:
.text
main:
lw $t0, numbers_size # loads the size of the array into the loop
la $t1, numbers # $t1 gets the number array
li $t2, 0 # loop control variable
li $t3, 1 # value stored inside numbers
li $v0, 4
la $a0, test_2 # print before loop
syscall
j number_for
li $v0, 10
syscall
number_for:
bge $t2, $t0 end_for
sw $t3, ($t1)
addi $t1, $t1, 4
addi $t2, $t2, 1
addi $t3, $t3, 1
b number_for
end_for:
lw $t0, numbers_size # loads the size of the array into the loop
li $t1, 0
li $t2, 0 # loop control variable
buc:
bge $t2, $t0 end
lw $a0, numbers($t1) # Printing integer from array
li $v0,1
syscall
la $a0, space #load a space: " "
li $v0, 4 #print string
syscall
addi $t1, $t1, 4
addi $t2, $t2, 1
b buc
end:
jr $ra
.data
test: .asciiz "Printing numbers:"
test_2: .asciiz "Before loop:\n"
space: .asciiz " "
numbers_size:
.word 200
numbers:
.word 0:200

Adding to an array in a subroutine(MIPS/Assembly)

I'm trying to add values to an array inside a subroutine. I can get the first valid value(positive number and divisible by 3) into the array, but the next valid entries don't work. I can enter a valid number and then enter an invalid number and the program works fine, but two valid numbers makes Spim stop working. I've spent a few hours trying to figure it out but no luck. The jumping from one subroutine is a requirement for the assignment, I have a working program but lacks all the unnecessary(in my opinion) subroutines.
.data
array1: .word 80
EnterARVal: .asciiz "Please enter a number:\t"
space: .asciiz " "
errormessage: .asciiz "*****Error: "
notpos: .asciiz " is not a positive number.\n"
notdiv3: .asciiz " is not divisible by 3.\n"
numadded: .asciiz " added to array.\n"
EnterElem: .asciiz "Enter number "
ARReverse: .asciiz "The contents of the array in reverse orders is:\n"
InvalidAR: .asciiz "Invalid number of array elements, please try again.\n"
.text
main:
la $s0, array1 #array1 pointer
li $t0, 1
begin:
jal readNum #go to readNum subroutine
add $a0, $0, $v0 #stores readNum input to $a0
jal verifySize #jumps to verifySize subroutine
add $t1, $v1, $0 #stores 0 or 1 value to $t1
beq $t1, $0, begin #starts over if t1 is 0 or false
beq $t1, $t0, numok #goes to numok if t1 is 1 or true
numok: add $a0, $0, $a0
add $a1, $0, $s0
jal createArray
j exit
readNum: li $v0, 4
la $a0, EnterARVal
syscall
li $v0, 5
syscall
add $v0, $v0, $0
j $ra
verifySize: add $t1, $0, $a0
li $t2, 20
li $t3, 1
li $t4, 0
li $t5, 1
slt $t6, $t1, $t3
beq $t6, $t3, toolow
sgt $t7, $t1, $t2
beq $t7, $t3, toohigh
beq $t7, $t4, oknum
oknum:
add $v1, $t5, $0
j $ra
toolow:
li $v0, 4
la $a0, InvalidAR
syscall
add $v1, $t4, $0
j $ra
toohigh:
li $v0, 4
la $a0, InvalidAR
syscall
add $v1, $t4, $0
j $ra
createArray: add $s1, $a0, $0
add $s0, $a1, $0
li $t0, 0 #counter
li $t2, 1
add $a0, $s1, $0
li $v0, 1
syscall
makingarray: beq $t0, $s1, arraydone
jal readNum #go to readNum subroutine
add $a0, $v0, $0 #stores number from readNum to $a0
jal checkNumPositive #jump to checkNumPositive subroutine
add $t1, $v0, $0
beq $t1, $0, positivenum #if number is positive go to positivenum
beq $t1, $t2, notpositive
positivenum:
jal divisibleBy3
add $t4, $v0, $0
beq $t4, $0, notdivisibleby3
sw $a0, 0($s0)
li $v0, 1
syscall
li $v0, 4
la $a0, numadded
syscall
add $s0, $s0, 4
add $t0, $t0, 1
j makingarray
arraydone:
add $v0, $s0, $0
j $ra
notpositive:
li $v0, 4
la $a0, notpos
syscall
j makingarray
notdivisibleby3:
li $v0, 4
la $a0, notdiv3
syscall
j makingarray
#reverseArray:
divisibleBy3: add $t0, $a0, $0
li $t1, 3
div $t0, $t1
mfhi $t2
mflo $t3
seq $t4, $t2, $0
add $v0, $t4, $0
j $ra
checkNumPositive: li $t0, 0
slt $t1, $a0, $0 #set t1 to 1 if number is less than 0
add $v0, $t1, $t0
j $ra
exit: li $v0, 10
syscall
Any tips with how I can fix createArray is appreciated, thanks.
Your primary problem is you used .word 80 which only reserves a single word with value 80. You probably meant .space 80 to reserve space for up to 20 words (which seems to be the limit enforced in your code).
Further problem is you are not following conventions about which registers need to be preserved.
For example, you use $t0 as counter in createArray and that's not preserved across subroutines, not by convention and de facto not by your code (both divisibleBy3 and checkNumPositive destroy it).
Similar problem with not properly preserving $ra in nested subroutine calls, as such the return address for createArray is overwritten by the subroutines invoked from there.
I assume the intent of the assignment was to teach you about these nuances.

printing of integers mips

I try to add numbers to a array and then I try to print those numbers, but when I add mine numbers and I try to print and then I got different numbers. How is that possible?
My code is:
#----------------------------------- Array Vullen -----------------------------------------------------
.data
question1_msg: .asciiz "How much integer do you want to give?\n"
question2_msg: .asciiz "give a number?\n"
.text
question_numbers:
la $a0, question1_msg #load the question in $a0
li $v0, 4
syscall
answer_numbers:
li $v0, 5 #read the answer of previous question
syscall
move $t0, $v0
move $t9, $t0
move $t8, $t0
generate_array:
sll $t0, $t0, 2 #create array
move $a0, $t0
li $v0, 9
syscall
move $t3, $v0 #put the stack pointer in a temperay register
move $t4, $v0
add_numbers_array:
bge $t1, $t9, Call_procedure # if $t1 >= $t0 then exit
#ask questions
la $a0, question2_msg #load the question in $a0
li $v0, 4
syscall
#read numbers
li $v0, 5
syscall
move $t2, $a0
#add number en go to the next array point
sw $t2, ($t3)
add $t3, $t3, 4
add $t1, $t1, 1
#get back to the begin of the loop
b add_numbers_array
#-------------------------------------Array Printen------------------------------------------------
Call_procedure:
li $t1, 0
la $a1, ($t8) # load the couple of numbers
la $a2, ($t4) # load the starting adress of the array
jal Print
b exit
Print:
bge $t1, $a1, return # if $t1 >= $t0 then exit
lw $t2, ($a2) #load integer and print
move $a0, $t2
li $v0, 1 # print the number in the array
syscall
addu $a2, $a2, 4 #increase the sp
addi $t1, $t1, 1 #increase the number printed
b Print
return:
jr $ra
exit:
li $v0 , 10 # let the code end
syscall
I see 2 errors:
1.
#read numbers
li $v0, 5
syscall
move $t2, $v0
This should be $v0, not $a0
2.
move $a1, $t8
move $a2, $t4
instead of
la $a1, ($t8) # load the couple of numbers
la $a2, ($t4) # load the starting adress of the array

Resources