Notes on learning how to assemble an introductory chapter (VI) - si, di, double loop

Publisher:RainbowPromiseLatest update time:2016-06-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Crazy summer study: Introduction to assembly learning notes (VI) - si, di, double loop

 

Reference: Assembly Language, Chapter 7 by Wang Shuang

 

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

 

I won’t say much about and and or.

[bx+idata] This is acceptable and in some cases, it is more convenient.

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

 

See the example directly: Change 'ABcde' and 'fGHig' to uppercase (in ASCII, uppercase letters and lowercase letters in binary only differ in the fifth bit, uppercase letters are 0, lowercase letters are 1)

 

  1. assume cs:code,ds:data   
  2.   
  3. data segment  
  4.     db 'ABcde'  
  5.     db 'fGHIg'  
  6. data ends   
  7.   
  8. code segment  
  9.   
  10. start: mov ax,data  
  11.     mov ds,ax  
  12.           
  13.     mov bx,0  
  14.     mov cx,4  
  15.     mov al,00100000b  
  16. s: or [bx],al  
  17.     or [5+bx],al ;[5+bx] can also be written as 5[bx]  
  18.     inc bx  
  19.     loops  
  20.           
  21.           
  22.     mov ax,4c00H  
  23.     int 21H  
  24.   
  25. code ends  
  26.   
  27. end start  

 

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

 

 

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

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

These are all possible and, in some cases, convenient.

 

