Question: 1 . Write a main program, testcalc.c ( this program will contain the main ( ) function ) , that will test calc.c . (
Write a main program, testcalc.c this program will contain the mainfunction that will test calc.chint: testcalc.c file will use the function calc defined in
calc.c
The main program should declare three int variables, x y and z and initialize them to and respectively. It should then call calc with these parameters and print x y z and the result using the format specification:
xd yd zd resultd
Before running the program, answer the following question:
a What output should be generated by this program?
b The program will need a prototype of the function calc. Compile your program with:
cc Oo testcalc testcalc.c calc.s
Test your program by running the binary testcalc to make sure that it works correctly. Turn in the file testcalc.c
# Function: int calcint x int y int z
# This function calculates: x y z
# Parameters:
# x First argument offset ebp
# y Second argument offset ebp
# z Third argument offset ebp
# Return value: The result of the calculation is stored in eax.
globl calc
calc:
pushlebp
movlesp, ebp
movlebpeax
movlebpecx
movlebpedx
lealecx, ecx, ecx
# Calculate z
lealedx, edx, edx
lealedx, edx, edx
addledx, edx
# Add x y
addlecx, eax
# Add x y z
addledx, eax
# Function Epilogue
poplebp
ret
pushlebp
movlesp, ebp
movlebpeax # Load the first argument x into the eax register.
movlebpecx # Load the second argument y into the ecx register.
movlebpedx # Load the third argument z into the edx register.
lealecx, ecx, ecx # Compute y by adding y y store in ecx.
lealedx, edx, edx # Compute z
lealedx, edx, edx # Compute z by adding z z
addledx, edx # Compute z by adding z z
addlecx, eax # Add y to x store in eax
addledx, eax # Add z to the result store in eax
poplebp # Restore the old base pointer.
ret # Return the result, which is now in eax.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
