一.8086 16位CPU寄存器
16个寄存器:ax,bx,cx,dx,ah,al,bh,bl,ch,cl,dh,dl,sp,bp,si,di
通用寄存器:ax,bx,cx,dx
8位寄存器: ah,al,bh,bl,ch,cl,dh,dl,兼容8位cpu。
sp:堆栈指针寄存器,指向栈顶
bp:指针寄存器,等同于sp
si,di:变址寄存器,和bx,bp连用, [bx+si],
段寄存器:ds、ss、cs、es
ds数据段,ss堆栈段,cs指令段,es扩展段
约定: 1.函数返回值保存在ax中, 2.循环记数保存在cx中
二.32位CPU寄存器等同于8086CPU,前面加E
通用寄存器:eax,ebx,ecx,edx
堆栈指针寄存器: esp, ebp
变址寄存器:esi,edi
段寄存器:eds、ess、ecs、ees
段寄存器:eds、ess、ecs、ees
eds数据段,ess堆栈段,ecs指令段,ees扩展段
约定: 1.函数返回值保存在eax中, 2.循环记数保存在ecx中
程序0:将"aaa", "bbb", "ccc"转化成大写
1.C语言实现代码:
char st1[3][4]={"aaa", "bbb", "ccc"};
void CtwoDlg::OnBnClickedButton2()
{
for (int i=0; i<3; i++)
{
int cc =0;
for (int j=0;j <3; j++)
{
char s = st1[i][j] & 0x5f;
st1[i][j] =s;
}
}
}
反汇编:
238:char st1[3][4]={"aaa", "bbb", "ccc"};
239: void CtwoDlg::OnBnClickedButton2()
240: {
011D2920 55 push ebp //保存ebp内容以便调用完后恢复
011D2921 8B EC mov ebp,esp//设置EBP指针指向栈顶
011D2923 83 EC 10 sub esp,10h //sp-10h
011D2926 89 4D F0 mov dword ptr [ebp-10h],ecx
242: for (int i=0; i<3; i++)
011D2929 C7 45 FC 00 00 00 00 mov dword ptr [i],0
011D2930 EB 09 jmp CtwoDlg::OnBnClickedButton2+1Bh (11D293Bh)//向下跳转 4行 cmp dword ptr [i],3
011D2932 8B 45 FC mov eax,dword ptr [i]//保存到寄存器eax
011D2935 83 C0 01 add eax,1 //寄存器加1
011D2938 89 45 FC mov dword ptr [i],eax//在放入内存单元
011D293B 83 7D FC 03 cmp dword ptr [i],3 //dword ptr表明指向一个32b的内存单元,和3比大小
011D293F 7D 40 jge CtwoDlg::OnBnClickedButton2+61h (11D2981h)//大于3就跳出循环
243: {
244: for (int j=0;j <3; j++)
011D2941 C7 45 F8 00 00 00 00 mov dword ptr [j],0
011D2948 EB 09 jmp CtwoDlg::OnBnClickedButton2+33h (11D2953h)
011D294A 8B 4D F8 mov ecx,dword ptr [j]
011D294D 83 C1 01 add ecx,1
011D2950 89 4D F8 mov dword ptr [j],ecx
011D2953 83 7D F8 03 cmp dword ptr [j],3
011D2957 7D 26 jge CtwoDlg::OnBnClickedButton2+5Fh (11D297Fh)
245: {
246: char s = st1[i][j] & 0x5f;
011D2959 8B 55 FC mov edx,dword ptr [i]
011D295C 8B 45 F8 mov eax,dword ptr [j]
011D295F 0F BE 8C 90 50 56 33 01 movsx ecx,byte ptr st1 (1335650h)[eax+edx*4]
011D2967 83 E1 5F and ecx,5Fh
011D296A 88 4D F7 mov byte ptr [s],cl
247: st1[i][j] =s;
011D296D 8B 55 FC mov edx,dword ptr [i]
011D2970 8B 45 F8 mov eax,dword ptr [j]
011D2973 8A 4D F7 mov cl,byte ptr [s]
011D2976 88 8C 90 50 56 33 01 mov byte ptr st1 (1335650h)[eax+edx*4],cl
248: }
011D297D EB CB jmp CtwoDlg::OnBnClickedButton2+2Ah (11D294Ah)
249: }
011D297F EB B1 jmp CtwoDlg::OnBnClickedButton2+12h (11D2932h)//向上跳转到mov eax,dword ptr [i]
250: }
011D2981 8B E5 mov esp,ebp
011D2983 5D pop ebp//恢复 ebp内容
011D2984 C3 ret
2.汇编实现代码:
assume cs:code, ds:data, ss:stack
data segment
db 'pppdisplay......'
db 'pppbrows........'
db 'pppreplace......'
db 'pppmodify.......'
data ends
stack segment
dw 0,0,0,0,0,0,0,0
stack ends
code segment
start:
mov ax,data
mov ds,ax
mov bx,0
mov cx,4
mov ax,stack
mov ss, ax
mov sp,16
s: push cx
mov cx,4
mov si,0
s1:mov al,[bx+si+3]
and al,1011111b
mov [bx+si+3],al
inc si
loop s1
add bx,16
pop cx
loop s
mov ax,4c00h
int 21h
code ends
end start
也可以这样写
code segment
start:
mov ax,data
mov ds,ax
mov bx,0
mov cx,4
mov ax,stack
mov ss, ax
mov sp,16
s: push cx
mov cx,4
mov si,0
s1:mov al,byte ptr[bx+si+3]
and al,1011111b
mov byte ptr [bx+si+3],al
inc si
loop s1
add bx,16
pop cx
loop s
程序1:用si,di实现将将字符串复制到它后面的缓冲区
assume cs:code, ds:data
data segment
db 'welcome to shiyan'
db '..................'
data ends
code segment
start:
mov ax,data
mov ds,ax
mov cx,17
mov di,0
mov si,17
s:mov al,[di]
mov [si],al
add di,1
add si,1
loop s
mov ax,4c00h
int 21h
code ends
end start
更简单的代码
assume cs:code, ds:data
data segment
db 'welcome to shiyan...................................'
data ends
code segment
start:
mov ax,data
mov ds,ax
mov cx,17
mov di,0
s:mov al,byte ptr[di]
mov byte ptr[di+17],al
add di,1
loop s
mov ax,4c00h
int 21h
code ends
end start
等价的写法
assume cs:code, ds:data
data segment
db 'welcome to shiyan...................................'
data ends
code segment
start:
mov ax,data
mov ds,ax
mov cx,17
mov dx,0
mov di,0
mov si,0
s:mov al,byte ptr[bx][di]
mov byte ptr[bx][si].17,al
add di,1
add si,1
loop s
mov ax,4c00h
int 21h
code ends
end start