STOSB writes AL to the byte at ES:DI, then moves DI by +1 (DF = 0) or -1 (DF = 1). STOSW writes AX (AL to the lower address, AH to the next) and steps by 2. No flags change and AL / AX keep their value, so with REP in front the same value is written CX times: a fill.
The destination is always ES:DI with no segment override. That is what makes STOS handy for writing to another segment such as video memory: point ES at it once and leave DS on your data. In an .EXE, ES starts at the PSP, so set it before storing into your own data.
Filling memory
MOV AX, 0B800h ; colour text screenMOV ES, AXXOR DI, DI ; top-left cellMOV AX, 0720h ; space, grey on blackMOV CX, 80*25 ; every cellCLDREP STOSW ; clear the screen
Each cell of the text screen at B800h is a word: the character in the low byte, the colour attribute in the high byte, so a single STOSW writes one coloured character. To zero a buffer, XOR AX, AX and REP STOSW over half its length. The example writes ten letters in the middle of the screen and puts ES back afterwards.
CLDPUSH ESMOV AX, 0B800hMOV ES, AXMOV DI, (12*80+35)*2 ; row 12, column 35MOV AX, 1E41h ; "A", yellow on blueMOV CX, 10CELL1:STOSW ; character + colourINC AL ; next letterLOOP CELL1POP ES
Pitfalls
Forgetting CLD; forgetting that CX counts elements, not bytes, so REP STOSW with CX = 100 fills 200 bytes; and leaving ES pointing at video memory when later code expects ES = DS.
Later CPUs: the 80386 adds STOSD (store EAX).