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
  • Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a...

    Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a high level language of your choice and in Assembly to find a Fibonacci number and check your result. At the end of this assignment a possible solution to this problem is given both in Python and MIPS assembler. Note that it would have been possible to write the Python differently to achieve the same function. . [20 points] Write and debug a MIPS program...

  • Control Structures and Subroutines I. A while loop whose condition is not made false is called...

    Control Structures and Subroutines I. A while loop whose condition is not made false is called an infi nite loop. However, it is infinite, in the sense of continuing to run forever, only in theory. The loop will stop at some point. Why? 2. To evaluate the if statements in this question below, assume that a Print statement simply prints whatever is in quotes after it. The phrases after a semicolon in the third code fragment are comments and are...

  • Given the following pseudo code: int result; (result is 1 byte) int count; (count is 1...

    Given the following pseudo code: int result; (result is 1 byte) int count; (count is 1 byte) for (result= 10, count= -10; count < result ; count++) { if(count > 2) result--; else result ++; } 1) write an assembly language program that will implement this pseudo code using a while construct. 2) write an assembly language program that will implement this pseudo code using a do-until construct Note: For both 1) and 2), do not forget to include the...

  • Complete this task4.s ARM assembly code to produce an assembly program that: • Waits for the...

    Complete this task4.s ARM assembly code to produce an assembly program that: • Waits for the user to enter two integers (this part is already coded in). • Places the first integer at r5, and the second integer at r6 (this part is already coded in). • (This is what you need to code in) Adds the two integers together. If the result is between 0 and 9 (including 0 and 9) the program should print the result. If not,...

  • Please write a MIPS program to print the first thirty numbers in the Fibonacci sequence in which each number is the sum of the two preceding ones, starting from 0 and 1. Note that each number in the Fibonacci sequence should be calculated using MIPS instr

    Please write a MIPS program to print the first thirty numbers in the Fibonacci sequence in which each number is the sum of the two preceding ones, starting from 0 and 1. Note that each number in the Fibonacci sequence should be calculated using MIPS instructions. After that please run your MIPS program using SPIM software to display the result in the output (console) window. Save your execution result in a log file of SPIM.What to submit:  1. Your MIPS...

  • Question 5 (1 point) userGuess ← 0, secretNumber ← 5 PRINT “Enter a number between 1...

    Question 5 (1 point) userGuess ← 0, secretNumber ← 5 PRINT “Enter a number between 1 and 10” READ userGuess WHILE (userGuess < 1 OR userGuess > 10) PRINTLINE “That is not between 1 and 10. Try again.” READ userGuess ENDWHILE IF (userGuess == secretNum) THEN PRINTLINE “That’s right! ”, userGuess, “ is the secret!” ELSE PRINTLINE userGuess, “ is not the secret!” ENDIF What is the first WHILE loop being used for? Question 5 options: Input validation Random number...

  • C Programming, part B: (1 point) Write a second program (Program B) that builds a contiguous...

    C Programming, part B: (1 point) Write a second program (Program B) that builds a contiguous list (i.e., array- based), also of 100 elements (cells), each containing a random integer between 0 and 99. You encouraged to re-use as much code from Program A as you wish (this is called code re- sue and it is a good thing). This program must: 1) Build the contiguous list (i.e., the properly populated array) 2) Print out the contents of the list....

  • long fib(int n) { long result;    if (n==0) result = 0; else if (n==1 ||...

    long fib(int n) { long result;    if (n==0) result = 0; else if (n==1 || n==2) result = 1; else result = fib(n-1) + fib(n-2);    return result; } [C language] Question : Why is this code is so slow? Explain ex2) Make the program from exercise 1 faster by trading space for time. Create an array of int that can be used as a cache. Each index in the array should correspond to a value that you can...

  • 2. Translate the following code into MIPS code.         B [0] = 5; for (i =...

    2. Translate the following code into MIPS code.         B [0] = 5; for (i = 1 ; i < 50 ; i = i + 2) {                         B[i] =i + B[i-1]; } Assume the compiler associates the variable i to the register $t0. Also, assume B is an array of integers and its address is stored at register $s1.   

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