Question: 1 . Write a main program, testcalc.c ( this program will contain the main ( ) function ) , that will test calc.c . (

1. Write a main program, testcalc.c (this program will contain the main()function), that will test calc.c.(hint: 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 5,7, and 13, respectively. It should then call calc with these parameters and print x, y, z, and the result using the format specification:
"x=%d, y=%d, z=%d, result=%d
".
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 -O1-o 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 calc(int x, int y, int z)
# This function calculates: x +3*y +19*z
# Parameters:
# x -> First argument (offset 8(%ebp))
# y -> Second argument (offset 12(%ebp))
# z -> Third argument (offset 16(%ebp))
# Return value: The result of the calculation is stored in %eax.
.globl calc
calc:
pushl%ebp
movl%esp, %ebp
movl8(%ebp),%eax
movl12(%ebp),%ecx
movl16(%ebp),%edx
leal(%ecx, %ecx, 2),%ecx
# Calculate 19* z
leal(%edx, %edx, 2),%edx
leal(%edx, %edx, 4),%edx
addl%edx, %edx
# Add x +(3* y)
addl%ecx, %eax
# Add x +(3* y)+(19* z)
addl%edx, %eax
# Function Epilogue
popl%ebp
ret
pushl%ebp
movl%esp, %ebp
movl8(%ebp),%eax # Load the first argument (x) into the %eax register.
movl12(%ebp),%ecx # Load the second argument (y) into the %ecx register.
movl16(%ebp),%edx # Load the third argument (z) into the %edx register.
leal(%ecx, %ecx, 2),%ecx # Compute 3* y by adding y +2*y, store in %ecx.
leal(%edx, %edx, 2),%edx # Compute 3* z.
leal(%edx, %edx, 4),%edx # Compute 15* z by adding 3*z +12*z.
addl%edx, %edx # Compute 19* z by adding 15*z +4*z.
addl%ecx, %eax # Add 3* y to x (store in %eax).
addl%edx, %eax # Add 19* z to the result (store in %eax).
popl%ebp # 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!