Printing an array of float numbers in MIPS - arrays

I am a anew user to MIPS and I am having problems in printing an array of floating point numbers that I am taking in from the user and also I am having problems with finding the most repeated floating point number.
Here is the code in which I am able to take in user input but not able to print the array.
#data declarations: declare variable names used in program, storage allocated in RAM
.data
max: .word 12
temp: .word 1
num: .space 96
input1: .asciiz "Enter a number:\n" #prints the statement
output1: .asciiz "The number that is repeated more often than any other is "
output2: .asciiz " with "
output3: .asciiz " repititions.\n"
output4: .asciiz "The array contains the following: \n"
.text
.globl main
main:
lw $t1, temp
lw $t2, max
Loop:
la $a0, input1 # $a0 = address of input1
li $v0, 4 # $v0 = 4 --- this is to call print_string()
syscall
li $v0, 6
syscall
la $t0, num
s.s $f0, 0($t0)
addi $t0, $t0, 8
addi $t1, $t1, 1
ble $t1, $t2, Loop
la $a0, output4
li $v0, 4
syscall
l.s $f12, 0($t0)
li $v0, 2
syscall
jr $ra

.data
max: .word 3
temp: .word 1
num: .space 96
input1: .asciiz "Enter a number:\n" #prints the statement
output1: .asciiz "The number that is repeated more often than any other is "
output2: .asciiz " with "
output3: .asciiz " repititions.\n"
output4: .asciiz "The array contains the following: \n"
.text
.globl main
main:
lw $t1, temp # loop counter
lw $t2, max # upper bound
la $t0, num # address of array
Loop:
# print input prompt
la $a0, input1
li $v0, 4
syscall
# get value from the user
li $v0, 6
syscall
# move user provided value from $f0 to array
s.s $f0, 0($t0)
# move to the next position in the array, increment loop counter
addi $t0, $t0, 4
addi $t1, $t1, 1
ble $t1, $t2, Loop
# restore loop counter, and array address for printing
lw $t1, temp
la $t0, num
# print output prompt
la $a0, output4
li $v0, 4
syscall
print_loop:
# print number from the array
l.s $f12, 0($t0)
li $v0, 2
syscall
# print space
la $a0, 32
li $v0, 11
syscall
# increment loop counter and move to next value
addi $t1, $t1, 1
addi $t0, $t0, 4
ble $t1, $t2, print_loop
Here is how it works:
Enter a number:
2.3
Enter a number:
3.14
Enter a number:
5.55
The array contains the following:
2.3 3.14 5.55

Related

Assembly: allocate registers into array from C

I have a code in C and I need to translate it into assembly in a dinamic way.
Is there a simple way I could do that?
The C code:
int size, i;
printf("Type array size:");
scanf("%d", &size);
int vector[size];
for (i = 0; i<size; i++){
printf("type value of array[%d]: ", i);
scanf("%d", &vector[i]);
}
My Mips code:
So far, I haven't been translating it dinamically, it just prints the same prompt over and over.
I need the prompt in assembly to look like the one in C.
.data
vec: .space 0
size: .asciiz "\n\n Type array size:"
prompt: .asciiz "Type value of array:"
.text
.globl main
main:
la $a1, vec
li $v0, 4
la $a0, size
syscall
li $v0, 5
syscall
move $a2, $v0 #$a2 holds array size
j Loop
Loop:
beq $a2, $t1, END
li $v0, 4
la $a0, prompt
syscall
li $v0,5 #gets value as printf
syscall
move $s1,$v0
sw $s1, 0($a1)
la $a1, 4($a1) #moves on to next value of the array
addi $t1, $t1, 1
j Loop
END:
a counter needed to be used and its value needed to be updated inside the loop
.data
vec: .space 0
size: .asciiz "\n\n Type array size:"
prompt: .asciiz "Type value of array:["
closebrack: .asciiz "]"
.text
.globl main
main:
la $a1, vec
li $t0, 0 #counter=0
li $v0, 4
la $a0, size
syscall
li $v0, 5
syscall
move $a2, $v0 #$a2 holds array size
j Loop
Loop:
beq $a2, $t1, END
li $v0, 4
la $a0, prompt
syscall
add, $t0, 1 #counter++
move $a0, $t0
li $v0, 1 #print counter
syscall
li $v0, 4
la $a0, closebrack # print the closing bracket "]"
syscall
li $v0,5 #gets value as printf
syscall
move $s1,$v0
sw $s1, 0($a1)
la $a1, 4($a1) #moves on to next value of the array
addi $t1, $t1, 1
j Loop
END:
li, $v0,10
syscall

MIPS Bubble sorting variably assigned array

So ive been able to create an array that is the size of what the user chooses and the code proceeds to Sort just fine when debugging, the issue is for some reason its not getting past Sort and into swap and im not quite sure why. In the debugger it seems to read the code to swap but then skips over it and repeats sort in an infinite loop. Am i doing something wrong?
.data
count: .word 0
prompt1: .asciiz "Enter the array size \n"
prompt2: .asciiz "Enter integers to fill the array \n"
prompt3: .asciiz "Sorted Array: "
.text
li $v0, 4
la $a0, prompt1
syscall
li $v0, 5
syscall
sw $v0, count
sll $a0, $v0, 2
li $v0, 9
syscall
move $t0, $v0
lw $t1, count
loop:
li $v0, 4
la $a0, prompt2
syscall
li $v0, 5
syscall
beqz $v0, sort
sw $v0, 0($t0)
addi $t0, $t0, 4
sub $t1, $t1, 1
bnez $t1, loop
sort:
lw $t3, 0($t0)
lw $t4, 4($t0)
blt $t4, $t3, swap
addiu $t0, $t0, 4
addi $t1, $t1, -1
bne $t1, $zero, sort
jr $ra
swap:
sw $t3, 4($t0)
sw $t4, 0($t0)
j sort
end:
exit:
li $v0, 10
syscall

