This assignment is designed to give you practice building control structures in
ID: 3747016 • Letter: T
Question
This assignment is designed to give you practice building control structures in MIPS.
VERY IMPORTANT: You are restricted to the core instructions for this assignment. DO NOT use any pseudo or extended instructions. Turn off the option on MARS under settings that allows the use of these instructions, This means you must use the standard formats and no use of li or la instructions.
Write a MIPS assembly language program to do the following.
Print your name
Prompt the user for the number of times to repeat the testing of your logic for step 4. (See the
example below.)
Read an integer
If the integer that is input is less than 1
Print an error message. Else
Repeat the following steps the number of times input in step 3. This will require a loop.
Read in a list of integers ending with a 0. The 0 marks the end of the input and is not
considered part of the list.
Print the largest and smallest numbers in the input list. If only a zero appears in the list,
print an error message
Print an ending message and then end your program. Note: your program should not end with an
error message.
Example input and output for the program. In this example, the number of times the logic is tested in this example is 3.
Tom Jones
Enter the number of times to test: 3
Enter a number 1 Enter a number 0
The smallest number is 1 The largest number is 1
Enter a number 0
Error: there are no numbers to test
Enter a number -1 Enter a number 8 Enter a number 5 Enter a number 0
The smallest number is -1 The largest number is 8
Program complete Specifics:
Do not store any values into memory. There are no lw or sw instructions in this program. Just read the integers and process them.
Document the beginning of your program with your name, project description, and class ID.
Document every instruction.
Explanation / Answer
Given below is the code for the question. In the code, please replace the word MyName with your actual name.
Please do rate the answer if it was helpful. Thank you
.data
name: .asciiz "MyName"
msg1: .asciiz " Enter the number of times to test: "
msg2: .asciiz "Enter a number "
smallest: .asciiz "The smallest number is "
largest: .asciiz " The largest number is "
error: .asciiz "Error: there are no numbers to test "
newline: .asciiz " "
.text
#print name
li $v0, 4
la $a0, name
syscall
#get no. of times to test
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
move $t0, $v0 #t0 = no. of times
li $t1, 0 #counter for no. of tests
test_loop1:
beq $t1, $t0, exit
#initialize
li $t2, 0 #smallest
li $t3, 0 #largest
li $t4, 0 #no. of inputs/numbers given
loop2:
#prompt and read number
li $v0, 4
la $a0, msg2
syscall
#read int and store in $t0
li $v0, 5
syscall
move $t5, $v0
beqz $t5, end_test
beqz $t2, update_smaller #if the smaller number so far is zero, we are getting 1st number, update smallest no.
blt $t5, $t2, update_smaller
b check_greater
update_smaller:
move $t2, $t5 #update the smaller number
check_greater:
bgt $t5, $t3, update_greater
b next_loop2
update_greater:
move $t3, $t5 #update the smaller number
b next_loop2
next_loop2:
add $t4, $t4, 1 #increment no. of inputs received
b loop2
end_test:
beqz $t4, show_error #check if there were no inputs
#there were some inputs, show smallest and largest
li $v0, 4
la $a0, smallest
syscall
li $v0, 1
move $a0, $t2
syscall
li $v0, 4
la $a0, largest
syscall
li $v0, 1
move $a0, $t3
syscall
li $v0, 4
la $a0, newline
syscall
b increment_test
show_error:
li $v0, 4
la $a0, error
syscall
increment_test:
add $t1, $t1, 1 #increment no of tests completed
b test_loop1
exit:
#exit
li $v0, 10
syscall
output
=====
MyName
Enter the number of times to test: 3
Enter a number 1
Enter a number 0
The smallest number is 1
The largest number is 1
Enter a number 0
Error: there are no numbers to test
Enter a number -1
Enter a number 8
Enter a number 5
Enter a number 0
The smallest number is -1
The largest number is 8
-- program is finished running --