The move string instructions are used to copy an array from one location to another location. They have one of the following forms:
The semantics of the instructions MOVSB, MOVSW, and MOVSD are illustrated below:
The MOVSB instruction
The MOVSB (move string, byte) instruction fetches the byte at address SI, stores it at address DI and then increments or decrements the SI and DI registers by one.
The MOVSW instruction
The MOVSW (move string, word) instruction fetches the word at address SI, stores it at address DI and then increments or decrements SI and DI by two.
The MOVSD instruction
The MOVSD (move string, double word) instruction fetches the double word at address SI, stores it at address DI and then increments or decrements SI and DI by four.
Let us consider the case of moving (i.e. copying) an array Array1 from one location to another location Array2, assuming the number of elements to move is 100 bytes.
If we use the normal MOV instructions, the program would look like:
However, If the string instructions are used, the code would be:
REP PrefixSince string instructions are designed to operate on arrays of bytes, words, or double words, their execution is repeated depending on the size of the processed arrays. Instead of using the Loop instruction, the string instructions come with a repeat prefix, REP, that allows their execution to be repeated a number of times depending on the unsigned content of the CX register. The semantics of REP are:
While (CX <> 0) execute the string instruction CX = CX -1 end while
The CX register is first checked and if it is not 0, only then is the string instruction is executed. Thus, if CX is 0 to start with, the string instruction is not executed at all. This is in contrast to the LOOP instruction, which first decrements CX and then tests if CX is 0.
Next we show how the above example can be used with the REP prefix.
Note that Array1 can also be copied to Array2 using the MOVSW instruction. This is illustrated in the example below.
We can also copy Array1 into Array2 by starting the copying from the bottom of the arrays. This is called the auto-decrement mode since SI and DI will be decremented after the move operation. This is illustrated in the example below.
In the next example, we illustrate the use of move string instructions to copy Array1 into Array2 in reverse.
Note that in the above example, SI and DI are both incremented after the data movement. However, what we want is to have SI incremented by one while DI is decremented by one. To achieve this, we decrement DI by 2 after the MOVSB instruction so that the net effect on DI is decrementing it by 1. For this reason, we can not use the REP prefix here and we have to use the loop instruction.