Program is written in MIPS .Write a program that prompt the user to input n numb
ID: 3793795 • Letter: P
Question
Program is written in MIPS.Write a program that prompt the user to input n numbers (0<n<11) and stores them in an
array. Then it finds the median of the array, assuming the elements are in ascending order. Here is the code I have been working on but I can not get it to compile. Please use MARS in order to compile and debug.
.data
array: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
UserNum:.asciiz "How many numbers would you like to enter? "
Median: .asciiz " The median is = "
EnterNum:.asciiz "Enter a number: "
message:.asciiz "The number you entered must be 0<n<11"
.text
main:
la $s2, array
li $s0, 0 #storing user value to register $0
li $s1,0
li $v0, 4 #printing str1: "How many numbers would you like to save"
la $a0, UserNum
syscall
li $v0, 5 #prompt user to enter a number
syscall
add $s0 $v0, $s0
loop:
ble $s0, $0,error1 #Sends user to error 1 message if parameters not met
bgt $s0, 10, error1
beq $s0, $s1, end
li $v0, 4
la $a0, EnterNum
syscall
li $v0, 5
syscall
add $t0, $v0, $s0
sw $t0, 0($s2) #store the next value of the array into $s0
addi $s0, $s1,1 #increment the counter, $t0 by 1
addi $s2, $s2,4
j loop
error1:
li $v0, 4
la $a0, message #loads error message
syscall
j main # send user to main to restart process
end:
#li $t0,2
#div $s0,$t0
#mflo $t1
#mfhi $t2
li $t0,4
mult $t1, $t0
mflo $t0
beq $t1, $s0, even
li $v0, 4
la $a0, Median #Printing out the Median
syscall
li $s5, 4
#mflo $t2
#mult $t2, $s5
la $s2, array
#mflo $t2
add $s2, $t2 , $s2
li $v0, 1
lw $a0, 0($s2)
syscall
even:
li $v0, 4
la $a0, Median
syscall
#mflo $s4
#li $s0, 4
#mult $s4, $s0
#mflo $s3
la $s2, array
add $s2, $s2, $s3
lw $s0, 0($s2)
lw $s0, -4 ($s2)
add $s0, $s0, $s1
li $s1, 2 #Was originally 2
#div $s0, $s1
#mflo $a0
li $v0, 1
syscall
exit:
li $v0 ,10
syscall
Explanation / Answer
I have written the code according to the system Stack.
MIPS programs use the runtime stack to hold:
- parameters to be passed to a called procedure
- register values that need to be preserved during the execution of a called procedure and restored after the return
- saved procedure return address, if necessary
- local arrays and structures, if any
MIPS provides a special register,$sp, which holds the address of the most recently allocated word on a stack that user programs can employ to hold various values.
Stack Layout:
While finding the median,we need to give our MIPS procedure a copyof the list, and the place to do that is the
runtime stack.
Creating the stack layout:
.data
array: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
EnterNum:.asciiz "Enter a number: "
message:.asciiz "The number you entered must be 0<n<11"
.text
main:
lw $s0, Size # get size of list
li $s4, 4 # size of word
mul $s4, $s4, $s0 #compute size of array on stack
addi $s4, $s4, 4 # allow space for array dimension
sub $sp, $sp, $s4
sw $s0, ($sp) # put size onto the stack
move $s1, $sp # get pointer to top of stack
la $s2, List # get pointerto first list element
li $t0, 0 # count list elementsas they are copied
loop: beq$t0, $s0, done
addi $s1, $s1, 4 # step to location for next list element
lw $s3, ($s2)
sw $s3, ($s1) # put element onto stack
addi $t0, $t0, 1
addi $s2, $s2, 4 # step to next list element
b loop
done:
error1:
li $v0, 4
la $a0, message #loads error message
syscall
j main # send user to main to restart process
end:
Now we find median:
lw $t3, ($sp) # get list dimension
addi $t5, $sp, 4 #get address of beginning of list
lw $t0, ($sp) # get array size
li $t1, 2
div $t0, $t1 # divide it by 2
mfhi $t1 # get the remainder from the divisio
beq $t1, $zero, even # check whether size was even or odd
swc1 $f0, ($sp) # put median onto stack for caller
jr $ra
swc1 $f0, ($sp) # put median onto stack for caller
jr $ra # return to caller
The branch and jump instructions on MIPS have delay slots. That is, the instruction directly following a branch or jump instruction is always executed.
I have done in simple code to find the median.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.