常见汇编指令及范例解释

来源:互联网 发布:mixer sam 软件 编辑:程序博客网 时间:2024/04/30 01:15

有两种汇编格式:Intel汇编和AT&T汇编,前者常用于Windows,后者常用于Unix,是GNU汇编器的缺省格式,本文介绍后者(AT&T汇编格式)。

1,常用指令及解释

movl %eax, %edx  ---   数据传递,%eax地址传递给%edx

pushl %eax  --- 入栈,分两步:subl $4, %esp(栈向低地址方向增长) + movl %eax, (%esp)

popl %eax --- 出栈,分两步:movl (%esp), %eax + addl $4, %esp 

addl $8, %eax --- 

subl $4, %esp --- 

enter --- 进入函数,分两步:pushl %ebp + movl %esp, %ebp(将栈顶地址复制给栈基址,当前栈变为空)

leave --- 退出函数,分两步:movl %ebp, %esp + popl %ebp

call 0x12345 --- push %eip + movl 0x12345, %eip(%eip存放下一条执行指令)

ret --- call的逆操作,相当于popl %eip


2,传输字节数

movb         8bit

movw        16bit

movl          32bit

movq         64bit


3,程序实例

int g(int x){  return x + 10;}int f(int x){  return g(x);}int main(){  return f(20) + 8;}
执行汇编命令:

    gcc -S -o main.s main.c -m32
生成32位汇编代码main.s;
.file"main.c".text.globlg.typeg, @functiong:.LFB0:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5movl8(%ebp), %eaxaddl$10, %eaxpopl%ebp.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE0:.sizeg, .-g.globlf.typef, @functionf:.LFB1:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5subl$4, %espmovl8(%ebp), %eaxmovl%eax, (%esp)callgleave.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE1:.sizef, .-f.globlmain.typemain, @functionmain:.LFB2:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5subl$4, %espmovl$20, (%esp)callfaddl$8, %eaxleave.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE2:.sizemain, .-main.ident"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-11)".section.note.GNU-stack,"",@progbits
剔除无关精要的部分,核心汇编代码如下:

1  g:2    pushl %ebp3    movl %esp, %ebp4    movl 8(%ebp), %eax5    addl $10, %eax6    popl %ebp7    ret8  f:9    pushl %ebp10  movl %esp, %ebp11  subl $4, %esp12  movl 8(%ebp), %eax13  movl %eax, (%esp)14  call g15  leave16  ret17 main:18  pushl %ebp19  movl %esp, %ebp20  subl $4, %esp21  movl $20, (%esp)22  call f23  addl $8, %eax24  leave25  ret




0 0
原创粉丝点击