Write a MIPS function (called “median”) that computes the median of a set of non
ID: 3798292 • Letter: W
Question
Write a MIPS function (called “median”) that computes the median of a set of nonnegative integer values; your program should be named as “median.s”. The address of the array will be passed in register $a0, and the number of elements in register $a1 and the median should be returned in register $v0. Note that the median is the middle value when sorted; if the number of values is even, then the median is the average of the two middle values (rounding off if the result is not an integer).
Note that the input array is not necessarily sorted.
Explanation / Answer
.text
.globl main
main:
addu $s7, $ra, $zero, # Save the ra
la $a0, int_prompt
li $v0, 4
syscall
li $v0, 5
syscall
bgtz $v0, positive
sub $v0, $zero, $v0
bgtz $v0, positive
li $v0, 1
positive:
slti $s1, $v0, 101 # is $v0 <= 100
bne $s1, $zero, within_limit
li $v0, 100
within_limit:
add $s2, $v0, $zero #sace the value to s2
add $a1, $v0, $zero #save the value to a1
la $a0, array_a # the address of the array
jal median
add $s1, $v0, $zero # We save the result first
la $a0, median_msg
li $v0, 4
syscall
add $a0, $s1, $zero # $a0 is the integer to be printed
li $v0, 1 # system call to print an integer
syscall # print int
la $a0, newline
li $v0, 4
syscall
la $s4, medians
sll $s2, $s2, 2
add $s4, $s4, $s2
lw $s4, -4($s4)
beq $s4, $s1, Correct
la $a0, wrong_message
j end_output
Correct:
la $a0, correct_message
end_output:
li $v0, 4
syscall
addu $ra, $zero, $s7 #restore $ra since the function calles
#another function
jr $ra
add $zero, $zero, $zero
add $zero, $zero, $zero
########## End of main function #########
.data
#Data segment starts here
median_msg:
.asciiz "The median from your function is "
newline:
.asciiz ". "
int_prompt:
.asciiz "Please enter an integer: "
wrong_message:
.asciiz "Your median is incorrect."
correct_message:
.asciiz "Your median is correct."
array_a:
.align 2
.word 7, 16, 291, 272, 287, 113, 372, 378, 159, 259
.word 380, 190, 137, 236, 390, 200, 239, 14, 25, 32
.word 396, 338, 194, 143, 142, 11, 88, 284, 256, 76
.word 46, 181, 63, 247, 393, 36, 342, 51, 250, 126
.word 343, 261, 75, 244, 39, 241, 320, 180, 265, 215
.word 102, 17, 343, 134, 189, 5, 273, 217, 135, 186
.word 356, 45, 54, 148, 253, 337, 20, 154, 68, 315
.word 359, 80, 72, 161, 201, 103, 209, 122, 3, 266
.word 262, 28, 251, 149, 131, 66, 147, 123, 338, 71
.word 256, 17, 235, 3, 152, 60, 394, 128, 73, 193
.word -5, -4, -3, -2, -1
medians:
.align 2
.word 7, 11, 16, 144, 272, 192, 272, 279, 272, 265
.word 272, 265, 259, 247, 259, 247, 239, 237, 236, 218
.word 236, 237, 236, 218, 200, 197, 194, 197, 200, 197
.word 194, 192, 190, 192, 194, 192, 194, 192, 194, 192
.word 194, 197, 194, 197, 194, 197, 200, 197, 200, 207
.word 200, 197, 200, 197, 194, 192, 194, 197, 194, 192
.word 194, 192, 190, 189, 190, 192, 190, 189, 189, 189
.word 190, 189, 189, 187, 189, 187, 189, 187, 186, 187
.word 189, 187, 189, 187, 186, 1
83, 181, 180, 181, 180
.word 181, 180, 181, 180, 180, 170, 180, 170, 161, 170
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.