Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Remember that you only need to do one of the assignments below. In each of these assignments, you will be asked to write a math

Remember that you only need to do one of the assignments below.

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

********************************************************** * THE USE OF ANY PROHIBITED SYNTAX IN YOUR ASSEMBLY CODE * * WILL RESULT IN A SIGNIFICANT LOSS OF POINTS. * **********************************************************

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

REGULAR VERSION

In hullSpeed(), write FPU assembly code to calculate the "hull speed" of a boat according to the formula 1.34*sqrt(lgth) where lgth is the length in feet of the boat at the waterline, and the computed speed is specified in knots.

Start with the provided code file a02.c. Upload your code as a02.c.

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

#include

#include

#include

// do not change anything above this comment

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

}

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_2

Step: 3

blur-text-image_3

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

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions