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)
- assume cs:code,ds:data
- data segment
- db 'ABcde'
- db 'fGHIg'
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov bx,0
- mov cx,4
- mov al,00100000b
- s: or [bx],al
- or [5+bx],al ;[5+bx] can also be written as 5[bx]
- inc bx
- loops
- mov ax,4c00H
- int 21H
- code ends
- 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!
- assume cs:code,ds:data
- data segment
- db 'Welcome! '
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov si,0
- mov cx,4
- s: mov ax,[si]
- mov 8[si],ax
- add si,2
- loops
- mov ax,4c00H
- int 21H
- code ends
- end start
Example: Capitalize the first letter of the word in data
- assume cs:code,ds:data
- data segment
- db '1.file '
- db '2.edit '
- db '3.search'
- db '4.view '
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov bx,0
- mov cx,4
- mov al,11011111b
- s: and [bx+2],al
- add bx,8
- loops
- mov ax,4c00H
- int 21H
- code ends
- end start
3. Double loop
Example: Change the words in data to uppercase
- assume cs:code,ds:data
- data segment
- db 'ibm '
- db 'dec '
- db 'dos '
- db 'vax'
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov bx,0
- mov al,11011111b
- mov cx,4
- s: mov cx,3
- mov si,0
- s0: and [bx+si],al
- inc si
- loop s0
- add bx,8
- loops
- mov ax,4c00H
- int 21H
- code ends
- 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.
- assume cs:code,ds:data
- data segment
- db 'ibm '
- db 'dec '
- db 'dos '
- db 'vax'
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov bx,0
- mov al,11011111b
- mov cx,4
- s: mov dx,cx; dx is used to temporarily store the value of cx in the outer layer
- mov cx,3
- mov si,0
- s0: and [bx+si],al
- inc si
- loop s0
- mov cx,dx ; used to restore cx of the outer loop
- add bx,8
- loops
- mov ax,4c00H
- int 21H
- code ends
- 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:
- assume cs:code,ds:data
- data segment
- db 'ibm '
- db 'dec '
- db 'dos '
- db 'vax'
- dw 0 ; define a word to save cx
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov bx,0
- mov al,11011111b
- mov cx,4
- s: mov ds:[20H],cx
- mov cx,3
- mov si,0
- s0: and [bx+si],al
- inc si
- loop s0
- mov cx,ds:[20H]
- add bx,8
- loops
- mov ax,4c00H
- int 21H
- code ends
- 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:
- assume cs:code,ds:data,ss:stack
- data segment
- db 'ibm '
- db 'dec '
- db 'dos '
- db 'vax'
- data ends
- stack segment
- dw 0,0,0,0,0,0,0,0
- stack ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov ax,stack
- mov ss,ax
- mov sp,16
- mov bx,0
- mov al,11011111b
- mov cx,4
- s: push cx
- mov cx,3
- mov si,0
- s0: and [bx+si],al
- inc si
- loop s0
- pop cx
- add bx,8
- loops
- mov ax,4c00H
- int 21H
- code ends
- end start
Example 2: Capitalize the first three letters of the words in data
- assume cs:code,ds:data,ss:stack
- data segment
- db '1.display.......'
- db '2.brows.........'
- db '3.replace.......'
- db '4.modify........'
- data ends
- stack segment
- dw 0,0,0,0,0,0,0,0
- stack ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov ax,stack
- mov ss,ax
- mov sp,16
- mov bx,0
- mov al,11011111b
- mov cx,4
- s: push cx
- mov si,0
- mov cx,3
- s0: and [bx+si+2],al
- inc si
- loop s0
- pop cx
- add bx,10H
- loops
- mov ax,4c00H
- int 21H
- code ends
- end start
Previous article:Introduction to assembly learning notes (VII) - dp, div, dup
Next article:Introduction to assembly learning notes (V) - programs containing multiple segments
- Popular Resources
- Popular amplifiers
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Smith Chart
- CC3200 LaunchPad Development Board IC Resources
- plc wireless remote control water and fertilizer integration + remote soil environment monitoring + cloud platform + smart irrigation + automatic spraying agricultural Internet of Things...
- Three considerations for selecting Ethernet for harsh industrial environments
- Simplify the design of automotive body motor controllers and quickly achieve lightweighting
- Analysis of Directivity and Directivity Coefficient of Microwave Antenna
- Does anyone know how to calculate the input power by first boosting the voltage and then reducing it?
- Remote control aircraft, electric vehicles, motorcycle anti-theft devices, flashlights, etc. single chip computer development
- Why do I/O ports of microcontrollers need drivers?
- Talking about modification, I added a HUD head-up display, and the effect is good!