Question
Almost complete MIPS program. I'm almost done with a mips program that loads 10 values into an array and gives the user the option to
Almost complete MIPS program. I'm almost done with a mips program that loads 10 values into an array and gives the user the option to manipulate the array. However, the last thing I need is ti be able to find the min and max values in my array and give the user the option to remove them from the array. I can't figure out how to get the min and max values, if you could help find the error in the commented out section of code/ help me implement the last feature it would be super beneficial. Thank you!
.data
.align 4
numbers: .space 40
message1: .asciiz "Enter an integer: "
message2: .asciiz "The array contains the following: "
next_line: .asciiz ", "
txt6: .asciiz " Menu(enter an int as your choice):
1) Replace an element at a certain position
2) Remove the max element
3) Remove the min element
4) Compute values and exit "
txt7: .asciiz "What position from the array do you wish to replace?"
txt8: .asciiz "What value to you want to change it to?"
txt9: .asciiz ", is the summation of all values in the array. "
txt10: .asciiz ", is the product of all values in the array."
.text
.globl main
main:
la $a1, numbers # $a1 is the base address of the array
li $a2, 10 # $a2 = 10;
li $a3, 0 #
jal readArray
jal printArray
jal menu
jr $ra
readArray:
li $t0, 0 # i = 0;
loop: bge $t0, $a2, Exit1 #if (i >= 10)Exit the loop;
# cout << message1 << endl;
la $a0, message1
li $v0, 4
syscall
# cin >> numbers[i];
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 # reloop
#############
# beq $t0, 1, first # branches to first if $t1 = 1
#blt $v0, $t1, min # branches to min if $s0 < $t1
# bgt $v0, $t2, max # branches to max if $s0 > $t2
#first:
# add $t1, $v0, 0 # $t1 = $s0 + 0
# add $t2, $v0, 0 # $t2 = $s0 + 0
# j loop # reloop
#min:
# add $t1, $v0, 0 # $t1 = $s0 + 0
# j loop # reloop
#max:
# add $t2, $v0, 0 # $t2 = $s0 + 0
j loop # reloop
################
arraySum:
la $a1, numbers
li $a2, 10
li $a0, 0
li $t3, 0 #sum
li $t0, 0
loop3:
bge $t0, 10, endsum #if (i >= 10) Exit the loop;
lw $t3, 0($a1)
add $a0, $t3, $a0
addi $a1, $a1, 4
addi $t0, $t0, 1
j loop3
endsum:
li $v0, 1
syscall
la $a0, txt9 # copies address of txt9 into $a0
li $v0, 4 # loads print string instruction into $v0
syscall
jal arrayProduct
arrayProduct:
la $a1, numbers
li $a2, 10
li $a0, 1
li $t3, 1 #product
li $t0, 0
loop4:
bge $t0, 10, endpro #if (i >= 10) Exit the loop;
lw $t3, 0($a1)
mult $a0, $t3
mflo $a0
addi $a1, $a1, 4
addi $t0, $t0, 1
j loop4
endpro:
li $v0, 1
syscall
la $a0, txt10 # copies address of txt10 into $a0
li $v0, 4 # loads print string instruction into $v0
syscall
jal Exit
printArray:
# lw $a0, 0($t1)
# li $v0, 1
# syscall
#
# #add $a0, $t2, 0
# lw $a0, ($t2)
# li $v0, 1
# syscall
la $a1,numbers
li $t0, 0 #1 = 0;
# cout << message2 << endl;
la $a0, message2
li $v0, 4
syscall
loop1:
bge $t0, $a2, Exit1 #if (i >= 10) Exit the loop;
li $v0, 1
lw $a0, 0($a1)
syscall
#cout << " " << endl;
la $a0, next_line
li $v0, 4
syscall
addi $a1, $a1, 4
addi $t0, $t0, 1
j loop1
replaceElement:
la $a1, numbers # $a1 is the base address of the array
li $t0, 0 # i = 0
la $a0, txt7 # copies address of txt7 into $a0 "what position"
li $v0, 4 # loads print string instruction into $v0
syscall
li $v0, 5 # load 5(read float instruction) into register $v0
syscall
loop2:
bge $t0, $v0, re2 #if (i >= user position(4) Exit the loop;
addi $a1, $a1, 4 # move the array over by 1 element
addi $t0, $t0, 1 #i++
j loop2
re2:
la $a0, txt8 # copies address of txt8 into $a0 "what value"
li $v0, 4 # loads print string instruction into $v0
syscall
li $v0, 5 # load 5(read float instruction) into register $v0
syscall
sw $v0, ($a1) # puts value for spot in array in $t7
jal printArray
jal menu
menu:
la $a0, txt6 # copies address of txt6 into $a0
li $v0, 4 # loads print string instruction into $v0
syscall
li $v0, 5 # load 5(read float instruction) into register $v0
syscall
beq $v0, 1, replaceElement
#beq $v0, 2, minmax # branches to remove max if user enters 2
#beq $v0, 3, removemin # branches to remove min if user enters 3
beq $v0, 4, arraySum
syscall
Exit1:
jr $ra
Exit:
li $v0, 10
syscall
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started