Where [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!

 

  1. assume cs:code,ds:data  
  2.   
  3. data segment  
  4.     db 'Welcome! '  
  5. data ends  
  6.   
  7. code segment  
  8.     start: mov ax,data  
  9.         mov ds,ax  
  10.       
  11.         mov si,0  
  12.         mov cx,4  
  13.     s: mov ax,[si]  
  14.         mov 8[si],ax  
  15.         add si,2  
  16.         loops  
  17.               
  18.         mov ax,4c00H  
  19.         int 21H  
  20. code ends  
  21.   
  22. end start  

 

 

Example: Capitalize the first letter of the word in data

 

  1. assume cs:code,ds:data  
  2.   
  3. data segment  
  4.     db '1.file '  
  5.     db '2.edit '  
  6.     db '3.search'  
  7.     db '4.view '  
  8. data ends  
  9.   
  10. code segment  
  11.     start: mov ax,data  
  12.         mov ds,ax  
  13.               
  14.         mov bx,0  
  15.         mov cx,4  
  16.         mov al,11011111b  
  17.     s: and [bx+2],al  
  18.         add bx,8  
  19.         loops  
  20.               
  21.         mov ax,4c00H  
  22.         int 21H  
  23.               
  24.               
  25. code ends  
  26.   
  27. end start  



 

 

3. Double loop

 

Example: Change the words in data to uppercase

 

  1. assume cs:code,ds:data  
  2.   
  3. data segment  
  4.     db 'ibm '  
  5.     db 'dec '  
  6.     db 'dos ​​'  
  7.     db 'vax'  
  8. data ends  
  9.   
  10. code segment  
  11.     start: mov ax,data  
  12.         mov ds,ax  
  13.               
  14.         mov bx,0  
  15.         mov al,11011111b  
  16.         mov cx,4  
  17.     s: mov cx,3  
  18.         mov si,0  
  19.     s0: and [bx+si],al  
  20.         inc si  
  21.         loop s0  
  22.           
  23.         add bx,8  
  24.         loops  
  25.               
  26.         mov ax,4c00H  
  27.         int 21H  
  28.               
  29. code ends  
  30.   
  31. end start  

 

The above code is wrong and will result in an infinite loop, because cx is constantly assigned 3, causing the outer loop to loop infinitely.

  1. assume cs:code,ds:data  
  2.   
  3. data segment  
  4.     db 'ibm '  
  5.     db 'dec '  
  6.     db 'dos ​​'  
  7.     db 'vax'  
  8. data ends  
  9.   
  10. code segment  
  11.     start: mov ax,data  
  12.         mov ds,ax  
  13.               
  14.         mov bx,0  
  15.         mov al,11011111b  
  16.         mov cx,4  
  17.     s: mov dx,cx; dx is used to temporarily store the value of cx in the outer layer  
  18.         mov cx,3  
  19.         mov si,0  
  20.     s0: and [bx+si],al  
  21.         inc si  
  22.         loop s0  
  23.         mov cx,dx ; used to restore cx of the outer loop  
  24.         add bx,8  
  25.         loops  
  26.               
  27.         mov ax,4c00H  
  28.         int 21H  
  29.               
  30. code ends  
  31.   
  32. end start  

 

The above code can solve the above problem and run normally, but the number of registers is limited, and sometimes there may be no other registers available.

Solution, save in memory. Example:

 

  1. assume cs:code,ds:data  
  2.   
  3. data segment  
  4.     db 'ibm '  
  5.     db 'dec '  
  6.     db 'dos ​​'  
  7.     db 'vax'  
  8.     dw 0 ; define a word to save cx  
  9. data ends  
  10.   
  11. code segment  
  12.     start: mov ax,data  
  13.         mov ds,ax  
  14.               
  15.         mov bx,0  
  16.         mov al,11011111b  
  17.         mov cx,4  
  18.     s: mov ds:[20H],cx    
  19.         mov cx,3  
  20.         mov si,0  
  21.     s0: and [bx+si],al  
  22.         inc si  
  23.         loop s0  
  24.         mov cx,ds:[20H]   
  25.         add bx,8  
  26.         loops  
  27.               
  28.         mov ax,4c00H  
  29.         int 21H  
  30.               
  31. code ends  
  32.   
  33. end start  

 

The above code solves the problem of insufficient registers. However, it is still quite complicated and will be difficult to understand if there are many loops.

The solution is to use the stack to save and restore cx. Example 1:

 

  1. assume cs:code,ds:data,ss:stack  
  2.   
  3. data segment  
  4.     db 'ibm '  
  5.     db 'dec '  
  6.     db 'dos ​​'  
  7.     db 'vax'  
  8. data ends  
  9.   
  10. stack segment  
  11.     dw 0,0,0,0,0,0,0,0  
  12. stack ends  
  13.   
  14. code segment  
  15.     start: mov ax,data  
  16.         mov ds,ax  
  17.         mov ax,stack  
  18.         mov ss,ax  
  19.         mov sp,16  
  20.               
  21.         mov bx,0  
  22.         mov al,11011111b  
  23.         mov cx,4  
  24.     s: push cx  
  25.         mov cx,3  
  26.         mov si,0  
  27.     s0: and [bx+si],al  
  28.         inc si  
  29.         loop s0  
  30.               
  31.         pop cx  
  32.         add bx,8  
  33.         loops  
  34.               
  35.         mov ax,4c00H  
  36.         int 21H  
  37.               
  38. code ends  
  39.   
  40. end start  

 

Example 2: Capitalize the first three letters of the words in data

  1. assume cs:code,ds:data,ss:stack  
  2.   
  3. data segment  
  4.     db '1.display.......'  
  5.     db '2.brows.........'  
  6.     db '3.replace.......'  
  7.     db '4.modify........'  
  8. data ends  
  9.   
  10. stack segment  
  11.     dw 0,0,0,0,0,0,0,0  
  12. stack ends  
  13.   
  14. code segment  
  15.     start: mov ax,data  
  16.         mov ds,ax  
  17.         mov ax,stack  
  18.         mov ss,ax  
  19.         mov sp,16  
  20.                   
  21.         mov bx,0  
  22.         mov al,11011111b  
  23.         mov cx,4  
  24.     s: push cx  
  25.         mov si,0  
  26.         mov cx,3  
  27.     s0: and [bx+si+2],al  
  28.         inc si  
  29.         loop s0  
  30.           
  31.         pop cx  
  32.         add bx,10H  
  33.         loops  
  34.               
  35.         mov ax,4c00H  
  36.         int 21H  
  37.           
  38. code ends  
  39.   
  40. end start  

Reference address:Notes on learning how to assemble an introductory chapter (VI) - si, di, double loop

Previous article:Introduction to assembly learning notes (VII) - dp, div, dup
Next article:Introduction to assembly learning notes (V) - programs containing multiple segments

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号