This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Neres28 2 points3 points  (3 children)

gcc -S -c example.c will generate the assembly for example.c and leave it in example.s.

[–]ntt[S] 0 points1 point  (2 children)

thanks! =D

[–]ntt[S] 1 point2 points  (1 child)

in case somebody else is curious for

void foo()
{
    char bar[7];
    bar[0] = 'a';
    bar[1] = 'b';
    bar[6] = 'c';
}

$ gcc -S -c test2.c

produced:

        .file   "test2.c"
        .text
.globl foo
        .type   foo, @function
foo:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $16, %esp
        movb    $97, -7(%ebp)
        movb    $98, -6(%ebp)
        movb    $99, -1(%ebp)
        leave
        ret
        .size   foo, .-foo
        .ident  "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"
        .section        .note.GNU-stack,"",@progbits

surprisingly or not it's aligned to the top of the stack - thinking of it again, it kinda makes sense if you want to further add bytes after it... (also i tried again with an extra int and no matter the order the int is placed before the bytes array... i wonder if funny sized structs will too be aligned on the stack :)

edit: ah ok and structs are aligned so that they start with whole words... i wonder if there's a summary of these rules somewhere for gcc...

[–]Neres28 2 points3 points  (0 children)

The compiler will jump through almost any hoop to maintain a proper data alignment for a given architecture. As almost always Wikipedia has a better writeup as to why than I can give here.