Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Page 150 of PC Assembly Tutorial on <a href="http://www.drpaulcarter.com/pcasm/" rel="nofollow">http://www.drpaulcarter.com/pcasm/</a> has a clear explanation about how C allows a function to return a struct:</p> <blockquote> <p>C also allows a structure type to be used as the return value of a func- tion. Obviously a structure can not be returned in the EAX register. Different compilers handle this situation differently. A common solution that compilers use is to internally rewrite the function as one that takes a structure pointer as a parameter. The pointer is used to put the return value into a structure defined outside of the routine called.</p> </blockquote> <p>I use the following C code to verify the above statement:</p> <pre><code>struct person { int no; int age; }; struct person create() { struct person jingguo = { .no = 1, .age = 2}; return jingguo; } int main(int argc, const char *argv[]) { struct person result; result = create(); return 0; } </code></pre> <p>Use "gcc -S" to generate assembly for this piece of C code:</p> <pre><code> .file "foo.c" .text .globl create .type create, @function create: pushl %ebp movl %esp, %ebp subl $16, %esp movl 8(%ebp), %ecx movl $1, -8(%ebp) movl $2, -4(%ebp) movl -8(%ebp), %eax movl -4(%ebp), %edx movl %eax, (%ecx) movl %edx, 4(%ecx) movl %ecx, %eax leave ret $4 .size create, .-create .globl main .type main, @function main: pushl %ebp movl %esp, %ebp subl $20, %esp leal -8(%ebp), %eax movl %eax, (%esp) call create subl $4, %esp movl $0, %eax leave ret .size main, .-main .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3" .section .note.GNU-stack,"",@progbits </code></pre> <p>The stack before call create:</p> <pre><code> +---------------------------+ ebp | saved ebp | +---------------------------+ ebp-4 | age part of struct person | +---------------------------+ ebp-8 | no part of struct person | +---------------------------+ ebp-12 | | +---------------------------+ ebp-16 | | +---------------------------+ ebp-20 | ebp-8 (address) | +---------------------------+ </code></pre> <p>The stack right after calling create:</p> <pre><code> +---------------------------+ | ebp-8 (address) | +---------------------------+ | return address | +---------------------------+ ebp,esp | saved ebp | +---------------------------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload