码迷,mamicode.com
首页 > 编程语言 > 详细

汇编语言5

时间:2015-05-27 15:42:25      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

一.8086 16位CPU寄存器

 16个寄存器:ax,bx,cx,dx,ah,al,bh,bl,ch,cl,dh,dl,sp,bp,si,di
 4个通用寄存器:ax,bx,cx,dx
 8位通用寄存器: ah,al,bh,bl,ch,cl,dh,dl,通用寄存器的高8位和第8位。

 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 ‘aaaa      ‘
db ‘bbbb      ‘
db ‘cccc      ‘
db ‘dddd      ‘
data ends


stack segment      ;定义一个段,用来作栈段,容量为4个字节
dw 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,4

s: push cx    ;将外层循环的cx值压栈
mov cx,4   ;cx设置为内层循环的次数
mov si,0 

s1:mov al,ds:[bx+si]
and al,1011111b
mov ds:[bx+si],al
inc si
loop s1
add bx,10
pop cx     ;从栈顶弹出原cx的值,恢复外层循环cx
loop s    ;外层循环的loop指令将cx中的计数值减 1

mov ax,4c00h
int 21h
code ends
end start

 

程序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,ds:[di]
mov ds:[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,ds:[di]
mov ds:[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,ds:[bx][di]
  mov ds:[bx][si].17,al
  add di,1
  add si,1
  loop s 

mov ax,4c00h
int 21h
code ends
end start

汇编语言5

标签:

原文地址:http://www.cnblogs.com/mayingkun/p/4533488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!