Assembly Language 5

Source: Internet
Author: User
Tags microsoft dynamics

I. 8086 16-bit CPU registers

16 Registers: Ax,bx,cx,dx,ah,al,bh,bl,ch,cl,dh,dl,sp,bp,si,di
4 General-Purpose registers: AX,BX,CX,DX
8-bit General purpose register: AH,AL,BH,BL,CH,CL,DH,DL, high 8-bit and 8th-bit for general purpose registers.

SP: stack pointer register, pointing to the top of the stack
BP: pointer register, equivalent to SP
SI,DI: Variable address register, with BX,BP, [Bx+si],


Segment Registers: DS, SS, CS, ES
DS data segment, SS stack segment, CS instruction segment, ES extension segment

Conventions: 1. Function return values are saved in Microsoft Dynamics AX, 2. Loop count saved in CX


Two. 32-bit CPU register equals 8086CPU, front plus E
General Register: Eax,ebx,ecx,edx
Stack pointer register: ESP, EBP
Variable address register: Esi,edi
Segment Register: EDS, ESS, ECS, EES

Segment Register: EDS, ESS, ECS, EES
EDS data segment, ESS stack segment, ECS instruction segment, EES extension segment

Convention: 1. Function return value is saved in eax, 2. Loop count is saved in ECX

Program 0: Convert "AAA", "BBB", "CCC" to uppercase

1.C Language Implementation code:
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;
}
}
}
Disassembly :
238:char st1[3][4]={"AAA", "BBB", "CCC"};
239:void Ctwodlg::onbnclickedbutton2 ()
240: {
011d2920. Ebp//Save the EBP content for recovery after the call is finished
011d2921 8B EC mov ebp,esp//Set EBP pointer to top of stack
011d2923-EC Ten sub esp,10h//sp-10h
011d2926 4D F0 mov dword ptr [EBP-10H],ECX

242:for (int i=0; i<3; i++)
011d2929 C7 FC xx/xx mov dword ptr [i],0
011d2930 EB, jmp ctwodlg::onbnclickedbutton2+1bh (11D293BH)//Jump down 4 lines cmp dword ptr [i],3
011d2932 8B FC mov eax,dword ptr [i]//Save to register EAX
011d2935 C0 Add eax,1//Register plus 1
011d2938-FC mov dword ptr [I],eax//Put in memory unit
011d293b 7D FC, CMP dword ptr [i],3//dword PTR indicates a memory unit pointing to a 32b, and a size of 3
011d293f 7D jge ctwodlg::onbnclickedbutton2+61h (11d2981h)//greater than 3 to jump out of the loop
243: {
244:for (int j=0;j <3; j + +)
011d2941 C7 F8/xx/mov dword ptr [j],0
011d2948 EB, jmp ctwodlg::onbnclickedbutton2+33h (11D2953H)
011d294a 8B 4D F8 mov ecx,dword ptr [j]
011d294d C1 Add ecx,1
011d2950 4D F8 mov dword ptr [J],ECX
011d2953 7D F8 cmp dword ptr [j],3
011d2957 7D jge ctwodlg::onbnclickedbutton2+5fh (11D297FH)
245: {
246:char s = st1[i][j] & 0x5f;
011d2959 8B FC mov edx,dword ptr [i]
011d295c 8B F8 mov eax,dword ptr [j]
011d295f 0F be 8C movsx ecx,byte ptr st1 (1335650h) [eax+edx*4]
011d2967 E1 5F and ECX,5FH
011d296a 4D F7 mov byte ptr [s],cl
247:ST1[I][J] =s;
011d296d 8B FC mov edx,dword ptr [i]
011d2970 8B F8 mov eax,dword ptr [j]
011d2973 8A 4D F7 mov cl,byte ptr [s]
011d2976 8C (1335650h) [EAX+EDX*4],CL], ST1 mov byte ptr
248:}
011d297d EB CB jmp ctwodlg::onbnclickedbutton2+2ah (11D294AH)
249:}
011d297f EB B1 jmp ctwodlg::onbnclickedbutton2+12h (11d2932h)//Jump up to mov eax,dword ptr [i]
250:}
011d2981 8B E5 mov esp,ebp
011d2983 5D Pop Ebp//Recover EBP content
011d2984 C3 RET

2. Assemble the implementation code:

Assume Cs:code, Ds:data, Ss:stack
Data segment
DB ' AAAA '
DB ' bbbb '
DB ' CCCC '
DB ' dddd '
Data ends


stack segment ; Defines a segment that is used as a stack segment and has a capacity of 4 bytes
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 ; stack the CX value of the outer loop
mov cx,4 ; CX set as the number of inner loops
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 ; eject the original CX value from the top of the stack and restore the outer loop CX
loop s ; loop instruction with outer loops minus 1 of the count value in CX

MOV ax,4c00h
int 21h
Code ends
End Start

Program 1: Use the Si,di implementation to copy the string to the buffer behind it
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

Simpler code
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

An equivalent notation
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

Assembly Language 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.