Tuesday, March 2, 2010

Hello World - 更小code size(2)

來trace 上一篇中用write()寫的Hello World程式吧:)

$ gcc fn.c -g -static -o fn
$ objdump -S fn
.... 省略 ....
int main(void)
{
80481f4: 55 push %ebp
80481f5: 89 e5 mov %esp,%ebp
80481f7: 83 ec 18 sub $0x18,%esp
80481fa: 83 e4 f0 and $0xfffffff0,%esp
80481fd: b8 00 00 00 00 mov $0x0,%eax
8048202: 83 c0 0f add $0xf,%eax
8048205: 83 c0 0f add $0xf,%eax
8048208: c1 e8 04 shr $0x4,%eax
804820b: c1 e0 04 shl $0x4,%eax
804820e: 29 c4 sub %eax,%esp
write(STDOUT_FILENO, HELLO_WORLD, sizeof(HELLO_WORLD));
8048210: c7 44 24 08 0d 00 00 movl $0xd,0x8(%esp)
8048217: 00
8048218: c7 44 24 04 c8 64 0a movl $0x80a64c8,0x4(%esp)
804821f: 08
8048220: c7 04 24 01 00 00 00 movl $0x1,(%esp)
8048227: e8 34 66 00 00 call 804e860 <__libc_write>

return 0;
804822c: b8 00 00 00 00 mov $0x0,%eax
}
8048231: c9 leave
8048232: c3 ret
8048233: 90 nop
.... 省略 ....
0804e860 <__libc_write>:
.... 省略 ....
804e86a: 53 push %ebx
804e86b: 8b 54 24 10 mov 0x10(%esp),%edx
804e86f: 8b 4c 24 0c mov 0xc(%esp),%ecx
804e873: 8b 5c 24 08 mov 0x8(%esp),%ebx
804e877: b8 04 00 00 00 mov $0x4,%eax
804e87c: cd 80 int $0x80
.... 省略 ....

所以...他的順序... main() -> write -> __libc_write
那之後的????
注意!!!有一個
int 0x80

這是一個Linux kernel的進入點(學名稱做system call)
所以到這邊先一個總結...(接下一篇)

No comments:

Post a Comment