The STD and CLD instructions

Setting and clearing the direction flag DF.

The STD instruction format

InstructionDescription
STDSet the direction flag (DF = 1).

The CLD instruction format

InstructionDescription
CLDClear the direction flag (DF = 0).

These instructions control the direction flag DF, which determines the direction in which string instructions (MOVS, CMPS, SCAS, LODS, STOS) process their operands. When DF=0, the addresses are incremented (processing goes from lower addresses to higher ones). When DF=1, the addresses are decremented (processing goes from higher addresses to lower ones).

Usage example

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

code segment public        ; Start of the code segment
    assume cs:code

    main proc              ; Start of the code segment
        cld                ; DF = 0, the strings will be processed forward (from lower addresses to higher ones)
        std                ; DF = 1, the strings will be processed backward (from higher addresses to lower ones)

        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
Yes
OF
No