当前位置: 技术问答>linux和unix
问段汇编代码的意思?怎么和.c程序联系起来??在线
来源: 互联网 发布时间:2015-09-25
本文导语: test.c 代码: #include void hi(void) { printf("hi"); } int main(int argc, char *argv[]) { hi(); return 0; } //编译时没有任何优化,请教下面的汇编代码的理解,实在不知道具体是怎么生成的.. //我的环境gcc version...
test.c 代码:
#include
void hi(void)
{
printf("hi");
}
int main(int argc, char *argv[])
{
hi();
return 0;
}
//编译时没有任何优化,请教下面的汇编代码的理解,实在不知道具体是怎么生成的..
//我的环境gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)+GNU gdb Red Hat Linux (6.1post-1.20040607.41rh)
main函数的汇编代码:
0x08048380 : push %ebp
0x08048381 : mov %esp,%ebp
0x08048383 : sub $0x8,%esp
0x08048386 : and $0xfffffff0,%esp
0x08048389 : mov $0x0,%eax
0x0804838e : add $0xf,%eax
0x08048391 : add $0xf,%eax
0x08048394 : shr $0x4,%eax
0x08048397 : shl $0x4,%eax
0x0804839a : sub %eax,%esp
0x0804839c : call 0x8048368
0x080483a1 : mov $0x0,%eax
0x080483a6 : leave
0x080483a7 : ret
hi函数的汇编代码:
0x08048368 : push %ebp
0x08048369 : mov %esp,%ebp
0x0804836b : sub $0x8,%esp
0x0804836e : sub $0xc,%esp
0x08048371 : push $0x8048488
0x08048376 : call 0x80482b0
0x0804837b : add $0x10,%esp
0x0804837e : leave
0x0804837f : ret
#include
void hi(void)
{
printf("hi");
}
int main(int argc, char *argv[])
{
hi();
return 0;
}
//编译时没有任何优化,请教下面的汇编代码的理解,实在不知道具体是怎么生成的..
//我的环境gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)+GNU gdb Red Hat Linux (6.1post-1.20040607.41rh)
main函数的汇编代码:
0x08048380 : push %ebp
0x08048381 : mov %esp,%ebp
0x08048383 : sub $0x8,%esp
0x08048386 : and $0xfffffff0,%esp
0x08048389 : mov $0x0,%eax
0x0804838e : add $0xf,%eax
0x08048391 : add $0xf,%eax
0x08048394 : shr $0x4,%eax
0x08048397 : shl $0x4,%eax
0x0804839a : sub %eax,%esp
0x0804839c : call 0x8048368
0x080483a1 : mov $0x0,%eax
0x080483a6 : leave
0x080483a7 : ret
hi函数的汇编代码:
0x08048368 : push %ebp
0x08048369 : mov %esp,%ebp
0x0804836b : sub $0x8,%esp
0x0804836e : sub $0xc,%esp
0x08048371 : push $0x8048488
0x08048376 : call 0x80482b0
0x0804837b : add $0x10,%esp
0x0804837e : leave
0x0804837f : ret
|
我的机器
gcc -v
Reading specs from /usr/lib/gcc-lib/i586-suse-linux/3.3.3/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --enable-languages=c,c++,f77,objc,java,ada --disable-checking --libdir=/usr/lib --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit i586-suse-linux
Thread model: posix
gcc version 3.3.3 (SuSE Linux)
(gdb) disassemble main
Dump of assembler code for function main:
0x080483a4 : push %ebp
0x080483a5 : mov %esp,%ebp
0x080483a7 : sub $0x8,%esp
0x080483aa : and $0xfffffff0,%esp
0x080483ad : mov $0x0,%eax
0x080483b2 : sub %eax,%esp
0x080483b4 : call 0x804838c
0x080483b9 : mov $0x0,%eax
0x080483be : leave
0x080483bf : ret
End of assembler dump.
(gdb) disassemble hi
Dump of assembler code for function hi:
0x0804838c : push %ebp
0x0804838d : mov %esp,%ebp
0x0804838f : sub $0x8,%esp
0x08048392 : sub $0xc,%esp
0x08048395 : push $0x80484d8
0x0804839a : call 0x80482c0
0x0804839f : add $0x10,%esp
0x080483a2 : leave
0x080483a3 : ret
End of assembler dump.
gcc -v
Reading specs from /usr/lib/gcc-lib/i586-suse-linux/3.3.3/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --enable-languages=c,c++,f77,objc,java,ada --disable-checking --libdir=/usr/lib --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit i586-suse-linux
Thread model: posix
gcc version 3.3.3 (SuSE Linux)
(gdb) disassemble main
Dump of assembler code for function main:
0x080483a4 : push %ebp
0x080483a5 : mov %esp,%ebp
0x080483a7 : sub $0x8,%esp
0x080483aa : and $0xfffffff0,%esp
0x080483ad : mov $0x0,%eax
0x080483b2 : sub %eax,%esp
0x080483b4 : call 0x804838c
0x080483b9 : mov $0x0,%eax
0x080483be : leave
0x080483bf : ret
End of assembler dump.
(gdb) disassemble hi
Dump of assembler code for function hi:
0x0804838c : push %ebp
0x0804838d : mov %esp,%ebp
0x0804838f : sub $0x8,%esp
0x08048392 : sub $0xc,%esp
0x08048395 : push $0x80484d8
0x0804839a : call 0x80482c0
0x0804839f : add $0x10,%esp
0x080483a2 : leave
0x080483a3 : ret
End of assembler dump.
|
感觉与GCC的版本有关系.
.file "test.c"
.section .rodata
.LC0:
.string "hi"
.text
.globl hi
.type hi,@function
hi:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
subl $12, %esp
pushl $.LC0
call printf
addl $16, %esp
leave
ret
.Lfe1:
.size hi,.Lfe1-hi
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call hi
movl $0, %eax
leave
ret
.Lfe2:
.size main,.Lfe2-main
.ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
===================================================================
反汇编出来与yanghuajia(newperson) 一样,不列出来.
.file "test.c"
.section .rodata
.LC0:
.string "hi"
.text
.globl hi
.type hi,@function
hi:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
subl $12, %esp
pushl $.LC0
call printf
addl $16, %esp
leave
ret
.Lfe1:
.size hi,.Lfe1-hi
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
call hi
movl $0, %eax
leave
ret
.Lfe2:
.size main,.Lfe2-main
.ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
===================================================================
反汇编出来与yanghuajia(newperson) 一样,不列出来.
|
try to use some -O2 -O3 -Os options, normal options contains some garbage codes.