Question
I need help with the roundD function. Please explain steps through comments. Thank you. Parameters: You can use a mixture of C code and assembly
I need help with the roundD function. Please explain steps through comments.
Thank you.
Parameters:
You can use a mixture of C code and assembly code, but all floating point calculations (including comparisons) must be done in assembly language. You should use C only for support tasks like defining and initializing local variables, returning a value from the function, etc
Your assembly code should work in the following way:
1. Load values from the function's arguments and local variables onto the floating point stack
2. Perform the required calculation entirely within the floating point stack.
3. Store the calculated result from the floating point stack to a local variable
4. Return that value for that local variable with a C return statement
The IA-32 Architecture includes the floating point instructions needed to implement these functions (they all start with the letter 'F'). In your assembly code, you are not allowed to use GAS syntax such as %0 or any specifier other than "=m" in an output or "m" in an input.
Instructions for roundD function:
#include #include #include
#define RND_CTL_BIT_SHIFT 10
// floating point rounding modes: IA-32 Manual, Vol. 1, p. 4-20 typedef enum { ROUND_NEAREST_EVEN = 0
// do not change anything above this comment
double roundD(double n, RoundingMode roundingMode) // this is the function I need help with { // here you will set the FPU rounding mode as specified, // and then round n to the nearest integer in the specified mode
return 0.0; }
double hullSpeed(double lgth) { // here you will compute the hull speed for the specified boat length, // implementing the numerical computation in GNU Assembly Language // through use of FPU instructions (do NOT call sqrt() from math.h) _asm_( "fldl %1 " //st(0)=>st(1), st(0)=lgth . FLDL loads double(float) "fsqrt " //st(0) = square root st(0) "fmulp " //Multiplies st(0) and st(1) (1.34). Result in st(0) : "=&t" (returnHullSpeed) : "m" (lgth), "0" (1.34)); return returnHullSpeed; }
// do not change anything below this comment, except for printing out your name
double hullSpeedC(double lgth) { // this C implementation of the hull speed computation is provided // so you can compare its correct results with the results of your code
return 1.34 * sqrt(lgth); }
void printRounded(char *label, RoundingMode roundingMode, double lgth, double hullSpd, double hullSpdC) { printf("%s hullSpeed(%.0f) = %.0f, %.0f ", label, lgth, roundD(hullSpd, roundingMode), roundD(hullSpdC, roundingMode)); }
int main (int argc, char **argv) { double lgth; double hullSpd, hullSpdC; if (argc != 2) { printf("need 1 argument: boat length "); return -1; } lgth = atof(argv[1]); hullSpd = hullSpeed(lgth); hullSpdC = hullSpeedC(lgth);
printf("hullSpeed(%.0f) = %.3f, %.3f ", lgth, hullSpd, hullSpdC); printRounded("round even:", ROUND_NEAREST_EVEN, lgth, hullSpd, hullSpdC); printRounded("round down:", ROUND_MINUS_INF, lgth, hullSpd, hullSpdC); printRounded("round up: ", ROUND_PLUS_INF, lgth, hullSpd, hullSpdC); printRounded("round zero:", ROUND_TOWARD_ZERO, lgth, hullSpd, hullSpdC);
return 0; }
In the function named round write assembly code that rounds its first argument to an integer value according to the rounding mode specified by its second argument. The function prototype is: double roundD (double n, RoundingMode roundingMode); RoundingMode is defined in the starter code. You will need to set the Rounding Control field of the FPU Control Word in accordance with the roundingMode argument. You need to do this with assembly code -- do not use library functions. You will also need to restore the original value of the FPU's Rounding Control field before round returns. Do not use finit or fninit for this. The original value is whatever the value was when roundD was called, which could have been set by the calling function to some arbitrary value, not necessarily what finit produces. In the function named round write assembly code that rounds its first argument to an integer value according to the rounding mode specified by its second argument. The function prototype is: double roundD (double n, RoundingMode roundingMode); RoundingMode is defined in the starter code. You will need to set the Rounding Control field of the FPU Control Word in accordance with the roundingMode argument. You need to do this with assembly code -- do not use library functions. You will also need to restore the original value of the FPU's Rounding Control field before round returns. Do not use finit or fninit for this. The original value is whatever the value was when roundD was called, which could have been set by the calling function to some arbitrary value, not necessarily what finit producesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started