29.8.3. x86 string instructions
Intel 64 and IA-32 Architectures Software Developer’s Manuals Volume 1 5.1.8 "String Instructions"
These instructions do some operation on an array item, and automatically update the index to the next item:
-
First example explained in more detail
-
userland/arch/x86_64/stos.S: STOS: STOre String: store register to memory. STOSD is called STOSL in GNU GAS as usual: https://stackoverflow.com/questions/6211629/gcc-inline-assembly-error-no-such-instruction-stosd
-
-
Further examples
-
userland/arch/x86_64/cmps.S: CMPS: CoMPare Strings: compare two values in memory with addresses given by RSI and RDI. Could be used to implement
memcmp
. Store the result in JZ as usual. -
userland/arch/x86_64/lods.S: LODS: LOaD String: load from memory to register.
-
userland/arch/x86_64/movs.S: MOVS: MOV String: move from one memory to another with addresses given by RSI and RDI. Could be used to implement
memmov
. -
userland/arch/x86_64/scas.S: SCAS: SCan String: compare memory to the value in a register. Could be used to implement
strchr
.
-
The RSI and RDI registers are actually named after these intructions! S is the source of string instructions, D is the destination of string instructions: https://stackoverflow.com/questions/1856320/purpose-of-esi-edi-registers
The direction of the index increment depends on the direction flag of the FLAGS register: 0 means forward and 1 means backward: https://stackoverflow.com/questions/9636691/what-are-cld-and-std-for-in-x86-assembly-language-what-does-df-do
These instructions were originally developed to speed up "string" operations such as those present in the <string.h>
header of the C standard library.
However, as computer architecture evolved, those instructions might not offer considerable speedups anymore, and modern glibc such as 2.29 just uses x86 SIMD operations instead:, see also: https://stackoverflow.com/questions/33480999/how-can-the-rep-stosb-instruction-execute-faster-than-the-equivalent-loop