The STOSB/STOSW instructions
Store the AL or AX register in memory.
Format of the STOSB/STOSW instructions
| Instruction | Description |
|---|---|
| STOSB | Store AL at the address ES:DI. |
| STOSW | Store AX at the address ES:DI. |
Stores a byte or a word from the al or ax register in the destination operand. The destination operand is a memory location whose address is read from the es:di register. The es segment cannot be overridden by a segment override prefix.
After the byte or word is transferred from the register into the memory location, the di register is incremented or decremented according to the DF flag setting in the FLAGS register. If the DF flag is 0, the di register is incremented; if the DF flag is 1, the di register is decremented (the register is incremented or decremented by 1 for byte operations and by 2 for word operations).
First example: filling an array with a value
title .model small .stack 100h data segment public array dw 50 dup(?) arrlen equ ($-array) / 2 data ends code segment public ; Start of the code segment assume ds:data, cs:code main proc ; Start of the main procedure mov ax, seg data ; Get the data segment address and store it in the ax register mov ds, ax ; Pass the data segment address from ax to ds mov es, ax ; Pass the data segment address from ax to es mov cx, arrlen ; Set the counter equal to the length of the array array mov ax, 0CCCCh ; Copy the number we want to fill the array with into ax lea di, es:[array] ; Load the address of the variable array into the es:di register rep stosw ; Copy from ax to es:di, incrementing di by 2 while CX≠0 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)".
Second example: copying the elements that satisfy a condition
title .model small .stack 100h data segment public aArray dw 6, 4, 7, 2, 5, 3, 1, 9, 8 aArrlen equ ($-aArray) / 2 bArray dw aArrlen dup(?) data ends code segment public ; Start of the code segment assume ds:data, cs:code main proc ; Start of the main procedure mov ax, seg data ; Get the data segment address and store it in the ax register mov ds, ax ; Pass the data segment address from ax to ds mov es, ax ; Pass the data segment address from ax to es mov cx, aArrlen ; Set the counter equal to the length of the array array lea di, es:[bArray] ; Load the address of the variable bArray into the es:di register lea si, ds:[aArray] ; Load the address of the variable aArray into the ds:si register xor bx, bx ; Clear the counter of the copied elements MainLoop: lodsw ; Load the number at the address si into the ax register, incrementing si by 2 cmp ax, 5 ; Compare the value in ax with the number 5 jnge Skip ; If SF≠OF, jump to the Skip label stosw ; Store the number in the ax register at the address di, incrementing di by 2 inc bx ; Increment the bx register by 1 Skip: loop MainLoop ; Loop while CX≠0 mov ax, bx ; Copy bx into ax. AX := the number of copied elements 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
Note: the DF flag is not changed by the STOSB/STOSW instructions, but it affects the direction of processing (increment/decrement of the index).