The MOVSB and MOVSW instructions

Moving a byte or a word from memory to memory.

Format of the MOVSB/MOVSW instructions

InstructionDescription
MOVSBMove the byte at address DS:SI to ES:DI.
MOVSWMove the word at address DS:SI to ES:DI.

Moves the byte or word specified by the second operand (the source operand) to the location specified by the first operand (the destination operand). Both the source and the destination operands are in memory. The address of the source operand is read from the ds:si register. The address of the destination operand is read from the es:di register. The ds segment can be overridden by a segment override prefix, but the es segment cannot be overridden.

The locations of the source and destination operands are always specified by the ds:si and es:di registers, which must be properly loaded before the string move instruction is executed.

After the move operation, the si and di registers are automatically incremented or decremented according to the setting of the DF flag in the FLAGS register. (If the DF flag is 0, the si and di registers are incremented; if the DF flag is 1, the si and di registers are decremented.) The registers are incremented or decremented by 1 for byte operations or by 2 for word operations.

First example: copying a byte array using a loop

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    aArray  db  1, 2, 3, 4, 5, 6, 7
    aArrlen equ ($-aArray)
    bArray  db  aArrlen dup(0)
data ends

code segment public              ; Start of the code segment
    assume ds:data, cs:code

    main proc                    ; Start of the main procedure
        mov     ax, seg data     ; Get the data segment address and store it in the ax register
        mov     ds, ax           ; Pass the data segment address from ax to ds
        mov     es, ax           ; Pass the data segment address from ax to es

        mov     cx, aArrlen      ; Set the counter equal to the length of the array aArray
        lea     di, es:[bArray]  ; Load the address of the variable bArray into the es:di register
        lea     si, ds:[aArray]  ; Load the address of the variable aArray into the ds:si register

    CopyForwardLoop:
        movsb                    ; Copy the value from ds:si to es:di, incrementing si and di by 1
        loop    CopyForwardLoop  ; Loop while CX≠0

        mov     ax, 4C00h        ; Copy the process termination function number into ah and the value into al
        int     21h              ; Call the interrupt
    main endp                    ; End of the main procedure

code ends                        ; End of the code segment
end main                         ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Second example: copying an array of words from the end using REP

In this example we will use the rep prefix, which repeats the string instruction the number of times specified in the cx register, or until the specified ZF flag condition is no longer met.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    aArray  dw  1, 2, 3, 4, 5, 6, 7, 8, 9
    aArrlen equ ($-aArray) / 2
    bArray  dw  aArrlen dup(0)
data ends

code segment public              ; Start of the code segment
    assume ds:data, cs:code

    main proc                    ; Start of the main procedure
        mov     ax, seg data     ; Get the data segment address and store it in ax
        mov     ds, ax           ; Pass the data segment address from ax to ds
        mov     es, ax           ; Pass the data segment address from ax to es

        mov     cx, aArrlen      ; Set the counter equal to the length of the array aArray
        lea     di, es:[bArray]  ; Load the address of the variable bArray into the es:di register
        lea     si, ds:[aArray]  ; Load the address of the variable aArray into the ds:si register

        shl     cx, 1            ; Multiply the value in the cx register by 2
        sub     cx, 2            ; Subtract 2 from cx
        add     si, cx           ; Add cx to si. It now points to the last element of the array
        add     di, cx           ; Add cx to di. It now points to the last element of the array
        mov     cx, aArrlen      ; Set the counter back to the length of aArray

        std                      ; Set the direction flag; DF=1

    rep movsw                    ; Copy from ds:si to es:di, decrementing si and di by 2 while CX≠0

        cld                      ; Clear the direction flag; DF=0

        mov     ax, 4C00h        ; Copy the process termination function number into ah and the value into al
        int     21h              ; Call the interrupt
    main endp                    ; End of the main procedure

code ends                        ; End of the code segment
end main                         ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Third example: copying a string between different segments

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    string db  "Hello "
    strlen equ ($-string)
data ends

extr segment public
    strdst db 6 dup(?), "World!"
    strptr dd strdst
extr ends

code segment public              ; Start of the code segment
    assume ds:data, es:extr, cs:code

    main proc                    ; Start of the main procedure
        mov     ax, seg data     ; Get the address of the data segment data and store it in ax
        mov     ds, ax           ; Pass the data segment address from ax to ds

        mov     ax, seg extr     ; Get the address of the data segment extr and store it in ax
        mov     es, ax           ; Pass the data segment address from ax to es

        mov     cx, strlen       ; Set the counter equal to the length of string
        les     di, es:[strptr]  ; Load the far pointer strptr into the es:di register
        lea     si, ds:[string]  ; Load the address of the variable string into the ds:si register

  repnz movsb                    ; Copy from ds:si to es:di, incrementing si and di by 1 while CX≠0

        mov     ax, 4C00h        ; Copy the process termination function number into ah and the value into al
        int     21h              ; Call the interrupt
    main endp                    ; End of the main procedure

code ends                        ; End of the code segment
end main                         ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Processor status flags

CF
No
PF
No
AF
No
ZF
No
SF
No
TF
No
IF
No
DF
No
OF
No

Note: the DF flag is not changed by the MOVSB/MOVSW instructions, but it affects the direction of processing (increment/decrement of the indices).