I want to create an array that stores in integers from the keyboard and then I c
ID: 3550299 • Letter: I
Question
I want to create an array that stores in integers from the keyboard and then I can scan that array to check numbers in order, if the number I check is greater than the previous then I will set the next number in the array to the higher value.
Should have a main, readArray and increaseOrder.
If you can help me with just making a readArray section of code I will give you full points.
Example code output:
Specify how many numbers should be stored in the array (at most 8):
8
Enter an integer:
1
Enter an integer:
2
Enter an integer:
3
Enter an integer:
4
Enter an integer:
5
Enter an integer:
4
Enter an integer:
4
Enter an integer:
4
The array context is:
1
2
3
4
5
5
5
5
Any help is appreciated please no spam
Explanation / Answer
please rate - thanks
.data
numbers: .space 32
message: .asciiz "Specify how many numbers should be stored in the array (at most 8):"
message1: .asciiz "Enter an integer: "
message2: .asciiz "The array contents is: "
next_line: .asciiz " "
.text
.globl main
main:
la $a1, numbers # $a1 is the base address of the array
li $v0,4
la $a0,message
syscall
li $v0,5
syscall
move $a2,$v0
jal readArray
la $a1,numbers
jal increaseOrder
la $a1,numbers
jal printArray
jr $ra
readArray:
li $t0, 0 # i = 0;
loop: bge $t0, $a2, Exit1
la $a0, message1
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($a1)
addi $a1, $a1, 4 # move the array over by 1 element
addi $t0, $t0, 1 #i++
j loop
printArray:
li $t0, 0 #1 = 0;
la $a0, message2
li $v0, 4
syscall
loop1:
bge $t0, $a2, Exit1
li $v0, 1
lw $a0, 0($a1)
syscall
la $a0, next_line
li $v0, 4
syscall
addi $a1, $a1, 4
addi $t0, $t0, 1
j loop1
Exit1:
jr $ra
increaseOrder:
li $t0, 0 # i = 0;
lw $t1, 0($a1)
addi $a1, $a1, 4 # move the array over by 1 element
loop2:
lw $t2,0($a1)
ble $t1,$t2,getNext
move $t2,$t1
sw $t2,0($a1)
getNext:
move $t1,$t2
addi $t0, $t0, 1 #i++
bge $t0, $a2, exit2
addi $a1, $a1, 4 # move the array over by 1 element
j loop2
exit2:
jr $ra
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.