Compilation of getting started Study Notes (6) -- si, di, Dual Loop, compilation of si

Source: Internet
Author: User

Compilation of getting started Study Notes (6) -- si, di, Dual Loop, compilation of si

Getting started with crazy summer vacation Study Notes (6) -- si, di, dual cycle


Reference: Chapter 7th of Assembly Language


1. and or commands, and [bx + idata]


And or.

[Bx + idata] This can be written. In some cases, it is more convenient.

[Bx + idata] can also be written as idata [bx]


For example, replace 'abcde' and 'fghig' with uppercase letters (uppercase and lowercase letters in ASCII are only the fifth digits, and uppercase letters are 0, lowercase letter is 1)


Assume cs: code, ds: data segmentdb 'abcde' db 'fghig' data ends code segmentstart: mov ax, datamov ds, axmov bx, 0mov cx, 4mov al, 00100000bs: or [bx], alor [5 + bx], al; [5 + bx] can also be written as 5 [bx] inc bxloop smov ax, 4c00Hint 21 Hcode endsend start

2. si, di, and [bx + si], [bx + di], [bx + si + idata], [bx + di + idata]


Si and di, except that they cannot be divided into bh and bl like bx, and others are the same as bx. For example, mov ax, [si], etc.

[Bx + si], [bx + di], [bx + si + idata], [bx + idata]

These are all possible. In some cases, they are more convenient.


[Bx + si] and [bx + di] can be written as [bx] [si], [bx] [di]

[Bx + si + idata], [bx + di + idata] can be written as idata [bx] [si], idata [bx] [di]


Example: Copy Welcome!

assume cs:code,ds:datadata segmentdb 'Welcome!        'data endscode segmentstart:mov ax,datamov ds,axmov si,0mov cx,4s:mov ax,[si]mov 8[si],axadd si,2loop smov ax,4c00Hint 21Hcode endsend start


Example: change the first letter of a word in data to uppercase.

assume cs:code,ds:datadata segmentdb '1.file  'db '2.edit  'db '3.search'db '4.view  'data endscode segmentstart:mov ax,datamov ds,axmov bx,0mov cx,4mov al,11011111bs:and [bx+2],aladd bx,8loop smov ax,4c00Hint 21Hcode endsend start




3. Double Loop


Example: replace words in data with uppercase letters

assume cs:code,ds:datadata segmentdb 'ibm     'db 'dec     'db 'dos     'db 'vax     'data endscode segmentstart:mov ax,datamov ds,axmov bx,0mov al,11011111bmov cx,4s:mov cx,3mov si,0s0:and [bx+si],alinc siloop s0add bx,8loop smov ax,4c00Hint 21Hcode endsend start

The above code is wrong and will show an endless loop. Because cx is constantly assigned 3, it leads to an endless loop of the outer layer.


Improvement: The cx is saved during the inner loop, and the cx is restored when the memory loop ends. Example:


Assume cs: code, ds: datadata segmentdb 'ibm 'db' dec 'db' dos 'db' vax 'data endscode segmentstart: mov ax, datamov ds, axmov bx, 0mov al, 11011111 bmov cx, 4 s: mov dx, cx; dx is used to temporarily store the outer cx values mov cx, 3mov si, 0s0: and [bx + si], alinc siloop s0mov cx, dx; used to restore the cxadd bx, 8 loop smov ax, 4c00Hint 21 Hcode endsend start of the outer loop

The above code can solve the problem and run properly. However, the number of registers is limited. Sometimes, there may be no other registers available.

Solution: Save it in memory. Example:

Assume cs: code, ds: datadata segmentdb 'ibm 'db' dec 'db'dos 'db' vax 'DW 0; define a word to save cxdata endscode segmentstart: mov ax, datamov ds, axmov bx, 0mov al, 11011111 bmov cx, 4 s: mov ds: [20 H], cx mov cx, 3mov si, 0s0: and [bx + si], alinc siloop s0mov cx, ds: [20 H] add bx, 8 loop smov ax, 4c00Hint 21 Hcode endsend start

The above code solves the problem of insufficient registers. However, it is still complicated. If there are many loops, it will be unclear.

Solution: Use the stack to save and restore cx. Example 1:

assume cs:code,ds:data,ss:stackdata segmentdb 'ibm     'db 'dec     'db 'dos     'db 'vax     'data endsstack segmentdw 0,0,0,0,0,0,0,0stack endscode segmentstart:mov ax,datamov ds,axmov ax,stackmov ss,axmov sp,16mov bx,0mov al,11011111bmov cx,4s:push cxmov cx,3mov si,0s0:and [bx+si],alinc siloop s0pop cxadd bx,8loop smov ax,4c00Hint 21Hcode endsend start


Example 2: replace the words in data with uppercase letters

assume cs:code,ds:data,ss:stackdata segmentdb '1.display.......'db '2.brows.........'db '3.replace.......'db '4.modify........'data endsstack segmentdw 0,0,0,0,0,0,0,0stack endscode segmentstart:mov ax,datamov ds,axmov ax,stackmov ss,axmov sp,16mov bx,0mov al,11011111bmov cx,4s:push cxmov si,0mov cx,3s0:and [bx+si+2],alinc siloop s0pop cxadd bx,10Hloop smov ax,4c00Hint 21Hcode endsend start





Mov si, DI, right?
Save the SI value before changing the SI value.
Compilation experts help !!! Thank you !! Debugging successful
Note the following three things: (1) Define and use types to match words and save energy; (2) initialize the internal and external cycles in a dual loop; (3) Output The hexadecimal number of the program
DATA SEGMENT
Cj db 30 DUP (?)
Buf db 3
DB?
DB 3 DUP (?)
DATA ENDS
CODE SEGMENT
Assume cs: CODE, DS: DATA, SS: STACK
MAIN PROC FAR
START: PUSH DS
Xor ax, AX
PUSH AX
Mov ax, DATA
Mov ds, AX

Mov di, OFFSET CJ

Mov cx, 10
B: MOV DX, OFFSET BUF
Mov ah, 0AH
INT 21 H

Mov si, OFFSET BUF
Mov bl, 2
Mov ax, 0
A: mov dl, 10
MUL DL
Mov dl, [SI + 2]
And dl, 0FH
Add al, DL
INC SI
DEC BL
JNZ
MOV [DI], AL
CALL HCHH
INC DI
LOOP B

Mov bl, 2
Mov si, OFFSET CJ
Mov cl, 4
Mov ch, [SI]
C: rol ch, CL
Mov dl, CH
And dl, 0FH
Add dl, 30 H
Cmp dl, 3AH
JB DISP
Add dl, 7 H
DISP:
Mov ah, 2
INT 21 H
DEC BL
JNZ C
Mov ah, 1
INT 21 H
EXIT: RET
MAIN ENDP

HCHH PROC
Mov dl, 0DH
Mov ah, 2
INT 21 H
Mov dl, 0AH
Mov ah, 2
INT 21 H
RET
HCHH ENDP

CODE ENDS
END MAIN

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.