Delete all elements in an array that are equal to the given number.

Source: Internet
Author: User

 

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:
  1. 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.
  2. ; Requirement: compile a program to search for this number in the array. If this number is found, delete it from the array.
  3. Data Segment
  4. Number DW 9 h
  5. Data ends
  6. Extra segment
  7. List DW 0ah, 01 H, 9 h, 3 h, 4 h, 5 h, 9 h, 6 h, 9 h, 0ah, 9 h
  8. Extra ends
  9. Code segment
  10. Assume DS: data, ES: Extra, CS: Code
  11. Start:
  12. MoV ax, Data
  13. MoV ds, ax
  14. MoV ax, extra
  15. MoV es, ax
  16. MoV Di, offset list
  17. MoV ax, number
  18. MoV dx, 0
  19. MoV CX, ES: [di]
  20. Add Di, 2
  21. ClD
  22. Repne scasw
  23. Jcxz exit
  24. INC DX
  25. MoV Si, Di
  26. Sub Si, 2
  27. Lo:
  28. CMP ax, ES: [di]
  29. JNE next
  30. INC DX
  31. JMP next1
  32. Next:
  33. MoV BX, ES: [di]
  34. MoV ES: [Si], BX
  35. Add Si, 2
  36. Next1:
  37. Add Di, 2
  38. Loop Lo
  39. Exit:
  40. Sub ES: list [0], DX
  41. ; Xchg ES: list [0], DX
  42. Add DL, 30 h
  43. MoV ah, 02 h
  44. Int 21 h
  45. MoV ah, 4ch
  46. Int 21 h
  47. Code ends
  48. End start

After note:

Code:
  1. ; 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.
  2. ; Requirement: compile a program to search for this number in the array. If this number is found, delete it from the array.
  3. Data Segment
  4. Number DW 9 h
  5. Data ends
  6. Extra segment
  7. List DW 0ah, 01 H, 9 h, 3 h, 4 h, 5 h, 9 h, 6 h, 9 h, 0ah, 9 h
  8. Extra ends
  9. Code segment
  10. Assume DS: data, ES: Extra, CS: Code
  11. Start:
  12. MoV ax, Data
  13. MoV ds, ax
  14. MoV ax, extra
  15. MoV es, ax
  16. MoV Di, offset list
  17. MoV ax, number
  18. MoV dx, 0; use to store the number of equal elements
  19. MoV CX, ES: [di]; MoV the number of the array's elements to CX
  20. Add Di, 2; Set di as the address of the first element
  21. CLD; set the DF of psw to 0, the direction is up
  22. Repne scasw; scan the number in ax, until Cx = 0 or find the First Equal Element
  23. Jcxz exit; can't find the equal element in the array, Goto exit
  24. Inc dx; make the number of equal elements increase 1, as one has been found
  25. MoV Si, di; Save the address of the First Equal Element
  26. Sub Si, 2; as di point to the address of the next element of the equal Element
  27. Lo:
  28. CMP ax, ES: [di]; compare the next word
  29. JNE next; if not equal goto next
  30. Inc dx; if equal, increase DX
  31. JMP next1; do other process
  32. Next:; if not equal, mov this element to Si
  33. MoV BX, ES: [di]
  34. MoV ES: [Si], BX
  35. Add Si, 2; change Si to the new first location
  36. Next1:
  37. Add Di, 2; change di to point to the next remaider Element
  38. Loop Lo
  39. Exit:
  40. Sub ES: list [0], DX; set the new array's length
  41. ; Xchg ES: list [0], DX
  42. ; Display the number of the equal elements in the array
  43. Add DL, 30 h
  44. MoV ah, 02 h
  45. Int 21 h
  46. ; The exit DOS system call
  47. MoV ah, 4ch
  48. Int 21 h
  49. Code ends
  50. End start
  51. .

 

 

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.