You are tasked to calculate the sum of two decimal values which are entered by t
ID: 3558595 • Letter: Y
Question
You are tasked to calculate the sum of two decimal values which are entered by 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.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 "
.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
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
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: b end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.