Question

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
Add a comment Improve this question Transcribed image text
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.

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

Add a comment
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:

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
  • MIPS Insertion program.........I could really use some help ASAP

    I have this MIPS program and I'm having trouble with it. This program is user inputs numbers until zero and sorts and print the numbers in order. Please soove this issue. You can use any sorting algorithm except bubble sort.  Need it as soon as possible. Here is the code:.datanum: .word 0space: .byte ' ' .text main:  # la $t0, val # loads val into a register  # li $t1, 0      #keeps track of how many numbers entered  la $a0,...

  • WRITE THE FOLLOWING CODE IN FLOATING POINT NUMBERS IN ASSEMBLY LANGUAGE USING MIPS IN MARS .data...

    WRITE THE FOLLOWING CODE IN FLOATING POINT NUMBERS IN ASSEMBLY LANGUAGE USING MIPS IN MARS .data prompt: .asciiz "\nMaximum number is : " prompt1: .asciiz "\nMinimum number is : " prompt2: .asciiz "\nRange of the array is : " size: .word 10 #load array array: .word 23, -12, 45, -32, 52, -72, 8, 13,22,876 .text #load address of array and size la $s4,array #load address of A lw $t0,size #load i to t0 jal getArrayRange li $v0, 4    la...

  • im trying to complete mips program code about a calculator program that can calculate integer addition...

    im trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler. im having hard times to debug this. The input is given to the array of Formula char (base address $ s0) in the form of a formula. The null character (\ 0, ASCII code 0) is placed at the end. The calculation result is given to the register $ s1 and the overflow is ignored. For example,...

  • I want to calculate Y[2]=X[3]+X[4] I'm not sure that I wrote right codes. .text main ....

    I want to calculate Y[2]=X[3]+X[4] I'm not sure that I wrote right codes. .text main . la $50, x #get base address of x la $s1, y #get base address of y lw $to, 12 ($50)#get data from memory of x[3] lw $t1 , 1 6($50) #get data from memory of x[4] add $t2, $t0, $t1 sw $t2, 8($51 ) #store result to y[2] li $v0, 10 #exit program syscall data x: word 5, 1, 17,-4, 6, 3 y: .word...

  • 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...

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