When working with strings, the advantages of the MOVS and CMPS instructions over the MOV and CMP instructions are:

  1. They are only 1 byte long.
  2. Both operands
  3. Their auto-indexing obviates the need for separate incrementing or decrementing instructions, thus decreasing overall processing time.
As an example consider the problemof moving the contents of a block of memory to another area in memory.A solution that uses only the MOV instruction, which cannot perform a memory-to-memory transfer, is shown in Fig.5-2(a).A solution
                MOV SI, OFFSET STRING1    ;USE SI AS SOURCE INDEX
                MOV DI, OFFSET STRING2    ;USE DI AS DESTINATION INDEX
                MOV CX, LENGTH STRING1    ;PUT LENGTH IN CX
    MOVE:       MOV AL, (SI)              ;MOVE BYTE FROM SOURCE
                MOV (DI), AL              ;TO DESTINATION
                INC SI                    ;INCREMENT SOURCE INDEX
                INC DI                    ;INCREMENT DESTINATION INDEX
                LOOP MOVE

                      (a)Uses the MOV instruction  


  
                MOV SI, OFFSET STRING1    ;ASUME (DS)=(ES)
                MOV DI, OFFSET STRING2 
                MOV CX, LENGTH STRING1 
                CLD                       ;CLEARALL FLAG
    MOVE:       MOVS STRING2, STRING1     ;FOR AUTO-INCREMENTING
                LOOP MOVE

                      (B)Uses primitive MOVS

            Figure 5-2 Program sequences for moving a block data     
Note that the second programsequence may move either bytes or words, depending on the type of STRING1 and STRING2.In Sec.5-2 it will be seen that this task can be performed even more efficiently by applying the REP prefix to eliminate the explicit loop.

The program sequence given in Fig.5-3 demonstrates the use of the DF flag by showing howdata can be moved from an area to an overlapping area.

 Figure 5-3  Moving a block of data between twooverlapping areas
        
          MOV SI, SRCADDR        ;ASSUME (DS)=(ES)
          MOV DI, DSTADDR
          MOV CX, N              ;PUT LENGTH IN CX
          CLD                    ;CLEAR DF FLAG
          CMP SI, DI              ;IF ADDR OF STRG1 IS GREATER
          JA MOVE                ;THAN ADDR OF STRG2, START FROM
          STD                    ;THE FIRST ELEMENT;OTHERWISE,
          ADD SI, CX              ;START FROM THE LAST ELEMENT
          DEC SI 
          ADD DI, CX
          DEC DI
  MOVE:   MOVSB
          LOOP MOVE
             .
             .
             .

PRETHODNA FOLIJA SADRZAJ SLEDECA FOLIJA