Question
Help with final problem in MIPS program. The program needs to be able to remove the minimum and maximum elements from an array of 10.
Help with final problem in MIPS program. The program needs to be able to remove the minimum and maximum elements from an array of 10. Currently when the user selects those options all they do it find the min or max. I am unsure how to make the program so that it matches the following i/o. Keep mind that the program can't just make the value in the array zero beause upon completion the program calculates the product. Below the i/o is my program with option 1 and 2 in the menu which is all that I need help on.
Please enter 10 integer values: 1 2 3 4 5 6 7 8 9 10 Your values are: 1 2 3 4 5 6 7 8 9 10 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 What would you like to do? 1 What position from the array do you wish to replace? 3 What value to you want to change it to? 4 Your values are: 1 2 4 4 5 6 7 8 9 10 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 What would you like to do? 2 Your values are: 1 2 4 4 5 6 7 8 9 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 What would you like to do? 4 The summation of all values in the array is: 46, the product of all values in the array is: 483840
Program:
.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 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
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:
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 menu
minimum:
la $a1,numbers
li $t0,0
lw $t1,($a1)
addi $a1,$a1,4
addi $t0,$t0,1
loop17:
bgt $t0,9,print
lw $t2,($a1)
addi $t0,$t0,1
addi $a1,$a1,4
blt $t2,$t1,loop27
j loop17
loop27:
move $t1,$t2
j loop1
maximum:
la $a1,numbers
li $t0,0
lw $t1,($a1)
addi $a1,$a1,4
addi $t0,$t0,1
loop37:
bgt $t0,9,print
lw $t2,($a1)
addi $t0,$t0,1
addi $a1,$a1,4
bgt $t2,$t1,loop47
j loop37
loop47:
move $t1,$t2
j loop37
print:
li $v0,1 # print $t2
move $a0,$t1
syscall
j menu
menu:
jal printArray
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, maximum # branches to remove max if user enters 2
beq $v0, 3, minimum # 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