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
ALto the memory location addressed byEDI(orRDIin 64-bit mode) and increments or decrementsEDIby 1.
2 STOSW:
Store Word
stosw- Stores the word in
AXto the memory location addressed byEDIand increments or decrementsEDIby 2.
3 STOSD:
Store Double Word
stosd- Stores the double word in
EAXto the memory location addressed byEDIand increments or decrementsEDIby 4.
4 STOSQ:
Store Quad Word (x86_64)
stosq- Stores the quad word in
RAXto the memory location addressed byRDIand increments or decrementsRDIby 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:
EDIis set to the starting address of the buffer.ECXis set to the number of times the store should be repeated.EAXis set to the value to be stored (0xDEADBEEF).
2 Clear Direction Flag:
- The
cldinstruction clears the direction flag, ensuringEDIwill be incremented after each store.
3 Store Value:
- The
rep stosdinstruction repeats theSTOSDinstructionECXtimes, storing the value inEAXat the address inEDIand then incrementingEDIby 4 each time.
Detailed Operation
When STOSD is executed:
- The 32-bit value in
EAXis stored at the address pointed to byEDI. - The
EDIregister is updated:- If
DFis clear,EDIis incremented by 4. - If
DFis set,EDIis 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
cldto clear the flag (increment) orstdto set the flag (decrement) as needed. - Performance: Using
rep stosdor other repeat prefixes can be more efficient for initializing large blocks of memory compared to a loop with individual store instructions.
Leave a comment
Your email address will not be published. Required fields are marked *
