Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program takes two side values as arguments. Use even number values; I will be testing the code with side values between 1 and 100

The program takes two side values as arguments. Use even number values; I will be testing the code with side values between 1 and 100 that are even. There are 3 functions inside final.c. Read the instructions for each function and either write assembly code or improve the performance of the code. 35 or 50 points max: The first function, write assembly code to compute the area of a right triangle: 1/2*a*b where a and b are the two legs of the triangle. Use integer arithmetic and don't worry about truncation by division. I will be using even values for a and b. Also, you will get only 35 points if you use the idiv instruction and a full 50 points if you avoid it.

*This must be in assembly. Do not write any C code. *This should be less than 10 lines long including any input and output. Writing more than this will reduce the number of points.

36 or 46 or 50 points max: The second function is a copy of the third function. Leave this function alone. This is for benchmarking your code. In the third function, improve the inner loop so that the function runs at least 50% faster for 35 points, at least 70% faster for 45 points or at least 75% faster for 50 points. Things that you need to do:

*Make sure that the sum for both functions are equal. *Leave the outer loop alone. Don't use i or RUNS value inside the outer loop. *If you want to, for debugging purposes, change the value of RUNS and SIZE you can do so but do make sure to return them to their original values. *Don't use any special math formulas as a 'short-cut' to calculation. However, you can change the way the current formula is done as long as the sum is the same as the original. *YOUR CODE MUST ACCESS EVERY ELEMENT IN THE ARRAY. THIS IS IMPORTANT. You will get substantial points off if it doesn't. *You can create your own variables above the loops and add other lines inside and outside the loops as long as the above holds true.

Start with file.c

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

file.c

#include

#include

#include // for times()

#include // for clock()

char * name = "I. FORGOT";

/* I will be using only the two functions

* you will be changing when grading. So

* don't write code you want to keep outside

* of the two functions (they won't be copied over)

*/

// These can be changed for debugging purposes

// but DO CHANGE THEM TO THE ORIGINAL VALUES

// RUNS = 10000

// SIZE = 10000

#define RUNS 10000

#define SIZE 10000

/* write a function using gcc inline assembly

* that calculates the area of a right triangle.

* the formula for doing so is:

* area = 1/2 x side1 x side2

*/

int calcArea(int side1, int side2)

{

int area = 1.0;

// PUT YOUR ASSEMBLY CODE HERE

// DON'T WRITE ANY OTHER

// C CODE

return area;

}

/* this is the original function

* DON"T TOUCH THIS FUNCTION

* it's for reference only

*/

int sumAreaOrig(int a, int b, int *arr)

{

int sum, i, j;

for(i=0; i

sum = 0;

for(j=0; j

sum += arr[j]*calcArea(a,b) + j;

}

}

return sum;

}

/* PLEASE MODIFY THIS FUNCTION

* make sure it returns the same

* sum as the original function

*/

int sumAreaImprove(int a, int b, int *arr)

{

/* you can add any variables here */

int sum, i, j;

/* leave this outer loop alone*/

for(i=0; i < RUNS; i++)

{

/* you can change this inner loop */

/* but remember it must sum over all values */

sum = 0;

for(j=0; j

sum += arr[j]*calcArea(a,b) + j;

}

}

return sum;

}

/* You can modify main for debugging

* but PLEASE RETURN IT TO THE ORIGINAL

* CODE. I will be using only your two functions

* above when grading. So don't write code

* you want to keep outside of the above two

* functions (they won't be copied over)

*/

int main(int argc, char **argv)

{

struct tms origStart, origStop, impStart, impStop;

double origTime, impTime;

double difference;

int area, sumOrig, sumImprove;

int side1, side2;

int i;

int array[SIZE];

for(i=0; i

array[i] = i;

if(argc < 3) {

printf("Need to provide two side measurements ");

exit(-1);

} else {

side1 = atoi(argv[1]);

side2 = atoi(argv[2]);

}

printf("side1: %d side2: %d ", side1, side2);

printf("Name: %s ",name);

area = calcArea(side1, side2);

printf("the area is: %d ", area);

//start time here

times(&origStart);

sumOrig = sumAreaOrig(side1, side2, array);

times(&origStop);

//end time here

printf("results from orignal: %d ", sumOrig);

// start time here

times(&impStart);

sumImprove = sumAreaImprove(side1, side2, array);

times(&impStop);

// end time here

printf("results from improved: %d ", sumImprove);

//do time calculations here

origTime = (origStop.tms_utime - origStart.tms_utime);

impTime = (impStop.tms_utime - impStart.tms_utime);

printf("original user time: %f ",origTime);

printf("improved user time: %f ", impTime);

difference = ((origTime - impTime)/origTime)*100.0;

printf("percent difference: %f ", difference);

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

More Books

Students also viewed these Databases questions

Question

Question Can plan participants borrow from a VEBA?

Answered: 1 week ago