Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It would really help to know what gcc version you are using and what libc. It looks like you have a very old gcc version or a strange platform or both. What's going on is some strangeness with calling conventions. I can tell you a few things:</p> <p>Save the frame pointer on the stack according to convention:</p> <pre><code>pushl %ebp movl %esp, %ebp </code></pre> <p>Make room for stuff at the old end of the frame, and round the stack pointer down to a multiple of 4 (why this is needed I don't know):</p> <pre><code>subl $8, %esp andl $-16, %esp </code></pre> <p>Through an insane song and dance, get ready to return 1 from <code>main</code>:</p> <pre><code>movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax movl %eax, -4(%ebp) movl -4(%ebp), %eax </code></pre> <p>Recover any memory allocated with <code>alloca</code> (GNU-ism):</p> <pre><code>call __alloca </code></pre> <p>Announce to libc that <code>main</code> is exiting (more GNU-ism):</p> <pre><code>call ___main </code></pre> <p>Restore the frame and stack pointers:</p> <pre><code>leave </code></pre> <p>Return:</p> <pre><code>ret </code></pre> <p>Here's what happens when I compile the very same source code with gcc 4.3 on Debian Linux:</p> <pre><code> .file "main.c" .text .p2align 4,,15 .globl main .type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx popl %ecx popl %ebp leal -4(%ecx), %esp ret .size main, .-main .ident "GCC: (Debian 4.3.2-1.1) 4.3.2" .section .note.GNU-stack,"",@progbits </code></pre> <p>And I break it down this way:</p> <p>Tell the debugger and other tools the source file:</p> <pre><code> .file "main.c" </code></pre> <p>Code goes in the text section:</p> <pre><code> .text </code></pre> <p>Beats me:</p> <pre><code> .p2align 4,,15 </code></pre> <p><code>main</code> is an exported function:</p> <pre><code>.globl main .type main, @function </code></pre> <p><code>main</code>'s entry point:</p> <pre><code>main: </code></pre> <p>Grab the return address, align the stack on a 4-byte address, and save the return address again (why I can't say):</p> <pre><code> leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) </code></pre> <p>Save frame pointer using standard convention:</p> <pre><code> pushl %ebp movl %esp, %ebp </code></pre> <p>Inscrutable madness:</p> <pre><code> pushl %ecx popl %ecx </code></pre> <p>Restore the frame pointer and the stack pointer:</p> <pre><code> popl %ebp leal -4(%ecx), %esp </code></pre> <p>Return:</p> <pre><code> ret </code></pre> <p>More info for the debugger?:</p> <pre><code> .size main, .-main .ident "GCC: (Debian 4.3.2-1.1) 4.3.2" .section .note.GNU-stack,"",@progbits </code></pre> <p>By the way, <code>main</code> is special and magical; when I compile</p> <pre><code>int f(void) { return 17; } </code></pre> <p>I get something slightly more sane:</p> <pre><code> .file "f.c" .text .p2align 4,,15 .globl f .type f, @function f: pushl %ebp movl $17, %eax movl %esp, %ebp popl %ebp ret .size f, .-f .ident "GCC: (Debian 4.3.2-1.1) 4.3.2" .section .note.GNU-stack,"",@progbits </code></pre> <p>There's still a ton of decoration, and we're still saving the frame pointer, moving it, and restoring it, which is utterly pointless, but the rest of the code make sense.</p>
 

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