Examine the code generated by GCC for functions that have st

Examine the code generated by GCC for functions that have structures as arguments and return values, and from this see how these language features are typically implemented. The following C code has a function word_sum having structures as argument and return values, and a function prod that calls process: typedef struct {long a[2]; long *p;} strA; typedef struct {long u[2]; long q;} strB; strB process(strA s) {strB r.u[0] = s.a[1]; r.u[1] = s.a[0]; r.q = *s.p; return r; long eval (long x, long y, long z) {strA s; s.a[0] = x; s.a[1] = y; s.p = &z; strB r = process(s); return r.u[0] + r.u[1] + r.q;} GCCgenerates the following code for these two functions: strB process(strA s) process: 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 long eval (long x, long y, long z) x in %rdi, y in %rsi, z in %rdx eval: 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 process movq 72(%rsp), %rax addq 64(%rsp), %rax addq 80(%rsp), %rax addq $104, %rsp ret We can see on line 2 of function eval that it allocates 104 bytes on the stack. Diagram the stack frame for eval, showing the values that it stores on the stack prior to calling process. What value does eval pass in its call to process? How does the code for process access the elements of structure argument s? How does the code for process set the fields of result structure r? Complete your diagram of the stack frame for eval, showing how eval accesses the elements of structure r following the return from process. What general principles can you discern about how structure values are passed as function arguments and how they are returned as function results?

Solution

Answer F

In C, structure can be passed to functions by two methods:

Passing structure by value

A structure variable can be passed to the function as an argument like a normal variable. The whole structure is passed to another function with all members and their values..

void display(struct distance d1);

Passing structure by reference

The memory address of a structure variable is passed to function while passing it by reference. It means only the address of the structure is passed to another function. Here, changes made to the structure variable inside function definition reflects in the originally passed structure variable.

Return a `struct` from a function in C

It is possible to return structure from a function. An example is given below.

 Examine the code generated by GCC for functions that have structures as arguments and return values, and from this see how these language features are typicall

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site