Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3, Do g problem. 3.64 For this exercise, we will t arguments aahine the code gen

ID: 3700516 • Letter: 3

Question

3, Do g problem. 3.64 For this exercise, we will t arguments aahine the code generated by occ for and return values and a function prod typedet struet ( code generated by oce for functions that have values, and from this see how these language the features are typically implementedion as arguments and Ccode has a function word aua having structures as argument that calls vordaum: iat a int P: stri; int prodCins x, iat y) atr2 vord aun(atrl a1) str2 result result.sun . s1.a ? *s1.p; result.diff a1.a-si.p return result: atr2 s2: s2 vord.sum(a1): occ generates the following code for these two functions: 1 vord sun: 3 mov1 esp, zobp 1prod: 2 pushl %ebp 3 nov1 %esp, %ebp pushl %ebp subl leal leal s 6 , 8 9 S20,%esp 12(%ebp), %edx -8(%ebp),%ecx 8(%ebp) ,%eax %eax, 4(%esp) %edx, 8(%esp) %ecz , pushl %ebx ov1 aov1 novi nov! call vord sum subl movi inull leave ret 6 7 8 9 nov! movl movl movi 12(%ebp), %ebx 16(2ebp), edx (%edx), %edx %ebx,%ecx 10 (%esp) " 12 13 14 15 16 nov1 addl movi popl popl %ebp ret %ecx, %ebx, %edx, %ebx 4(%eax) %edx (teas) 12 13 14 15 16 $4, Xeop -4(%ebp), -8(%ebp), %eax %eax $4 The instruction ret $4 is like a normal return instruction, but it increments the stack pointer by 8 (4 for the return address plus 4 additional), rather than 4 A. We can see in lines 5-7 of the code for vord_sum that it appears as if three B. We can see in line 4 of the code for prod that 20 bytes are allocated in the C. How would you describe the general strategy for passing structures as argu- D. How would you describe the general strategy for handling a structure as a values are being retrieved from the stack, even though the function has only a single argument. Describe what these three values are. stack frame. These get used as five fields of 4 bytes each. Describe how each of these fields gets used. ments to a function? return value from a function?

Explanation / Answer

A.

When word_sum is called in prod three values are being pushed : s1.a, s1.*p, return address

These three values are used in word_sum as : return address, ebx = s1.a , edx = s1.*p

8(%ebp), %eax 12(%ebp), %ebx 16(%ebp), %edx

B.

Three values are used to pass above three values to word_sum and other two are used by result in word_sum to put values of result.sum & result.diff which will be later on used by s2.sum & s2.diff in prod

C.

Before passing an structure to a function:

- Allocate memory for all the structure variable, subl $NUM_BYTES, %esp

- Push all the struct variables onto the stack relative to ebp

- call function

D.

Returning struct from function:

- Before calling a function which return a structure, calling fucntion allocates memory for return structure

- Function being called will push all the resultant structure fields onto the stack based on ebp

- Pop ebp

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote