MIPS Values arent able to be accessed from memory - arrays

So the issue is that whenever i try to access the integers placed into the array which seems to work perfectly and theyre even saved to an address of +0 and +4 respectively and so on for the amount of values there are but when i attempt to take them out to use them for sorting purposes they're seemingly not being loaded into the $t4 and $t5 values. below is my code im using for this, why wont the values transfer when loading them from 0,4($s1) where they are placed?
.data
prompt1: .asciiz "Enter the array size \n"
prompt2: .asciiz "Enter integers to fill the array \n"
prompt3: .asciiz "Sorted Array: "
.text
main:
li $t0, 0
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0 #array size
add $t9, $t0, $t0
li $v0, 9
la $a0, ($t0)
syscall
move $s0, $v0 #array address w/ size in memory
add $s1, $zero, $s0
loop:
la $a0, prompt2
li $v0, 4
syscall
li $v0, 5
syscall
beqz $t0, sort
sw $v0, 0($s1)
addiu $s1, $s1, 4
subiu $t0, $t0, 1
bnez $t0, loop
sort:
lw $t4, 0($s1)
lw $t5, 4($s1)
blt $t5, $t4, swap
addiu $s1, $s1, 4
subiu $t9, $t9, 1
bnez $t9, sort
j exit
swap:
sw $t4, 4($s1)
sw $t5, 0($s1)
exit:

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

Copying an array for sorting not working correctly in MIPS program?

I'm working on a sorting program that sorts an array of integers, for which I copy the array entered in by the user into a new array to be mutated. However, I'm not sure if I'm doing it correctly.
.globl main
.data
input: .asciiz "Enter the size of the array: \n"
entries: .asciiz "Enter the elements of the array, one line at a time: \n"
output: .asciiz "Original array and then sorted array: \n"
space: .asciiz " "
.text
main:
subi $sp, $sp 32
sw $ra, 0($sp)
sw $t0, 4($sp) # the size of the array
sw $t4, 8($sp) # the number 4
sw $t1, 12($sp) # temporary
sw $t2, 16($sp) # array original
sw $t3, 20($sp) # specific element
sw $s1, 24($sp) # copied array
sw $t5, 28($sp) # number to copy
la $a0, input
li $v0, 4
syscall
# get the size
li $v0, 5
syscall
move $t0, $v0
# allocate space for the array on the heap
li $t4, 4
mul $t1, $t0, $t4
li $v0, 9
move $a0, $t1
syscall
move $t2, $v0
li $s0, 0
la $a0, entries
li $v0, 4
syscall
read_array:
# read element
li $v0, 5
syscall
move $t3, $v0
# place in right address
mul $t1, $s0, $t4
add $t1, $t2, $t1
sw $t3, 0($t2)
addi $s0, $s0, 1
blt $s0, $t0, read_array
li $s0, 0
gnome_sort:
# allocate space on heap for copy
mul $t1, $t0, $t4
li $v0, 9
move $a0, $t1
syscall
move $s1, $v0
mul $s2, $t4, $t0
add $s3, $s1, $s2
copy_array:
lw $t5, 0($t2)
sw $t5, 0($s1)
add $t2, $t2, $t4
add $s1, $s1, $t4
blt $s1, $s3, copy_array
li $s0, 0
while_loop:
bgt $s0, $t0, finish_sort
beq $s0, $zero, increase_i
sw $s4, 0($s1)
sw $s5, -4($s1)
bge $s4, $s5, increase_i
j swap_elements
increase_i:
addi $s0, $s0, 1
j while_loop
swap_elements:
la $a0, input
li $v0, 4
syscall
sw $t6, 0($s1)
sw $t7, -4($s1)
lw $t7, 0($s1)
lw $t6, -4($s1)
subi $s0, $s0, 1
j while_loop
finish_sort:
li $s0, 0
la $a0, output
li $v0, 4
syscall
j print_original
print_original:
bge $s0, $t0, print_sorted
lw $s6, 0($t2)
li $v0, 1
move $a0, $s6
syscall
la $a0, space
li $v0, 4
syscall
addi $s0, $s0, 1
j print_original
print_sorted:
li $s0, 0
loop:
bge $s0, $t0, finish
lw $s6, 0($s1)
li $v0, 1
move $a0, $s6
syscall
la $a0, space
li $v0, 4
syscall
addi $s0, $s0, 1
j loop
finish:
li $v0, 10
syscall
After testing this in QTSpim, it seems that you're going out of allocated memory on the line sw $s4, 0($s1)
in the while_loop. This is because $s1 at that point is past the end of the second array you allocated. If you want to use that memory, you will have to syscall for it.
The way you copy the array using lw and sw is correct.

