# Purpose: Read in three integers and calculate first - second - third + 38 and
ID: 3694446 • Letter: #
Question
# Purpose: Read in three integers and calculate first - second - third + 38 and
# the product first*third.
.data
prompt1: .asciiz "Enter a number: " # first prompt message
prompt2: .asciiz "Enter another number: " # second prompt message
prompt3: .asciiz "Enter a third number: " # third prompt message
result: .asciiz "The first result is " # first answer message
product: .asciiz "The product is " # the product answer message
endline: .asciiz " " # new line
.text
main:
# Prompt user for first number.
la $a0, prompt1 # load the addr of prompt1 into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Read first number.
li $v0, 5 # 5 is the read integer syscall
syscall # read the first integer
move $t1, $v0 # move the first number read into $t1.
# Prompt user for second number.
la $a0, prompt2 # load the addr of prompt2 into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Read second number.
li $v0, 5 # 5 is the read integer syscall
syscall # read the second integer
move $t2, $v0 # move the second number read into $t2
# Prompt user for third number.
# You must add three lines here. Use copy and paste with one minor change.
# Read third number. Put it in $t3
# You must add three lines here. Use copy and paste with one minor change.
# Do the math here. Leave first - second - third + 38 in $t4
# and leave first*third in $t5
# You must add 4 or 5 lines here. Copy and paste will not work this time.
# Tell user the first result is to follow.
la $a0, result # load the addr of result into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Print the above result.
move $a0, $t4 # load the result into $a0
li $v0, 1 # 4 is the print integer syscall
syscall # print the integer
# Print the newline character to bring the cursor down and back to the left.
la $a0, endline # load the addr of endline into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Tell user the product is to follow.
la $a0, product # load the addr of result into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Print the above result.
move $a0, $t5 # load the product into $a0
li $v0, 1 # 4 is the print integer syscall
syscall # print the integer
# Print the newline character to bring the cursor down and back to the left.
la $a0, endline # load the addr of endline into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Exit the program.
li $v0, 10 # 10 is the exit program syscall
syscall # exit program
Explanation / Answer
# Purpose: Read in three integers and calculate first - second - third + 38 and
# the product first*third.
.data
prompt1: .asciiz "Enter a number: " # first prompt message
prompt2: .asciiz "Enter another number: " # second prompt message
prompt3: .asciiz "Enter a third number: " # third prompt message
result: .asciiz "The first result is " # first answer message
product: .asciiz "The product is " # the product answer message
endline: .asciiz " " # new line
.text
main:
# Prompt user for first number.
la $a0, prompt1 # load the addr of prompt1 into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Read first number.
li $v0, 5 # 5 is the read integer syscall
syscall # read the first integer
move $t1, $v0 # move the first number read into $t1.
# Prompt user for second number.
la $a0, prompt2 # load the addr of prompt2 into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Read second number.
li $v0, 5 # 5 is the read integer syscall
syscall # read the second integer
move $t2, $v0 # move the second number read into $t2
# Prompt user for third number.
la $a0, prompt3 # load the addr of prompt3 into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Read third number. Put it in $t3
li $v0, 5 # 5 is the read integer syscall
syscall # read the third integer
move $t3, $v0 # move the third number read into $t3
# Leave first - second - third + 38 in $t4
# $t4 = $t1 - $t2 - $t3 + 38
sub $t4,$t1,$t2 #$t4 = $t1 - $t2
sub $t4,$t4,$t3 #$t4 = $t4 - $t3
addi $t4,$t4, 38 #$t4 = $t4 + 38
# and leave first*third in $t5
# $t5 = $t1 * $t3
mult $t1,$t3 # multiply 32-bit quantities in $t1 and $t3, and store 64-bit result in special
#registers Lo and Hi: (Hi,Lo) = $t1 * $t3
mfhi $t5 # move quantity in special register Hi to $t5: $t5 = Hi
mflo $t5 # move quantity in special register Lo to $t5: $t5 = Lo
# Tell user the first result is to follow.
la $a0, result # load the addr of result into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Print the above result.
move $a0, $t4 # load the result into $a0
li $v0, 1 # 4 is the print integer syscall
syscall # print the integer
# Print the newline character to bring the cursor down and back to the left.
la $a0, endline # load the addr of endline into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Tell user the product is to follow.
la $a0, product # load the addr of result into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Print the above result.
move $a0, $t5 # load the product into $a0
li $v0, 1 # 4 is the print integer syscall
syscall # print the integer
# Print the newline character to bring the cursor down and back to the left.
la $a0, endline # load the addr of endline into $a0
li $v0, 4 # 4 is the print_string syscall
syscall # print the prompt
# Exit the program.
li $v0, 10 # 10 is the exit program syscall
syscall # exit program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.