Question:; in the additional segment, there is a list of unordered word groups with the first address, which is stored in the length of the array in the first of the array, the first address of the array is included in the di register. There is a number in the ax register.
Requirement: compile a program to search for this number in the array. If this number is found, delete it from the array.
Train of Thought: the analysis of this array may contain more than one element equal to this number, but may not have one. It can be implemented in the same way as below:
First, find the first element equal to the number:
1> exit the operation if there are no equal elements.
2> If an equal element exists and the position Si of the element is recorded at the same time, and the element is deleted, you can move the subsequent element forward,
3> when moving forward, consider whether it is equal to this number,
A. If they are equal, no other processing is performed except for moving forward the cursor di pointing to the post element.
B. If not, it is transmitted to the position indicated by Si, and then Si and Di are moved forward simultaneously.
All elements in the Goto 3 and until arrays are traversed.
Implementation: considering that string search commands combined with repeated prefix repe/repz or repne/repnz can implement the search function:
SCAs ES: [di]: Operation on the target address
Scasb: Performs byte operations and implements the following functions:
(Al)-(DI), if df = 0 as CLD, (DI) <---- (DI) + 1, else df = 1 as STF, (DI) <------ (DI)-1
Scasw: The following functions are implemented for word operations:
(Ax)-(dx), if df = 0 as CLD, (DI) <---- (DI) + 2, else df = 1 as STF, (DI) <------ (DI)-2
Command function: the command compares the content of Al (or ax) with a byte (or word) in the additional segment specified by (DI), but does not save the result, only the result is configured with a condition code (that is, the value of the program status word: psw ).
The preceding commands are usually combined with the repeated prefix repe/repz or repne/repnz to compare two data strings or find the specified data in a data string.
The rep prefix is usually used in combination with the movs (string transmission, movsb, movsw), STOs (string storage, stosb, stosw), and lods (get string, lodsw, lodsb) commands.
Format: rep command
Function: 1. test whether the Cx value is 0. If the value is 0, exit the execution of the command and execute the next command of the command; otherwise, go to 2,
2. (CX) <------ (CX)-1
3. Execute the subsequent commands
4. Repeat the 1.2.3 operation
In addition to testing CX, repe/repz or repne/repnz with a duplicate prefix with a condition code, it also tests the setting of the condition code after executing the command (that is, the effect on psw, ZF flag changes)
Do not confuse the association of repeated prefixes with instructions.
There is also a jump command: The jcxz label is worth mentioning to test whether the Cx is 0, and if it is 0, it is transferred to the specified number.
Code:
- In the additional segment, there is an unsorted word group whose first address is list, and the first address of the array is stored in the length of the array. The first address of the array has been placed in the di register. There is a number in the ax register.
- ; Requirement: compile a program to search for this number in the array. If this number is found, delete it from the array.
- Data Segment
- Number DW 9 h
- Data ends
- Extra segment
- List DW 0ah, 01 H, 9 h, 3 h, 4 h, 5 h, 9 h, 6 h, 9 h, 0ah, 9 h
- Extra ends
- Code segment
- Assume DS: data, ES: Extra, CS: Code
- Start:
- MoV ax, Data
- MoV ds, ax
- MoV ax, extra
- MoV es, ax
- MoV Di, offset list
- MoV ax, number
- MoV dx, 0
- MoV CX, ES: [di]
- Add Di, 2
- ClD
- Repne scasw
- Jcxz exit
- INC DX
- MoV Si, Di
- Sub Si, 2
- Lo:
- CMP ax, ES: [di]
- JNE next
- INC DX
- JMP next1
- Next:
- MoV BX, ES: [di]
- MoV ES: [Si], BX
- Add Si, 2
- Next1:
- Add Di, 2
- Loop Lo
- Exit:
- Sub ES: list [0], DX
- ; Xchg ES: list [0], DX
- Add DL, 30 h
- MoV ah, 02 h
- Int 21 h
- MoV ah, 4ch
- Int 21 h
- Code ends
- End start
After note:
Code:
- ; In the additional segment, there is a list of unordered word groups with the first address, which is stored in the length of the array in the first of the array, the first address of the array is included in the di register. There is a number in the ax register.
- ; Requirement: compile a program to search for this number in the array. If this number is found, delete it from the array.
- Data Segment
- Number DW 9 h
- Data ends
- Extra segment
- List DW 0ah, 01 H, 9 h, 3 h, 4 h, 5 h, 9 h, 6 h, 9 h, 0ah, 9 h
- Extra ends
- Code segment
- Assume DS: data, ES: Extra, CS: Code
- Start:
- MoV ax, Data
- MoV ds, ax
- MoV ax, extra
- MoV es, ax
- MoV Di, offset list
- MoV ax, number
- MoV dx, 0; use to store the number of equal elements
- MoV CX, ES: [di]; MoV the number of the array's elements to CX
- Add Di, 2; Set di as the address of the first element
- CLD; set the DF of psw to 0, the direction is up
- Repne scasw; scan the number in ax, until Cx = 0 or find the First Equal Element
- Jcxz exit; can't find the equal element in the array, Goto exit
- Inc dx; make the number of equal elements increase 1, as one has been found
- MoV Si, di; Save the address of the First Equal Element
- Sub Si, 2; as di point to the address of the next element of the equal Element
- Lo:
- CMP ax, ES: [di]; compare the next word
- JNE next; if not equal goto next
- Inc dx; if equal, increase DX
- JMP next1; do other process
- Next:; if not equal, mov this element to Si
- MoV BX, ES: [di]
- MoV ES: [Si], BX
- Add Si, 2; change Si to the new first location
- Next1:
- Add Di, 2; change di to point to the next remaider Element
- Loop Lo
- Exit:
- Sub ES: list [0], DX; set the new array's length
- ; Xchg ES: list [0], DX
- ; Display the number of the equal elements in the array
- Add DL, 30 h
- MoV ah, 02 h
- Int 21 h
- ; The exit DOS system call
- MoV ah, 4ch
- Int 21 h
- Code ends
- End start
- .