(zz from http://blog.luoyuanhang.com/)
##常見寄存器
寄存器 | 16位 | 32位 | 64位 |
---|---|---|---|
累加寄存器 accumulator |
AX | EAX | RAX |
基址寄存器 base |
BX | EBX | RBX |
計數寄存器 count |
CX | ECX | RCX |
數據寄存器 data |
DX | EDX | RDX |
堆棧基指針 Base Pointer |
BP | EBP | RBP |
變址寄存器 Source Index |
SI | ESI | RSI |
堆棧頂指針 Stack Pointer |
SP | ESP | RSP |
指令寄存器 Instruction Pointer |
IP | EIP | RIP |
AH&AL=AX(accumulator):累加寄存器 BH&BL=BX(base):基址寄存器 CH&CL=CX(count):計數寄存器 DH&DL=DX(data):數據寄存器 SP(Stack Pointer):堆棧指針寄存器 BP(Base Pointer):基址指針寄存器 SI(Source Index):源變址寄存器 DI(Destination Index):目的變址寄存器 IP(Instruction Pointer):指令指針寄存器 CS(Code Segment)代碼段寄存器 DS(Data Segment):數據段寄存器 SS(Stack Segment):堆棧段寄存器 ES(Extra Segment):附加段寄存器
##匯編指令
##mov
-
movb(8位)、movw(16位)、movl(32位)、movq(64位)
-
寄存器尋址:
movl %eax, %edx
eax -> edx -
立即數尋址:
movl $0x123, %edx
數字->寄存器 -
直接尋址:
movl 0x123, %edx
直接訪問內存地址數據,edx = *(int32_t *)0x123; -
間接尋址:
movl (%ebx), %edx
%ebx 是個內存地址,(%ebx)指的是該地址中的數據,edx = *(int32_t*)ebx; -
變址尋址:
movl 4(%ebx), %edx
edx = *(int32_t*)(ebx+4);
##push & pull
###堆棧數據結構簡介
####作用:
- 程序調用框架
- 傳遞參數
- 保存返回地址
- 提供局部變量
- ……
####結構:
-
相關寄存器: esp, ebp
-
相關操作: pop, push
//建立被調用者函數的堆棧框架 pushl %ebp movl %esp, %ebp //拆除框架 movl %ebp, %esp popl %ebp ret
###push:壓棧
-
push %eax
相當於:subl $4, %esp //棧頂指針減4 movl %eax, (%esp) //%eax -> esp 地址
###pop:出棧
-
pop %eax
相當於:movl (%esp), %eax addl %4, %esp //棧頂指針加4
##call&ret
###call
-
call 0x12345
相當於:pushl %eip movl $0x12345, %eip //當前地址壓棧,存入新地址
###ret
-
相當於:
popl %eip //棧 -> eip
##enter&leave
###enter
push %ebp
movl %esp, %ebp
//將堆棧置空(棧上重堆)
###leave
movl %ebp, %esp
popl %ebp
//將堆棧置空(撤銷堆棧)
##例子:分析一段匯編代碼
pushl $8 ①
movl %ebp, %esp ②
subl $4, %esp ③
movl $8, (%esp) ④