The STOS
instruction family in x86 assembly is used for storing data from a register into memory. These instructions are part of the string operation instructions and come in different forms based on the size of the data being stored: STOSB
for bytes, STOSW
for words, STOSD
for double words, and STOSQ
for quad words (in x86-64).
Syntax and Forms
1 STOSB:
Store Byte
stosb
- Stores the byte in
AL
to the memory location addressed byEDI
(orRDI
in 64-bit mode) and increments or decrementsEDI
by 1.
2 STOSW:
Store Word
stosw
- Stores the word in
AX
to the memory location addressed byEDI
and increments or decrementsEDI
by 2.
3 STOSD:
Store Double Word
stosd
- Stores the double word in
EAX
to the memory location addressed byEDI
and increments or decrementsEDI
by 4.
4 STOSQ:
Store Quad Word (x86_64)
stosq
- Stores the quad word in
RAX
to the memory location addressed byRDI
and increments or decrementsRDI
by 8.
The increment or decrement of the EDI
/RDI
register depends on the state of the direction flag (DF
):
- If the direction flag is clear (
DF = 0
), the register is incremented. - If the direction flag is set (
DF = 1
), the register is decremented.
Example
Here is an example showing how to use STOSD
to fill a block of memory with a specific value:
section .data
buffer times 16 dd 0 ; Define a buffer of 16 double words (64 bytes)
section .text
global _start
_start:
; Fill buffer with the value 0xDEADBEEF
mov edi, buffer ; Point EDI to the start of the buffer
mov ecx, 16 ; Number of double words to fill
mov eax, 0xDEADBEEF ; Value to store
cld ; Clear the direction flag (increment EDI)
rep stosd ; Repeat STOSD ECX times
; Exit program (Linux syscall)
mov eax, 1 ; Syscall number for exit
xor ebx, ebx ; Exit code 0
int 0x80 ; Call kernel
Explanation:
1 Initialize Registers:
EDI
is set to the starting address of the buffer.ECX
is set to the number of times the store should be repeated.EAX
is set to the value to be stored (0xDEADBEEF).
2 Clear Direction Flag:
- The
cld
instruction clears the direction flag, ensuringEDI
will be incremented after each store.
3 Store Value:
- The
rep stosd
instruction repeats theSTOSD
instructionECX
times, storing the value inEAX
at the address inEDI
and then incrementingEDI
by 4 each time.
Detailed Operation
When STOSD
is executed:
- The 32-bit value in
EAX
is stored at the address pointed to byEDI
. - The
EDI
register is updated:- If
DF
is clear,EDI
is incremented by 4. - If
DF
is set,EDI
is decremented by 4.
- If
Considerations
- Preserving Registers: If other parts of your program rely on the values of
EDI
,EAX
, orECX
, make sure to save and restore these registers as needed. - Direction Flag: The state of the direction flag affects whether the index register is incremented or decremented. Use
cld
to clear the flag (increment) orstd
to set the flag (decrement) as needed. - Performance: Using
rep stosd
or other repeat prefixes can be more efficient for initializing large blocks of memory compared to a loop with individual store instructions.