MIPS filling a variable size array

For some reason the integer values i'm putting into memory of the array of base $t0 cant be accessed later on even though when debugging i can clearly see that they are stored correctly in memory at 0($t0) and 4($t0) in the case of 2 integers. Its when i go into the passloop and attempt to load them into t6 or t7 that no value is being passed. Can someone explain why this is?
# bubble sort
.data
prompt1: .asciiz "Enter the array size \n"
prompt2: .asciiz "Enter integers to fill the array \n"
prompt3: .asciiz "Sorted Array: "
.text
.globl main
main:
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
add $t1, $zero, $v0
add $t2, $zero, $v0
sll $a0, $v0, 2
li $v0, 9
syscall
add $t0, $zero, $v0
loop:
la $a0, prompt2
li $v0, 4
syscall
li $v0, 5
syscall
beqz $t1, mainloop
sw $v0, ($t0)
addiu $t0, $t0, 4
subiu $t1, $t1, 1
bnez $t1, loop
mainloop:
subi $a1,$t2,1
blez $a1,done
li $t3,0
jal passloop
beqz $t3,done
subi $t2,$t2,1
j mainloop
#print sorted data, code still needed. Need to fix accessing issue first
done:
j end
passloop:
lw $t6,0($t0)
lw $t7,4($t0)
bgt $t6,$t7,swap
next:
addiu $t0,$t0,4
subiu $a1,$a1,1
bgtz $a1,passloop
jr $ra
swap:
sw $t6,4($t0)
sw $t7,0($t0)
li $t3,1
j next
end:
li $v0,10
syscall

Error of incorrect type in MIPS

I am getting an "operand of incorrect type" error for the following code at the line where I try to load the individual characters. I am trying to write a program that inputs two strings and compares them (i.e. prints if one is greater than or less than or equal to the other). I want to use a loop to load each letter until either a GT or LT is found, or alternatively, at the end I would know they are equal. Any help would be appreciated!
.data
prompt1: .asciiz "Enter string 1: "
prompt2: .asciiz "Enter string 2: "
gthan: .asciiz "s1 > s2\n"
lthan: .asciiz "s1 < s2\n"
equalto: .asciiz "The strings are equal\n"
s1size: .word 80
s2size: .word 80
s1: .space 20
s2: .space 20
.text
main:
# read in strings
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8 #read in string
la $a0, s1
lw $a1, s1size
move $s0, $a0 #move to s0
syscall
li $v0, 4
la $a0, prompt2
syscall
li $v0, 8 #read in string
la $a0, s2
lw $a1, s2size
move $s1, $a0 #move to s1
syscall
li $a0, 0 #set sentinel
start_loop:
#load single character
la $t0, s1
lb $a1, $a0($t0)
la $t0, s2
lb $a2, $a0($t0)
#compare
bgt $a1, $a2, greaterthan
blt $a1, $a2, lessthan
beq $a1, $a2, equal
addi $a0, $a0, 4
end_loop:
greaterthan:
li $v0, 4
la $a0, gthan
syscall
lessthan:
li $v0, 4
la $a0, gthan
syscall
equal:
li $v0, 4
la $a0, equalto
syscall

Can't print contents of array

I am doing my homework and it says that I have to ask user for how many members they want to be present in the array. Then the user should enter the number in their array and then the program prints the array. I cannot print contents from my array in the console window.
Here is my code:
.data
max: .word -9999
array: .space 12
message1: .asciiz "Enter an integer:\n"
message2: .asciiz "Specify how many numbers should be stored in the array (atmost 8): \n"
message3: .asciiz "The array content is: \n"
message4: .asciiz "The maximum is: \n"
message5: .asciiz "They have the same maximum.\n"
message6: .asciiz "The first array has a larger maximum.\n"
message7: .asciiz "The second array has a larger maximum.\n"
.text
.globl main
main:
lw $s1, max
la $s3, array
li $s2, 3
la $a0, message2
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0
blt $t0, $s2, Read
Read:
la $a0, message1
li $v0, 4
syscall
li $v0, 5
syscall
move $t1, $v0
sw $t1, 0($s3)
addi $s3, $s3, 4
addi $t0, $t0, -1
bgt $t0, $zero, Read
j Print
#blt $t0, $s2, Print
Print:
la $a0, message3,
li $v0, 4
syscall
jr $ra
Thanks for the help.
When you enter Print you've got the end of the array plus 4 in $s3, so you could do something like this:
$s2 = ADDRESSOF(array)
while ($s2 != $s3) do
print_int($s2[0]) // syscall 1
print_character(' ') // syscall 11
$s2 += 4
end while
This is pseudo-code to illustrate the logic; I'll leave the actual assembly implementation to you since it's your assignment.
(By the way, this: la $a0, message3, is a typo. There shouldn't be a comma at the end of that line)

Resources