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
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
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 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 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 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 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...
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 || 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...
Consider the following MIPS machine code. bne $0, $0, 2 addi $t0, $0, 5 addi $t1, $0, 10 What will be in $t0 and $t1 after the above program is executed? a. $t0 = 5, $t1 is unknown because 3rd statement will not be executed b. $t1 = 10, $t0 is unknown because 2nd statement will not be executed c. $t0 = 5, $t1 = 10 d. $t0 = 0, $t1 = 0
Using python, write code for problem. Please also put screenshot of your code. The following program will perform properly if the user enters 0 in response to the request for input. However, the program will crash if the user responds with "eight". Rewrite the program using a try/except statement so that it will handle both types of responses. See Fig. 6.1. while True: n = int (input ("Enter a nonzero integer: ")) if n! = 0: reciprocal = 1/n print...