- Registers
- General-purpose registers
- EAX EBX ECX EDX - 32 bit
- AX BX CX DX - 16 bit
- AH BH CH DH - 8 bit (high)
- AL BLCLDL - 8bit (low)
- Buffer pointer (缓冲区指针)
- EDI ESI - 32 bit
- DI SI - 16 bit
- Stack Pointer
- ESP - 32 bit
- 指向 stack 顶端
- Base Pointer
- EBP - 32 bit
- 指向 stack 底端
- ESP 到 function 参数范围称为该 function 得到 Stack Frame
- Program counter register
- EIP
- 只想目前程式执行的位置
- Flag register
- eflags
- 储存指令执行的结果
- Segment register
- cs ss ds es fs gs
- General-purpose registers
汇编指令
- AT & T
- mov %eax,%ebx
- Intel
- mov ebx,eax
- Basic instruction (基本命令)
- mov
- mov imm/reg/mem value to reg/mem
- mov A,B (move B to A)
- A 与 B 的 size 要相等
- ex:
- mov eax,ebx (√)
- mov eax,bl (×)
- mov eax,0xdeadbeef
- add/sub
- and/or/xor
- add/sub/or/xor/and reg,imm/reg
- add/sub/or/xor/and A,B
- A 与 B 的 size 一样要相等
- ex:
- add ebp,0x48
- sub eax,ebx
- push/pop
- push/pop reg
- ex:
- push eax 等价于 sub esp , 4 ; mov [esp] ,eax
- pop ebx 等价于 mov ebx , [esp] ; add esp ,4
- lea
- ex:
- lea eax , [esp+4]
- mov
- lea v.s. mov
按我的理解解释,lea取地址。mov取值
- jmp/call/ret
- jmp 跳至程式码的某处去执行
- call eax 等价于 push 下一条指令的ip ; jmp eax
- ret 等价于 pop eip
- leave
- mov esp ,ebp
- pop ebp
- nop
- 一个 byte 不做任何事
- opcode = 0x90
- System call
- instruction : int 0x80
- eax : system call number / return value
- ebx,ecx,edx,esi,edi : argument
- System call table
- https://syscalls.w3challs.com/?arch=x86
- Calling convention (调用约定)
- function call
- call : push return address to stack then jump
- function return
- ret : pop return address
- function argument
- x86 下一般来说用 stack 传递 , 但也有用 register 传递 没有强制规定
- 依序 push 进 stack 后 ,再去执行 function call
- function prologue (我的理解就是开栈)
- compiler(编译器) 在 function 开头加的指令 , 主要在保存 ebp 分配区域变数所需空间
- function epilogue
- compiler 在 function 结尾加的指令 , 主要在利用保存的 ebp 回覆 call function 时的 stack 状态
- function call