Question

first = 4 second = −5 if first > 0 and second >= 0:    result...

first = 4
second = −5
if first > 0 and second >= 0:
   result = second//first
elseif first == second or first < second:
   result = first∗second
else :
   result = second∗2
  
   print ( result )

since while first>0 succeeds, second >=0 fails, and both first==second and first

translate the above code to a properly commented MIPS program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please upvote ,comment if any query . Thanks .

Note : check attached image for output and code . Code compiled and tested in MARS Simulator MIPS.

Program :

.data
   result: .asciiz "result is: "
.text
   addi $s0,$zero ,4 #first =4
   addi $s1,$zero ,-5 #second =-5
   addi $s2,$zero ,0 #result=0
  
  
   sgt $t0,$s0,0 #if first>0 set $t0=1
   sge $t1,$s1,0 #if second>=0 set $t1=1


   
    beq $t0,0,elseif #if t0 equal to 0 go to elseif
   beq $t1,0,elseif #if t1 equal to 0 go to elseif
   #if above both not true calculate result
  
   div $s2,$s0,$s1 #result = first//second // operator return integer value of divide
   j Result #go to result label
  
  
elseif: #else if


seq $t2,$s0,$s1 #first==second $t2=1
    slt $t3,$s0,$s1 #first<second $t3=1


    beq $t2,1,calculateResult #if t2 equal to 1 go to calculateResult
    beq $t3,0,else #if t3 equal to 0 go to else
    #elseif condition both are zero
calculateResult:
#result = first*second
    mul $s2,$s0,$s1
    jal Result  
      
else : #else
   #result = second*2

   mulo $s2,$s1,2
  
  
   j Result
   
Result: #pprint result
  
   #print result is string
   li $v0,4
   la $a0,result
   syscall
  
#print result variable
   li $v0,1 #print result
   addi $a0,$s2,0
   syscall
     
   #terminate the program
li $v0,10 #exit
   syscall
   
  
   
   
   
   
   


   
Output : when first=4 result =5 will go to if


   Output : when first=-4 result =-4 will go to else if

   Output : when first= 4 result =-5 will go to else
   
   


Add a comment
Know the answer?
Add Answer to:
first = 4 second = −5 if first > 0 and second >= 0:    result...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT