Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are CLD and STD for in x86 assembly language? What does DF do?

Tags:

x86

assembly

well, I know that CLD clears direction flag and STD sets direction flag. but what's the point in setting and clearing direction flag?

like image 545
Melika Avatar asked Mar 09 '12 15:03

Melika


People also ask

What is CLD and STD?

CLD: clear direction flag so that string pointers auto increment after each string operation. STD: std is used to set the direction flag to a 1 so that SI and/or DI will automatically be decremented to point to the next string element when one of the string instruction executes.

What does CLD mean in assembly language?

Clear Direction Flag (cld)

What is DF flag in x86?

The direction flag is a CPU flag specific to all Intel x86-compatible CPUs. It applies to all assembly instructions that use the REP (repeat) prefix, such as MOVS, MOVSD, MOVSW, and others. Addresses provided to applicable instructions are increased if the direction flag is cleared.

What is role of STD instruction?

This instruction is used to set the Direction Flag to 1 so that SI/DI will automatically decrement when one of the string instructions executes.


2 Answers

The direction flag is used to influence the direction in which string instructions offset pointer registers. These are the same instructions that can be used with the REP prefix to repeat the operation. (Although lods isn't very useful with rep).

The string instructions are: MOVS (copy mem to mem), STOS (store AL/AX/EAX/RAX), SCAS (scan string), CMPS (compare string), and LODS (load string). There's also ins/outs for copying between memory and an IO port. Each of these instructions is available in byte, word, dword, and qword operand sizes.

In a nutshell, when the direction flag is 0, the instructions work by incrementing the pointer to the data after every iteration (until ECX is zero or some other condition, depending on the flavour of the REP prefix), while if the flag is 1, the pointer is decremented.

For example, movsd copies a dword from [ds:esi] to [es:edi] (or rdi in 64-bit mode), and does this: (See the "Operation" section in the linked ISA reference manual entry extracted from Intel's PDFs)

dword [es:edi] = dword [ds:esi]      // 4-byte copy memory to memory if (DF == 0)     esi += 4;     edi += 4; else  // DF == 1     esi -= 4;     edi -= 4; fi 

With a REP prefix, it does this ECX times, and modern x86 CPUs have optimized "fast strings" microcode that does the copying (or stos storing) with 16-byte or 32-byte internal operations. See also this Q&A about memory bandwidth and the ERMSB feature. (Note that only rep stos and rep movs are optimized this way, not repne/repe scas or cmps).

like image 148
Daniel Kamil Kozar Avatar answered Oct 04 '22 15:10

Daniel Kamil Kozar


CLD CLears the Direction flag, data goes onwards. STD SeTs the Direction flag, data goes backwards.

like image 23
Kenneth Zarr Avatar answered Oct 04 '22 15:10

Kenneth Zarr