Here is my MIPS code. It is supposed to input 10 integers in array and then output them. I can input integers but after entering 10 integers it doesn't output any array element.
.data
arr: .space 40
.text
.globl main
main:
la $t0, arr #copying array address into $t0
iterateForInput:
blt $t4, 9, forLoopInput
sub $t0, $t0, 40
iterateForOutput:
blt $t4, 9, forLoopOutput
li $v0, 10
syscall
forLoopInput:
li $v0, 5 #input integer
syscall
move $t1, $v0 #moving integer into $t1
sw $t1, ($t0) #storing integer at array address
add $t0, $t0, 4
add $t4, $t4, 1
b iterateForInput
forLoopOutput:
lw $t6, ($t0)
li $v0, 1
move $a0, $t6
syscall
add $t0, $t0, 4
add $t4, $t4, 1
b iterateForOutput
Related
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
i have to make a program that fills an array with 30 integers entered from keyboard.Then the user type 'c' to copy the array to an other array.i've done with the first step but i cant manage to copy the array to another.
Here is my code
.data
msg1: .asciiz "> "
msg2: .asciiz "type 'c' to copy \n>"
.align 2
array: .space 400
.text
main:
la $t3 array
loop:
la $a0, msg1 #output message 1
li $v0, 4
syscall
li $v0, 5 #read integer input
syscall
move $t0, $v0
beq $t0, -99, endloop #loop until user types -99
beq $t1,30,endloop #get user input up to 30 times
addi $t1, $t1, 1 #counter
sw $t0,($t3)
addi $t3,$t3,4
b loop #loop until it reaches 30
endloop:
la $a0, msg2 #output message 2
li $v0, 4
syscall
li $v0, 12 #read character input
syscall
beq $v0, 'c', COPY
j NEXT
COPY:
NEXT:
The most primitive way to do it is to
la $t1, dst_array
la $t3, src_array
addu $t0, $t3, 30*4 # setup a 'ceiling'
copy_loop:
lw $at, 0($t3)
sw $at, 0($t1)
addu $t1, $t1, 4
addu $t3, $t3, 4
blt $t1, $t0, copy_loop # if load pointer < src_array + 30*4
However, some implementations of MIPS don't use forwarding, and therefore you have to wait until $at is written back. For that purpose, there may be either a stall (which you could get rid off)
subu $t1, $t1, 4
copy_loop:
lw $at, 0($t3)
addu $t1, $t1, 4
addu $t3, $t3, 4
sw $at, 0($t1)
or a load delay slot, which usually takes 1 cycle, making it
copy_loop:
lw $at, 0($t3)
addu $t1, $t1, 4
sw $at, 0($t1)
addu $t3, $t3, 4
Generally speaking, it depends :)
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.
i have been practicing my MIPS programming skills, as well as taking a class for computer architecture and i ran into a problem. these are the instructions: Write a MIPS program that:
Initializes an integer array
Sorts its elements in ascending order in a second array and prints them.
Sorts its elements in descending order in a third array and prints them....
This is what i have so far but I don't know how to populate array 3 with the descending order numbers
.data
array: .space 100
input: .asciiz "Enter at least 4 integers: Enter the number 999 to exit \n"
output: .asciiz "The array in ascending order: \n"
commas: .asciiz ","
.text
.globl main
main:
la $a1, array #loads a pointer to array into $a1
li $a2,9 #loads 9 into $a2
li $t0,0
li $t1,999
loops:
la $a0, input #loads input 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 loops
swap:
la $t4, array #loads array to $t4
la $t1, array #loads array to $t1
addi $t1,$t1,4 #add 4 to $t1, save to $t1
la $t8,array #loads array to $t8
add $t8,$t0,$t8 #add $t8 to $t0, save to $t8
la $t9,array
add $t9,$t0,$t9 #add $t9 to $t0, save to $t9
addi $t9,$t9,-4 #subtracts 4 from $t9, save to $t9
loop:
lw $t2,($t4) #load input into $t2
lw $t3,($t1) #load input into $t3
bgt $t2,$t3,loop1 #if $t2 > $t3, go to loops
#blt $t2,$t3,loop1 #if $t2 < $t3, go to loops
sw $t3,($t4) #store $t3 in $t4
sw $t2,($t1) #store $t2 in $t1
loop1:
addi $t1,$t1,4 #add 4 to $t1, save to $t1
blt $t1,$t8,loop #if $t1<$t8, go to loop
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,loop #if $t4<$t9, to go loop
print:
la $a1,array #loads array to $a1
la $a0, output #loads output to $a0
li $v0, 4 #loads 4 into #v0
syscall
loop2:
blez $t0, done #if $t0<=0, go to done
li $v0, 1 #loads 1 into $v0
lw $a0, 0($a1) #load an inout 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 loop2
done:
j done
could anyone please help me out?
P.S. those comments are there as a guideline for myself
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