Compilation of learning notes (6) -- Si and Di, Dual Loop

Source: Internet
Author: User

Getting started with crazy summer vacation Study Notes (6) -- Si, Di, dual cycle


Reference: Chapter 7th of Assembly Language


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


And 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 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




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.