C - what is the return value of a semicolon? -


im curious following example

#include<stdio.h> int test(); int test(){      //    int = 5;      //    int b = a+1;      return ; } int main(){      printf("%u\n",test());      return 0; } 

i compiled 'gcc -wall -o semicolon semicolon.c' create executable , 'gcc -wall -s semicolon.c' assembler code is:

    .file   "semicolon.c"     .text .globl test     .type   test, @function test:     pushl   %ebp     movl    %esp, %ebp     subl    $4, %esp     leave     ret     .size   test, .-test     .section        .rodata  .lc0:     .string "%u\n"     .text  .globl main     .type   main, @function  main:     leal    4(%esp), %ecx     andl    $-16, %esp     pushl   -4(%ecx)     pushl   %ebp     movl    %esp, %ebp     pushl   %ecx     subl    $20, %esp     call    test     movl    %eax, 4(%esp)     movl    $.lc0, (%esp)     call    printf     movl    $0, %eax     addl    $20, %esp     popl    %ecx     popl    %ebp     leal    -4(%ecx), %esp     ret     .size   main, .-main     .ident  "gcc: (ubuntu 4.3.3-5ubuntu4) 4.3.3"     .section        .note.gnu-stack,"",@progbits 

since im not such assembler pro, know printf prints in eax dont understand 'movl %eax, 4(%esp)' means assume fills eax before calling test value then? means 4(%esp) , value of esp mean?

if uncomment lines in test() printf prints 6 - written in eax ^^

your assembly language annotated:

test:     pushl   %ebp        # save frame pointer     movl    %esp, %ebp  # new frame pointer.     subl    $4, %esp    # allocate local space on stack.     leave               # restore old frame pointer/stack     ret 

note nothing in test touches eax.

.size   test, .-test .section        .rodata  .lc0: .string "%u\n" .text  .globl main .type   main, @function main: leal    4(%esp), %ecx      # point past return address. andl    $-16, %esp         # align stack. pushl   -4(%ecx)           # push return address. pushl   %ebp               # save frame pointer movl    %esp, %ebp         # new frame pointer. pushl   %ecx               # save old top of stack. subl    $20, %esp          # allocate local space (for printf parameters , ?). call    test               # call test. 

note @ point, nothing has modified eax. whatever came main still here.

movl    %eax, 4(%esp)      # save eax printf argument. movl    $.lc0, (%esp)      # send format string. call    printf             # duh. movl    $0, %eax           # return 0 main. addl    $20, %esp          # deallocate local space. popl    %ecx               # restore old top of stack. popl    %ebp               # , old frame pointer. leal    -4(%ecx), %esp     # fix stack pointer, ret 

so, gets printed out whatever came in main. others have pointed out undefined: depends on startup code (or os) has done eax previously.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -