Note that there are some explanatory texts on larger screens.

plurals
  1. POGCC's assembly output of an empty program on x86, win32
    primarykey
    data
    text
    <p>I write empty programs to annoy the hell out of stackoverflow coders, NOT. I am just exploring the gnu toolchain.</p> <p>Now the following might be too deep for me, but to continuie the empty program saga I have started to examine the output of the C compiler, the stuff GNU as consumes.</p> <pre><code>gcc version 4.4.0 (TDM-1 mingw32) </code></pre> <p><em>test.c:</em></p> <pre><code>int main() { return 0; } </code></pre> <p><em>gcc -S test.c</em></p> <pre><code> .file "test.c" .def ___main; .scl 2; .type 32; .endef .text .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl %esp, %ebp andl $-16, %esp call ___main movl $0, %eax leave ret </code></pre> <p>Can you explain what happens here? Here is my effort to understand it. I have used the <code>as</code> manual and my minimal x86 ASM knowledge:</p> <ul> <li><code>.file "test.c"</code> is the directive for the logical filename.</li> <li><code>.def</code>: according to the docs <em>"Begin defining debugging information for a symbol name"</em>. What is a symbol (a function name/variable?) and what kind of debugging information?</li> <li><code>.scl</code>: docs say <em>"Storage class may flag whether a symbol is static or external"</em>. Is this the same <em>static</em> and <em>external</em> I know from C? And what is that '2'?</li> <li><code>.type</code>: stores the parameter <em>"as the type attribute of a symbol table entry"</em>, I have no clue.</li> <li><code>.endef</code>: no problem.</li> <li><code>.text</code>: Now this is problematic, it seems to be something called section and I have read that its the place for code, but the docs didn't tell me too much.</li> <li><code>.globl</code> <em>"makes the symbol visible to ld."</em>, the manual is quite clear on this.</li> <li><code>_main:</code> This might be the starting address (?) for my main function</li> <li><code>pushl_</code>: A long (32bit) push, which places EBP on the stack</li> <li><code>movl</code>: 32-bit move. Pseudo-C: <code>EBP = ESP;</code></li> <li><code>andl</code>: Logical AND. Pseudo-C: <code>ESP = -16 &amp; ESP</code>, I don't really see whats the point of this.</li> <li><code>call</code>: Pushes the IP to the stack (so the called procedure can find its way back) and continues where <code>__main</code> is. (what is __main?)</li> <li><code>movl</code>: this zero must be the constant I return at the end of my code. The MOV places this zero into EAX.</li> <li><code>leave</code>: restores stack after an ENTER instruction (?). Why?</li> <li><code>ret</code>: goes back to the instruction address that is saved on the stack</li> </ul> <p><strong>Thank you for your help!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    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