Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We will examine code generated by GCC for a function that has a structure as a parameter and returns a structure as the result of

We will examine code generated by GCC for a function that has a structure as a parameter and returns a structure as the result of the function. You have learned that a function returns its result to the caller by loading the %eax or %rax register. What if we want to return something like a structure that doesnt fit in a register. How does that happen. You will discover the solution by examining the assembly code. You learned that by convention for the x86-64 architecture the first 6 parameters to a function are passed in 6 designed resisters. It is also true, however, that sometimes we stil pass some parameters through the stack as we saw in the IA32 calling conventions. By examining the C and assembly provided you will discover how a structure is passed to a function and also how a function returns a structure as its result. Given these two codes: typedef struct { long a[2]; long *p; } strA; typedef struct { long u[2]; long q; } strB; strB p(strA s) { strB r; r.u[0] = s.a[1]; r.u[1] = s.a[0]; r.q = *s.p; return r; } long e(long x, long y, long z) { strA s; s.a[0] = x; s.a[1] = y; s.p = &z; strB r = p(s); return r.u[0] + r.u[1] + r.q; } p: movq %rdi, %rax movq 24(%rsp), %rdx movq (%rdx), %rdx movq 16(%rsp), %rcx movq %rcx, (%rdi) movq 8(%rsp), %rcx movq %rcx, 8(%rdi) movq %rdx, 16(%rdi) ret e: subq $104, %rsp movq %rdx, 24(%rsp) leaq 24(%rsp), %rax movq %rdi, (%rsp) movq %rsi, 8(%rsp) movq %rax, 16(%rsp) leaq 64(%rsp), %rdi call p movq 72(%rsp), %rax addq 64(%rsp), %rax addq 80(%rsp), %rax addq $104, %rsp ret solve: A. We can see on line 2 of function e that it allocates 104 bytes on the stack. Diagram the stack frame for function e, showing the values that it stores on the stack prior to calling function p. In other words, diagram what the stack looks like at the point e calls p. You can draw your diagram of the stack in your ASCII text file for your answer submission by creating a table of two columns using the characters _ and |. One column shows the offsets from the stack pointer %rsp and the other column can be used for an annotation indicating the contents of the locations are. B. What value does function e pass in its call to p? C. How does the code for function p access the elements of structure arguments s? D. How does the code for function p set the fields of result structure r? E. Complete your diagram of the stack frame for function e, showing how function e accesses the elements of structure r following the return from function p. F. What general principles can you discern about how structure values are passed as function arguments and how they are returned as function results

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

Transactions On Large Scale Data And Knowledge Centered Systems Iv Special Issue On Database Systems For Biomedical Applications Lncs 6990

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Christian Bohm ,Johann Eder ,Claudia Plant

2011th Edition

3642237398, 978-3642237393

More Books

Students also viewed these Databases questions