You are tasked to calculate the sum of two decimal values which are entered bv t
ID: 3558608 • Letter: Y
Question
You are tasked to calculate the sum of two decimal values which are entered bv the user, and then output the sum in decimal format followed by the sum in binary format. More formally, write MIPS assembly code that will execute in MARS to prompt and allow the user to enter a positive integer represented in decimal. A second input step then allows entry of a second positive integer represented in decimal. Calculate the sum of the two values entered. Display the sum in decimal format on the display. Next, display the sum in binary format on the display. For each output, you shall also provide the declarative text as indicated in the Sample output below. You are tasked to provide a final line of output to the display which provides a declarative statement of which value is greater. More formally, write MIPS assembly code that will execute in MARS to display exactly one of the following statements: "The first number is greater than the second number." which shall be displayed if and only if the value of the first number entered by the user is greater than the value of the second number entered. "The second number is greater than the first number." which shall be displayed if and only if the value of the second number entered by the user is greater than the value of the first number entered. "The two values entered are equal." which shall be displayed if and only if the value of the second number entered by the user is equal to the value of the first number entered.Explanation / Answer
.data
str1: .asciiz "Enter first integer "
str2: .asciiz "Enter Second integer "
str3: .asciiz "the sum in decimal is "
str4: .asciiz " the sum in binary is "
str5: .asciiz " The first number is greater than the second number."
str6: .asciiz " The second number is greater than the first number."
str7: .asciiz " The two values entered are equal."
.text
li $v0,4 #command to print string
la $a0,str1 #string to be printed
syscall #print str1
li $v0,5 #command to read integer
syscall #read int
# $v0 contains integer read
move $a1,$v0 #store first integer in $a1
move $a2,$a1
li $v0,4 #command to print string
la $a0,str1 #string to be printed
syscall #print str2
li $v0,5 #command to read integer
syscall #read int
add $a1,$a1,$v0 #add both first and second integer and store the resulti in $a1
move $a3,$v0
li $v0,4 #command to print string
la $a0,str3 #string to be printed
syscall #print str3
li $v0,1 #command to print integer
move $a0,$a1 #integer to be printed in decimal
syscall #print the sum
li $v0,4 #command to print string
la $a0,str4 #string to be printed
syscall #print str4
#to print in binary we have to print every bit of the number starting from MSB
#there are 32 bits
li $t0,0x80000000 #mask to extract each bit, this also acts as check if all the bits are printed
again:
and $t1,$t0,$a1 #mask all the bits except the one required and print either 0 or 1
beq $0,$t1,printzero
b printone
return:
srl $t0,$t0,1 #shift the mask right to find the next bit
beq $t0,$0,end
b again
printzero:
li $v0,1 #command to print integer
li $a0,0 #integer to be printed in decimal
syscall #print the sum
b return
printone:
li $v0,1 #command to print integer
li $a0,1 #integer to be printed in decimal
syscall #print the sum
b return
end:
beq $a2,$a3,printequal
bgt $a2,$a3,printgreater
#if not grater or equal it should be lesser
li $v0,4 #command to print string
la $a0,str6 #string to be printed
syscall #print str2
b end1 #infinite loop at the end of program
printequal:
li $v0,4 #command to print string
la $a0,str7 #string to be printed
syscall #print str2
b end1
printgreater:
li $v0,4 #command to print string
la $a0,str5 #string to be printed
syscall #print str2
b end1
end1: b end1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.