Write and test a MIPS assembly language program that determines if three positiv
ID: 3699775 • Letter: W
Question
Write and test a MIPS assembly language program that determines if three positive integers entered by a user form a triangle. The three integers from a triangle if the sum of any two of the integers is greater than the third integer. The program begins by prompting the user to enter three positive integers. Next, it performs the calculations and comparisons necessary to determine if the integers form a triangle. The program then displays an appropriate message (see sample runs), based on the results of the tests. Finally, it asks the user if s/he wants to test more integers. If the user types “1,” the program loops again. If the user types “0,” it ends. Be sure to include error-checking code to deal with the possible entry of integers that are negative or 0 (zero), in the event of which an appropriate error message should be displayed and the user prompted to enter another integer. Your program output should resemble the sample runs at the end of this document. Notes: ? The program must loop. ? The use of procedures is optional. ? Please document all sources (Web, in particular) used to complete this project.
Sample run #1 (User enters integers that do/do not form a triangle, as well as some invalid values.):
Please enter 3 positive integers by pressing <enter> after each:
3
4
5
The integers DO form a triangle
***********************************************************************
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 1
***********************************************************************
Please enter 3 positive integers by pressing <enter> after each:
0
Invalid entry!
Please enter 3 positive integers by pressing <enter> after each:
1
-1
Invalid entry!
Please enter 3 positive integers by pressing <enter> after each:
1
1
0
Invalid entry!
Please enter 3 positive integers by pressing <enter> after each:
2
2
2
The integers DO form a triangle
***********************************************************************
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 1
***********************************************************************
Please enter 3 positive integers by pressing <enter> after each:
1
2
3
The integers do NOT form a triangle
***********************************************************************
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 1
***********************************************************************
Please enter 3 positive integers by pressing <enter> after each:
4
4
8
The integers do NOT form a triangle
***********************************************************************
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 1
***********************************************************************
Please enter 3 positive integers by pressing <enter> after each:
7
5
6
The integers DO form a triangle
***********************************************************************
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 0
***********************************************************************
-- program is finished running --
4
Sample run #2 (User enters three integers to test and quits.):
Please enter 3 positive integers by pressing <enter> after each:
12
5
13
The integers DO form a triangle
***********************************************************************
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 0
***********************************************************************
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
.data
prompt: .asciiz "Please enter 3 positive integers by pressing <enter> after each: "
yesMsg: .asciiz "The integers DO form a triangle "
noMsg: .asciiz "The integers do NOT form a triangle "
invalidMsg: .asciiz "Invalid entry! "
againMsg: .asciiz " Do you want to test more integers? (Type 0 to quit, 1 to run again.): "
.text
loop:
#prompt and read int
li $v0, 4
la $a0, prompt
syscall
#read int and store in $t0
li $v0, 5
syscall
move $t0, $v0
ble $t0, 0, error
#read int and store in $t1
li $v0, 5
syscall
move $t1, $v0
ble $t1, 0, error
#read int and store in $t2
li $v0, 5
syscall
move $t2, $v0
ble $t2, 0, error
add $t3, $t0, $t1 #sum of side1 and side2 in $t3
add $t4, $t1, $t2 #sum of side2 and side3 in $t4
add $t5, $t0, $t2 #sum of side1 and side3 in $t5
ble $t3, $t2, printNo #if side1+side2 <= side3, no triangle
ble $t4, $t0, printNo #if side2+side3 <= side1, no triangle
ble $t5, $t1, printNo #if side1+side3 <= side2, no triangle
#all conditions met, so forms triangle
li $v0, 4
la $a0, yesMsg
syscall
b ask_again
error:
li $v0, 4
la $a0, invalidMsg
syscall
b loop
printNo:
li $v0, 4
la $a0, noMsg
syscall
ask_again:
li $v0, 4
la $a0, againMsg
syscall
#read user input int
li $v0, 5
syscall
move $t0, $v0
beq $t0, 1, loop
end_loop:
#exit
li $v0, 10
syscall
=================
output
Please enter 3 positive integers by pressing <enter> after each:
3
4
5
The integers DO form a triangle
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 1
Please enter 3 positive integers by pressing <enter> after each:
0
Invalid entry!
Please enter 3 positive integers by pressing <enter> after each:
1
-1
Invalid entry!
Please enter 3 positive integers by pressing <enter> after each:
1
1
0
Invalid entry!
Please enter 3 positive integers by pressing <enter> after each:
2
2
2
The integers DO form a triangle
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 1
Please enter 3 positive integers by pressing <enter> after each:
1
2
3
The integers do NOT form a triangle
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 1
Please enter 3 positive integers by pressing <enter> after each:
7
5
6
The integers DO form a triangle
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 1
Please enter 3 positive integers by pressing <enter> after each:
12
5
13
The integers DO form a triangle
Do you want to test more integers? (Type 0 to quit, 1 to run again.): 0
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.