Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Write a MIPS function that computes the middle value(s) of a set of nonnegative integer values. The address of the array will be passed in

Write a MIPS function that computes the middle value(s) of a set of nonnegative integer values. The address of the array will be passed in using register $a0, and the number of elements using register $a1 and the results should be returned in registers $v0 and $v1. If the number of values is even, your program needs to return both of the middle values; otherwise, your program should return the middle value in $v0 and 0 in $v1. Note that the input array is not necessarily sorted. For example, if the input array is [1, 5, 6, 4, 3, 2], your program needs to return 3 in $v0 and 4 in $v1; if the input array is [6, 9, 7, 8, 10], your program should return 8 in $v0 and 0 in $v1.

You need to test your program using the code provided below using two different odd numbers (between 1 and 100) and two different even numbers (between 1 and 100) and include the results shown on the console.

# Register usage # $s7 - save $ra # $s1 - used as a temporary register # $a0 - for function parameter / syscall parameter # $v0 - syscall number / function return value .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  

I'm providing the sort program below as well, which you can use:

image text in transcribed

Saving registers $sp,$sp. -20 $ra, 16( $sp)# save $ra on stack $s3,12($sp) $52, 8($sp)# save $s2 on stack $51, 4($sp)# save $s1 on stack $sO, 0($sp)# save $s0 on stack sort: addi # make room on stack for 5 registers S W SW SW S W SW # save $s3 on stack Procedure body $52, $a0 # copy parameter $a0 into $s2 (save $a0) $s3, $a1 # copy parameter $al into $s3 (save $al) $s0, $t0,$50,$53 #reg$t0-0 i f$50 $tO, $zero, exit!# go to exit! if $s0 $s1,$50,-1#j-1-1 move move move Move parameters $zerot i-0 Outer loop forltst:slt $53(i n) $53 (i n) addi $tO, $zero, exit2t go to exit2 if $s1 0 (j 0) $t1, $s1, 2# reg $t1-J * 4 bne Inner loop add slt st0, $zero, exit2 go to exit2 if $t4 S $a0, $s'2 $a1, $s1 # 2nd parameter of swap is J Swap $51,$51,-1#j--1 for2tst $s0, $s0, 1 forltst $t3 # 1st parameter of swap is v (old $a0) # swap code shown in Figure 2.25 # jump to test of inner loop # jump to test of outer loop move Pass parameters move and call Inner loop Outer loop addi exit2: addi Restoring registers exitl: w $S0, 0($sp) $51, 4($sp)# restore $s1 from stack $52, 8($sp)# restore $s2 from stack $s3,12($sp) ra,16($sp) sp,$sp, 20 # restore $s0 from stack # restore $s3 from stack # restore $ra from stack # restore stack pointer addi Procedure return ra # return to calling routine

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions