Homework Help Question & Answers

Assignment 4 File “quad_sol.s” contains a quadratic polynomial solver, which calculates the integer solution of a quadratic polynomial equation. 1. Rewrite the program using instructions reordering to...

Assignment 4

File “quad_sol.s” contains a quadratic polynomial solver, which calculates the integer solution of a quadratic polynomial equation.

1. Rewrite the program using instructions reordering to reduce the number of cycles needed to execute the program. Indicate the number of cycle reduction.

2. Describe how forwarding would affect the execution of the program.

CODE

# quad_sol.s
# This assembly program calculates the integer solutions of a quadratic polynomial.
# Inputs : The coefficients a,b,c of the equation a*x^2 + b*x + c = 0
# Output : The two integer solutions.
#
# All numbers are 32 bit integers

.globl main

main:                    # Read all inputs and put them in floating point registers.

   li t1, 1 # a=1
li t2, -3 # b=-3
li t3, 2 # c=2
  
  
   # In the following lines all the necessary steps are taken to
   # calculate the discriminant of the quadratic equation
   # D = b^2 - 4*a*c
  
   mul t4,t2,t2        # t4 = t2*t2, where t2 holds b
   mul t5,t1,t3        # t5 = t1*t3, where t1 holds a and t3 holds c
   li a3, 4
   mul t5,t5,a3        # Multiply value of s0 with 4, creating 4*a*c
   sub t6,t4,t5        # Calculate D = b^2-4*a*c
  
  
   # calculating the integer square root by the equation x*x = D
   li s0, 1        # Square Root Partial Result, sqrt(D).
   mv s1,t6            # Move value in register t6 to register s1 for safety purposes.
  
   sqrtloop:           # calculating the integer square root of D

       mul s2, s0,s0
       bge s2, s1, endsqrt
       addi s0,s0, 1
j sqrtloop
  
  
   endsqrt:
       neg s2,t2       # calculate -b and save it to s2
       add s3,s2,s0    # Calculate -b+sqrt(D) and save it to s3
       sub s4,s2,s0    # Calculate -b-sqrt(D) and save it to s4
       li t0, 2        # Load constant number to integer register
       mul s5,t1,t0    # Calculate 2*a and save it to s5
       div s6,s3,s5    # Calculate first integer solution
       div s7,s4,s5    # Calculate second integer solution
      
   #Print the calculated solutions.

   li a0, 4           # Load print_string syscall code to register v0 for the 1st result string
   la a1, str1            # Load actual string to register a0
   ecall
  
   li a0 1           # Load print_int syscall code to register v0 for the 1st result string
   mv a1, s6           # Load actual integer to register a0
   ecall
  
   li a0, 4           # Load print_string syscall code to register v0 for the 1st result string
   la a1, str2           # Load actual string to register a0
   ecall
  
   li a0, 1
   mv a1, s7
   ecall
  
   li a0, 10
   ecall
  
.data
  
   str1: .asciiz "The first integer solution is: "
  
   str2: .asciiz "\nThe second integer solution is: "

0 0
Report
Answer #1

Solution:-

  1. Rewrite the program using instructions reordering to reduce the number of cycles needed to execute the program. Indicate the number of cycle reduction.

li t3, 2                  # c=2, moved li t1, 1             # a=1

li t2, -3          # b=-3

# In the following lines all the necessary steps are taken to # calculate the discriminant of the quadratic equation

# D = b^2 - 4*a*c

mul t5,t1,t3 # t5 = t1*t3, where t1 holds a and t3 holds c, moved

mul t4,t2,t2        # t4 = t2*t2, where t2 holds b li a3, 4               # moved

mul t5,t5,a3        # Multiply value of s0 with 4, creating 4*a*c

li s0, 1            # Square Root Partial Result, sqrt(D), moved sub t6,t4,t5     # Calculate D = b^2-4*a*c

. . . .

endsqrt:

li t0, 2 # Load constant number to integer register, movedd

neg s2,t2 # calculate -b and save it to s2

mul s5,t1,t0   # Calculate 2*a and save it to s5, moved

The moved instructions are marked with a ‘moved’ in the comments. The previous code had 2 stalls for every hazard. After reordering the code, I have been able to reduce it to 1 stall for every hazard. Therefore, the number of cycles added due to stalls have now been divided by 2.


-----------------------------------------------------------------------------------------------------------------

.         2. Describe how forwarding would affect the execution of the program.

          Solution:-

With the addition of forward, there will only be one stall required to execute the program. Thus, reducing the total number of cycles even more.

---------------------------------------------------------------------------------------------

Know the answer?
Add Answer to:
Assignment 4 File “quad_sol.s” contains a quadratic polynomial solver, which calculates the integer solution of a quadratic polynomial equation. 1. Rewrite the program using instructions reordering to...
Your Answer: Your Name: What's your source?
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
  • MIPS Fibonacci print console

    I have this code that prints out the code correctly. But How can I only print out the last 5 numbers?I know I have to use some kind of branch condition before I print to the console to make sure it doesn't print the previous numbersCode is below:# Compute first twelve Fibonacci numbers and put in array, then printmain:.datafibs: .word 0 : 12 # "array" of 12 words to contain fib valuessize: .word 12 # size of "array".textla $t0, fibs...

  • Please comment the MIPS code to help me understand. Here is what the code accomplishes. Here...

    Please comment the MIPS code to help me understand. Here is what the code accomplishes. Here is the code, partially commented. .data Matrix: .word 41,45,5, 34,8, 15,16,23,44,48,12,32,18,47,22,8,22 .word 46,40,42,33,13,38,27,6, 29,25,18,40,47,22,26,14,3 .word 7, 48,35,9, 43,38,9, 49,28,25,42,5, 44,10,5, 38,14 .word 46,33,16,6, 13,20,31,1, 8, 17,1, 47,28,46,14,28,7 .word 32,2, 48,25,41,29,14,39,43,46,3, 39,32,49,41,28,46 .word 5, 43,2, 48,13,4, 33,41,32,19,9, 25,30,22,2, 9, 40 .word 14,47,22,18,47,3, 35,44,18,6, 33,22,11,6, 47,50,4 .word 28,34,20,30,18,27,38,5, 26,40,37,23,16,13,37,8,7 .word 48,38,39,12,10,39,23,20,21,20,33,16,24,21,25,3,46 .word 49,38,40,38,13,47,5, 13,4, 13,23,26,12,30,29,29, 3 .word 8, 20,10,13,31,7, 12,41,12,21,28,26,43,14,35,10,19 .word 49,33,25,26,24,29,46,22,7, 5, 15,41,10,31,19,41,27 .word 48,9,...

  • 5. Consider the SPIM code below. globl main .text main: ori $t1, $0, 10 ori $t2,...

    5. Consider the SPIM code below. globl main .text main: ori $t1, $0, 10 ori $t2, $0, 11 add $t3, $t1,$t2 move $t4, $t3 The following image shows a screen shot of QtSPIM page when this program is loaded, and executed in step-by step fashion. Current instruction is highlighted. Data Text x Text Regs Int Regs [16] Int Regs [16] PC = 400028 EPC 0 Cause = 0 BadAddr = 0 Status = 3000ff10 HI LO = 0 = 0...

Free Homework 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.