write a mips program that read in 20 integers stores them in an array then calls
ID: 655371 • Letter: W
Question
write a mips program that read in 20 integers stores them in an array then calls the following funtion then print the appropriate result:
minmax: find the smallest and the largest number in the array
divisible: show how many numbers in the array are divisible by 4
so far i have done reading the 20 integers and store them in an array, how do i do the 2 functions?
.data
array: .space 80
prompt: .asciiz "Enter an integer for the array: "
snumber: .asciiz "The smallest number is: "
lnumber: .asciiz "The largest number is: "
divide4: .asciiz "Number of integers divisible by 4: "
.globl main
.text
main:
li $t0 20 #initialize 20 spaces
la $a1 array #load array
li $t1 0 #index i = 0
loop1:
la $a0 prompt #ask to enter integers
li $v0, 4 #print prompt
syscall
li $v0, 5 #read integers from console
syscall
sw $v0, 0 ($a1) #store it
addi $a1 $a1 4 #increments the array
addi $t1 $t1 1 #increments the index
bne $t1 $t0 loop1 #check if condition is true
la $a1 array #reload the arrray
li $t1 0 #counter start from 0
Explanation / Answer
Program:
.data
Array: .space 80
Array2: .word 3, 3, 3, 17, 3
.word 3, 24, 3, 3, 4
.word -4, -8, 97, 3, 2
.word 3, 3, 3, 3, 3, 3
newline: .asciiz “ ” # start a new line
space: .asciiz “ ” # To leave a space
prompt: .asciiz "Enter an integer for the array: "
snumber: .asciiz "The smallest number is: "
lnumber: .asciiz "The largest number is: "
divide4: .asciiz "Number of integers divisible by 4: "
.globl main
.text
main:
li $t0 20 #initialize 20 spaces
la $a1 array #load array
li $t1 0 #index i = 0
loop1:
la $a0 prompt #ask to enter integers
li $v0, 4 #print prompt
syscall
li $v0, 5 #read integers from console
syscall
sw $v0, 0 ($a1) #store it
addi $a1 $a1 4 #increments the array
addi $t1 $t1 1 #increments the index
bne $t1 $t0 loop1 #check if condition is true
la $a1 array #reload the arrray
li $t1 0 #counter start from 0
# Determine the smallest and largest integers in the minmax
Minmax:
move $t0, $s0 # to initialize the array
move $t1, $s1 # to initialize the pointer of array
lw $t2, 0($t1) # load the smallest integer
move $t3, $t2 # load the largest integer
add $t1, $t1, 4
addi $t0, $t0, -1 # decrement the index of the array
Smallestlargest_loop:
blez $t0, Smallestlargest_done # end of the array
lw $t4, 0($t1) # fetch the current #element form the array
add $t1, $t1, 4
addi $t0, $t0, -1
bge $t4, $t2, Smallestlargest_notl
#branch if greater than
move $ t2, $t4
Smallestlargest_notlt:
ble $t4, $t3, Smallestlargest_loop
#branch if less than
move $ t3, $t4
b Smallestlargest_loop
Smallestlargest_done:
li $v0, 4 # system call to print screen
la $a0, snumber # print the message
syscall
move $a0, $t2 # move the value to $a0
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
move $a0, $t3 # move the value to $a0
li $v0, 1
Syscall
la $a0, newline
li $v0, 4
syscall
jr $ra # jump to return
# Program for numbers in the array which are divisible 4
div4:
move $t0, $s0 # to initialize the array
move $t1, $s1 # to initialize the pointer of array
li $t2, 0
Div4_loop:
blez $t0, Div4_done # End of the array
lw $t4, 0($t1) # fetch the current element form the array
add $t1, $t1, 4
addi $t0, $t0, -1
and $t4, $t4, 0X03
bnez $t4, Div4_loop
addi $t2, $t2, 1
b div4_loop
Div4_done:
li $v0, 4
la $a0, divide4
syscall
move $a0, $t2 # move the value to $a0
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.