THE ONLY SECTION OF THE CODE THAT NEEDS TO BE COMPLETED IS THIS: # You write cod
ID: 3795215 • Letter: T
Question
THE ONLY SECTION OF THE CODE THAT NEEDS TO BE COMPLETED IS THIS:
# You write code here to implement the following C code.
# This will find the MIN ($s6) and MAX ($s7)
# in the array from A[0] through A[i].
# Note that you need to use lw to load A[j].
# while (j<=i)
# {
# if A[j]<MIN
# MIN=A[j];
# if A[j]>MAX
# MAX=A[j];
# j=j+1;
# }
In this project, you are asked to write MIPS code to finish all the functions listed in project2.asm. In project2.asm, an array A of 10 integers are given at the beginning (make sure do not change these integers):
34, 5, 11, -12, 60, -2, 14, 71, 13, -27
The finished project2.asm should be filled with your MIPS code in the specified space to implement the given C code and with the finished code, it reads in one index i (the range of the index is between 0 and 9) from the console, then find the minimum value (MIN) and the maximum value (MAX) in the subarray A[0..i], i.e., A[0] through A[i]. You need to use branch instructions, as well as other MIPS instructions we learned for memory and register operations. In project2.asm, I have provided code to read the index i and print out MIN and MAX. Please read the code in project2.asm carefully before working on this project.
A sample result:
--------------------------------------------
Index i [0~9]:
4
MIN=-12
MAX=60
---------------------------------------------
For project submission, please submit your completed project2.asm and make sure it can be compiled and executed in Mars.
project2.asm
.data
baseadd: .word 34, 5, 11, -12, 60, -2, 14, 71, 13, -27
string1: .asciiz "Index i [0~9]: "
string2: .asciiz " MIN="
string3: .asciiz " MAX="
.text
main:
# Input i to $s1
addi $v0, $zero, 4 # code for printing string is 4
la $a0, string1 # load address of string to be printed into $a0
syscall # call operating system
addi $v0, $zero, 5 # code for reading integer is 5
syscall # call operating system
add $s1, $v0, $zero # i in $s1
#baseadd of the array to $s5
la $s5, baseadd
#Load A[0] to initialize MIN ($s6) and MAX ($s7)
lw $s6, ($s5)
lw $s7, ($s5)
# initialize j=0 (j in $t1)
add $t1, $zero, $zero
# You write code here to implement the following C code.
# This will find the MIN ($s6) and MAX ($s7)
# in the array from A[0] through A[i].
# Note that you need to use lw to load A[j].
# while (j<=i)
# {
# if A[j]<MIN
# MIN=A[j];
# if A[j]>MAX
# MAX=A[j];
# j=j+1;
# }
# Print MIN from s6
Exit: addi $v0, $zero, 4 # code for printing string is 4
la $a0, string2 # load address of string to be printed into $a0
syscall # call operating system
add $a0, $s6, $zero
addi $v0,$zero,1 # prints integer
syscall
# Print MAX from s7
addi $v0, $zero, 4 # code for printing string is 4
la $a0, string3 # load address of string to be printed into $a0
syscall # call operating system
add $a0, $s7, $zero
addi $v0,$zero,1 # prints integer
syscall
# exit
addi $v0, $zero, 10
syscall
Explanation / Answer
The Given C code represents the implementation of loop to find maximum and minimum number , the MIPS implementation of the same is given as,
.data
prompt1: .asciiz " Enter number"
ARRAY: .space 64
.text
.globl main
li $t0, 0
li $t1, 10
la $t2, ARRAY
loop:
la $a0, prompt2 # print prompt1 "Enter number"
li $v0, 4
syscall
main:
la $s1, ARRAY
lw $t0, ($s1)
la $t6, counter
lw $s2, ($t6)
addi $t1,$zero, 0
loop: add $t1,$t1,1 # increment index i by 1
beq $t1,$s2,done
add $t2,$t1,$t1 # compute 2i in $t2
add $t2,$t2,$t2 # compute 4i in $t2
add $t2,$t2,$s1 # form address of A[j] in $t2
lw $t3,0($t2) # load value of A[j] into $t3
slt $t4,$t3,$t0 # A[j] < minimum?
beq $t4,$zero,loop
bltz $t3, loop # if A[j] < 0, then skip
addi $t0,$t3,0 # if so, A[j] is the new maximum
j loop
done: la $a0,ans2
li $v0,4
syscall
move $a0,$t0
li $v0,1
syscall # print maximum
la $a0,endl # system call to print
li $v0,4
syscall
li $v0,10
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.