MIPS (arrays and loops)

so i am working on a project which asks the user for numbers (integers) and than rearranges them in ascending order and prints them out.
now i am stuck because i want to also print them in descending order but in a separate array with the same numbers. can anyone help me out?
this is my code so far for ascending numbers:
(btw the comments are just for my guiding)
.data
array1: .space 100
array2: .space 100
array3: .space 100
msg1: .asciiz "Enter at least 4 integers: Enter the number 500 to exit \n"
msg2: .asciiz "Numbers you entered are: \n"
msg3: .asciiz "The numbers you entered in ascending order are: \n"
commas: .asciiz ","
msg4: .asciiz "The numbers you entered in descending order are: \n"
.text
.globl main
main:
la $a1, array1 #load pointer to array1
la $a2, array2 ##load pointer to array2
la $a3, array3 #load pointer to array3
li $t1, 500 # once 500 is entered it will exit the input and produce an output
li $t0, 0
loopset:
la $a0, msg1 #loads msg1 text into $a
li $v0, 4 #loads 4 into $v0 (prints string)
syscall
li $v0, 5 #loads 5 into $v0 (read interger)
syscall
beq $v0,$t1,swap
addi $t0,$t0,4 #add 4 to $t0, save to $t0
sw $v0, ($a1) #stores input into array
addi $a1, $a1,4 #add 4 to $a1, save to $a1
j loopset
swap:
la $t4, array1 #loads array1 to $t4
la $t1, array1 #loads array1 to $t1
addi $t1, $t1, 4 #add 4 to $t1, save to $t1
la $t8, array1 #loads array to $t8
add $t8,$t0,$t8 #add $t8 to $t0, save to $t8
la $t9,array1 #loads array1 to $t9
add $t9, $t0, $t9 #add $t9 to $t0, save to $t9
addi $t9, $t9, -4 #subtracts 4 from $t9, save to $t9
loop1:
lw $t2, ($t4) #load input into $t2
lw $t3, ($t1) #load input into $t3
blt $t2, $t3, loop2 #if $t2 > $t3, go to loop2
sw $t3, ($t4) #store $t3 in $t4
sw $t2, ($t1) #store $t2 in $t1
loop2:
addi $t1, $t1, 4 #add 4 to $t1, save to $t1
blt $t1, $t8, loop1 #if $t1< $t8, go to loop1
addi $t4, $t4, 4 #add 4 to $t4, save to $t4
move $t1, $t4
addi $t1, $t1, 4 #add 4 to $t1, save to $t1
blt $t4, $t9, loop1 #if $t4 < $t9, to go loop1
print:
la $a1, array1 #loads array to $a1
la $a0, msg3 #loads msg3 to $a0
li $v0, 4
syscall
la $a0, array1 #loads array1 to $a0
li $v0, 4 #loads 4 into #v0
syscall
loop3:
blez $t0, EXIT #if $t0 <= 0, go to EXIT
li $v0, 1 #loads 1 into $v0
lw $a0, 0($a1) #load an input into $a0
syscall
la $a0, commas #loads commas into $a0
li $v0, 4 #loads 4 into $v0
syscall
addi $a1, $a1, 4 #add 4 to $a1, save to $a1
addi $t0, $t0, -4 #subtracts 4 from #t0, save to $t0
j loop3
EXIT:
li $v0,10 #exit
syscall
Try changing out all of the blt (branch less than) to bgt (branch greater than) and assuming your program works like it is supposed to you should get an array in descending order. If you want it in a different array for the descending order you will need to copy your loop1, loop2, loop3, and print and change their names switching out the blt calls for bgt calls.

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