程序的機器級表示
IA32和X86-64(AMD)
32位和64位
ISA
instruction system architecture
重要寄存器
- 程序計數器PC %eip
- 整數寄存器 8個每個32位
有的用來記錄狀態,有的用來保存臨時數據,局部變量,返回值
3.條件碼寄存器
字 16位,雙字double word, 4字
- 指針都是雙字
- char short long 其他4字節
- movb movw movl
32位的寄存器
%al %ah -> %ax -> %eax
%eax %ebx %ecx %edx %edi %esi %esp %ebp
存取數
Imm(Ea,Eb,s) -> M[Imm + R[Ea] + R[Eb] * s]
只有完整的寄存器可以()
只有完整的寄存器可以()
習題
3.1
操作數 | 值 |
---|---|
%eax | 0x100 |
0x104 | 0x104 -> 0xAB |
$0x108 | 0x108 |
(%eax) | 0xFF |
4(%eax) | 0xAB |
9(%eax,%edx) | 0x11 |
260(%ecx,%edx) | ->0x13 |
0xFC(,%ecx,4) | 0xFF |
(%eax,%edx,4) | 0x11 |
mov操作
分爲mov,movs(sign),movz(zero)
然後IA32規定不能直接存儲器到存儲器,中間必須經過一個寄存器。
算術與邏輯操作
1.加載有效地址 2.一元操作和二元操作 3.移位操作
1. load effective address = lea 就是取地址 第二個位置必須是寄存器
2. Inc x x可以是寄存器也可以是存儲器
3. sub s,d d <- d-s d是寄存器或者存儲器
特殊的操作
imull S R[%edx]:R[%eax] <- S * R[%eax] 有符號的
divl S R[%edx] <- R[%edx]:R[%eax] MOD S
R[%eax] <- R[%edx]:R[%eax] / S
3.2
movl | %eax, (%esp) |
movw | (%eax),%dx |
movb | $0xFF, %bl |
movb | (%esp,%edx,4),%dh |
pushb->pushl |
$0xFF 棧只能4個字節4個字節存放 |
movw | %dx, (%eax) |
popl | %edi |
3.3
movb $0xF, (%bl) | 不完整不能寄存器 |
movl %ax,(%esp) | l是4, %ax是2 |
movw (%eax),4(%esp) | 不能都是內存地址 |
movb %ah,%sh | 沒有%sh |
movl %eax, $0x123 | 不可以吧 |
movl %eax,%dx | dx是2,l是4 |
movb %si,8(%ebp) | b是1,%si是4 |
3.4
src_t | dest_t | 指令 |
---|---|---|
int | int | movl %eax, (%edx) |
char | int | movzbl %eax, (%edx) -> ? |
char | unsigned | movzbl %eax, (%edx) -> ? |
unsigned char | int | movzbw %eax, (%edx)->movzbl %eax, (%edx) |
int | char | movb %al,(%edx) |
unsigned | unsigned char | movb %al, (%edx) |
unsigned | int | movl %eax, (%edx) |
3.5
void f(int *xp,int *yp,int *zp) {
int tx = *xp;
int ty = *yp;
int tz = *zp;
*yp = tx;
*zp = ty;
*xp = tz;
}
3.6
指令 | 效果 |
---|---|
leal 6(%eax),%edx | 6 + x |
leal (%eax,%ecx),%edx | x + y |
leal (%eax,%ecx,4),%edx | x + 4 * y |
leal 7(%eax,%eax,8),%edx | 9 * x + 7 |
leal 0xA(,%ecx,4),%edx | 4 * y + 0xA |
leal 9(%eax,%ecx,2),%edx | x + 2 * y + 9 |
3.7
指令 | 目的 | 值 |
---|---|---|
addl %ecx,(%eax) | 0x100 | 0x100 |
subl %edx,4(%eax) | 0x104 | 0xA8 |
imull $16,(%eax,%edx,4) | 0x10C | 0x101->0x110 |
incl 8(%eax) | 0x108 | 0x14 |
decl %ecx | %ecx | 0x0 |
subl %edx,%eax | %eax | 0xFD |
3.8
- shll
2, eax - sarl
%cl
,%eax
3.9
int t1 = y ^ x;
int t2 = (y ^ x) * 8;
int t3 = ~t3;
int t4 = ~(y^x * 8) - z
3.10
- 清零
- movl $0,%edx
3.11
movl 8(%ebp) %eax
movl 0 %edx
divl 12(%ebp)
movl %eax 4(%esp)
movl %edx (%esp)