For Programmers: Free Programming Magazines  


Home > Archive > A86 Assembler > April 2005 > Please help explain hello world program to newbie....









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Please help explain hello world program to newbie....
spamtrap@crayne.org

2005-04-12, 8:55 pm

After much work I understand this hello world program....

segment .data
message db 'Hello World!', 10, 0

section .text
global main
extern printf
main:
push message
call printf
add esp, 4
ret

For fun I used gcc to create similar assembly and got this output that
I don't understand.
My primary question is what is all this stuff with %ebp and %eax???....

.file "hello_world.c"
.intel_syntax
.section .rodata
..LC0:
.string "Hello World!\n"
.text
..globl main
.type main, @function
main:
push %ebp
mov %ebp, %esp
sub %esp, 8
and %esp, -16
mov %eax, 0
sub %esp, %eax
sub %esp, 12
push OFFSET FLAT:.LC0
call printf
add %esp, 16
leave
ret
.size main, .-main
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"

Scott Moore

2005-04-13, 8:55 am

spamtrap@crayne.org wrote:
> After much work I understand this hello world program....
>
> segment .data
> message db 'Hello World!', 10, 0
>
> section .text
> global main
> extern printf
> main:
> push message
> call printf
> add esp, 4
> ret
>
> For fun I used gcc to create similar assembly and got this output that
> I don't understand.
> My primary question is what is all this stuff with %ebp and %eax???....
>
> .file "hello_world.c"
> .intel_syntax
> .section .rodata
> .LC0:
> .string "Hello World!\n"
> .text
> .globl main
> .type main, @function
> main:
> push %ebp
> mov %ebp, %esp
> sub %esp, 8
> and %esp, -16
> mov %eax, 0
> sub %esp, %eax
> sub %esp, 12
> push OFFSET FLAT:.LC0
> call printf
> add %esp, 16
> leave
> ret
> .size main, .-main
> .section .note.GNU-stack,"",@progbits
> .ident "GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"
>


It is framing. The ebp is used as the local framing pointer.

randyhyde@earthlink.net

2005-04-13, 8:55 am

>>My primary question is what is all this stuff with %ebp and
%eax???....

You might want to start here:

http://webster.cs.ucr.edu

Check out the "Art of Assembly Language".
Also, check out Jonathon Bartlett's e-book on Gas which can also be
found on Webster, if you're really dying to learn the difference
between %eax and eax.
Cheers,
Randy Hyde

KVP

2005-04-13, 3:55 pm

spamtrap@crayne.org wrote:
> After much work I understand this hello world program....
>
> segment .data
> message db 'Hello World!', 10, 0
> section .text
> global main
> extern printf
> main:
> push message
> call printf
> add esp, 4
> ret
>
> For fun I used gcc to create similar assembly and got this output that
> I don't understand.
> My primary question is what is all this stuff with %ebp and %eax???....
>
> .file "hello_world.c"
> .intel_syntax
> .section .rodata
> .LC0:
> .string "Hello World!\n"
> .text
> .globl main
> .type main, @function
> main:


main()'s entry code (creates a c style stack frame):

> push %ebp
> mov %ebp, %esp


reserves 0 bytes for local variables, and cache aligns the stack

> sub %esp, 8
> and %esp, -16
> mov %eax, 0
> sub %esp, %eax


printf("Hello World!\n");
with some cache line alignement again
then removes string offset and alignement

> sub %esp, 12
> push OFFSET FLAT:.LC0
> call printf
> add %esp, 16


removes a c style stack frame (aka. mov %esp, %ebp + pop %ebp)
and returns

> leave
> ret
> .size main, .-main
> .section .note.GNU-stack,"",@progbits
> .ident "GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"


Imho you either have a slightly misconfigured system or forgot to use
optimization, since some of the operations here could be eliminated
(removed) with a slightly higher gcc optimization setting.

Viktor

randyhyde@earthlink.net

2005-04-17, 8:55 am

>>My primary question is what is all this stuff with %ebp and
%eax???....

You might want to start here:

http://webster.cs.ucr.edu

Check out the "Art of Assembly Language".
Also, check out Jonathon Bartlett's e-book on Gas which can also be
found on Webster, if you're really dying to learn the difference
between %eax and eax.
Cheers,
Randy Hyde

Scott Moore

2005-04-17, 8:55 am

spamtrap@crayne.org wrote:
> After much work I understand this hello world program....
>
> segment .data
> message db 'Hello World!', 10, 0
>
> section .text
> global main
> extern printf
> main:
> push message
> call printf
> add esp, 4
> ret
>
> For fun I used gcc to create similar assembly and got this output that
> I don't understand.
> My primary question is what is all this stuff with %ebp and %eax???....
>
> .file "hello_world.c"
> .intel_syntax
> .section .rodata
> .LC0:
> .string "Hello World!\n"
> .text
> .globl main
> .type main, @function
> main:
> push %ebp
> mov %ebp, %esp
> sub %esp, 8
> and %esp, -16
> mov %eax, 0
> sub %esp, %eax
> sub %esp, 12
> push OFFSET FLAT:.LC0
> call printf
> add %esp, 16
> leave
> ret
> .size main, .-main
> .section .note.GNU-stack,"",@progbits
> .ident "GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"
>


It is framing. The ebp is used as the local framing pointer.

KVP

2005-04-17, 8:55 am

spamtrap@crayne.org wrote:
> After much work I understand this hello world program....
>
> segment .data
> message db 'Hello World!', 10, 0
> section .text
> global main
> extern printf
> main:
> push message
> call printf
> add esp, 4
> ret
>
> For fun I used gcc to create similar assembly and got this output that
> I don't understand.
> My primary question is what is all this stuff with %ebp and %eax???....
>
> .file "hello_world.c"
> .intel_syntax
> .section .rodata
> .LC0:
> .string "Hello World!\n"
> .text
> .globl main
> .type main, @function
> main:


main()'s entry code (creates a c style stack frame):

> push %ebp
> mov %ebp, %esp


reserves 0 bytes for local variables, and cache aligns the stack

> sub %esp, 8
> and %esp, -16
> mov %eax, 0
> sub %esp, %eax


printf("Hello World!\n");
with some cache line alignement again
then removes string offset and alignement

> sub %esp, 12
> push OFFSET FLAT:.LC0
> call printf
> add %esp, 16


removes a c style stack frame (aka. mov %esp, %ebp + pop %ebp)
and returns

> leave
> ret
> .size main, .-main
> .section .note.GNU-stack,"",@progbits
> .ident "GCC: (GNU) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)"


Imho you either have a slightly misconfigured system or forgot to use
optimization, since some of the operations here could be eliminated
(removed) with a slightly higher gcc optimization setting.

Viktor

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com