Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CS201 assignment. Is there anybody can help with the following assignment? Thanks in advance! instruction: In each of these assignments, you will be asked to

CS201 assignment.

Is there anybody can help with the following assignment? Thanks in advance!

instruction:

In each of these assignments, you will be asked to write a "math library" function. You will be writing the function in assembly language, using gcc's inline assembler. Class code files asm.c and cpuid.c are good models for how to write assembly code for gcc.

You can use a mixture of C code and assembly code for these assignments, 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 (local variables can be initialized first by C code as long as no calculations are performed). 2. Perform the required calculation entirely within the floating point stack. Using stack comments in this code will help you keep track of what's going on. (A stack comment is a comment that specifies what is supposed to be on the floating point stack after that instruction has executed. Or you can write stack comments to specify the situation before the instruction executes if you prefer.) 3. Store the calculated result from the floating point stack to a local variable 4. Return that the value of 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'). You will save yourself a lot of work if you carefully choose the right instructions. This is a large and powerful instruction set, and you should make use of that power instead of "re-inventing the wheel." In other words, don't spend a lot of time writing a lot of dumb code. Instead, spend your time thinking first, and you will be able to write a smaller amount of smart code.

In your assembly code, you are required to use only the syntax demonstrated in course file objectCode/asm.c. You are not allowed to use any other GAS syntax, such as %0, or any specifier other than "=m" in an "output" or "m" in an "input."

PREMIUM VERSION

This assignment includes all of the work from the Regular version plus this additional task:

In the function named roundD 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 roundD 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.

provided code:

#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 << RND_CTL_BIT_SHIFT, ROUND_MINUS_INF = 1 << RND_CTL_BIT_SHIFT, ROUND_PLUS_INF = 2 << RND_CTL_BIT_SHIFT, ROUND_TOWARD_ZERO = 3 << RND_CTL_BIT_SHIFT } RoundingMode;

// do not change anything above this comment

double roundD(double n, RoundingMode roundingMode) { // 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)

return 0.0; }

// 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;

printf("CS201 - Assignment 02 - I. Forgot "); 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; }

source.txt:

This program i.e. your program is running excellently but with one modification.

1) in the hullspeed(lgth) function you never used lgth, you can use that.

2) Make exe file of this program and run the file in the following way.

At the prompt you give the exe file name and one value i.e. boat length

followed by a space. Then this file will run.

#include #include #include double hullSpeed(double lgth) { return 0.0; } double hullSpeedC(double lgth) { return 1.34 * sqrt(lgth); } 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); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions

Question

Exposure to SQL desirable but not required

Answered: 1 week ago