Write a MIPS assembly language program that computes the difference between two
ID: 3766830 • Letter: W
Question
Write a MIPS assembly language program that computes the difference between two numbers specified by the user at runtime, and displays the result on the screen. The program should display the message "Enter First Integer >>" to let the user enter the first integer and store the first integer in $t1, then display the message "Enter Second Integer >> " and store the second integer in $t2. After computing the result display "The Result is = " then display the result.
This is what i did, but instead of subtracting the 2 numbers, it add the numbers and leaves a minus sign in front of it. So for example, I input 9 and 3, it should be 6, but it shows -12. I don't know what I'm doing wrong. Please help!!
.data
prompt1: .asciiz "Enter First Integer"
prompt2: .asciiz "Enter Second Integer"
prompt3: .asciiz "The Result is = "
.globl main
.text
main:
li $v0, 4
la $a0, prompt1
syscall
#Read the integer from keyboard
li $v0, 5
syscall #User will now input the first integer
sub $t0, $t0, $v0 #read num1 into v0
li $v0, 4
la $a0, prompt2
syscall
#Read the integer from keyboard
li $v0, 5
syscall #User will now input the second integer
sub $t0, $t0, $v0
li $v0, 4
la $a0, prompt3
syscall
li $v0, 1
move $a0, $t0
syscall
li $0, 10
syscall
Explanation / Answer
PrograM for MIPS assembly language that computes the difference between two numbers specified by the user at runtime, and displays the result on the screen.
#prompt1: .asciiz "Enter First Integer"
#prompt2: .asciiz "Enter Second Integer"
#prompt3: .asciiz "The Result is = "
# Registers used:
# $t0 - used to hold the I num.
# $t1 - used to hold the II num.
# $t2 - used to hold the sum of the $t1 and $t2.
# $v0 - syscall parameter and return value.
# $a0 - syscall parameter
main:
## Get I num from user, put into $t0
la $a0, prompt1
Li $v0, 5 # load syscall read_int into $v0.
Syscall # make the syscall.
move $t0, $v0 # move the number read into $t0.
## Get II nuM from user, put into $t1
la $a0, prompt2
li $v0, 5 # load syscall read_int into $v0.
syscall # make the syscall.
move $t1, $v0 # move the number read into $t1.
sub $t2, $t0, $t1 # calculate the subtraction.
## Print out $t2
move $a0, $t2
la $a0, prompt3
li $v0, 1 # load syscall print_int into $v0.
Syscall
li $v0, 10 # syscall code 10 is for exit.
Syscall # make the syscall.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.