string instructions Flashcards
(20 cards)
What are the 8086 string instructions and their purposes?
“MOVS: Move data between memory blocks. CMPS: Compare two strings. SCAS: Scan a string for a value. LODS: Load data into AL/AX. STOS: Store AL/AX into memory. REP: Repeat instructions based on CX/flags.”
How does MOVSB
work?
“MOVSB: Copies a byte from [DS:SI] to [ES:DI]. Updates SI/DI: Increments if DF=0 (CLD), decrements if DF=1 (STD). Example: REP MOVSB
copies CX bytes from DS:SI to ES:DI.”
What is the role of the Direction Flag (DF)?
“CLD (DF=0): SI/DI increment after each operation. STD (DF=1): SI/DI decrement. Affects MOVS, CMPS, SCAS, LODS, STOS.”
How does CMPSB
compare strings?
“CMPSB: Subtracts [ES:DI] from [DS:SI], updates flags. Flags: CF=1 if [DS:SI] < [ES:DI], ZF=1 if equal. SI/DI updated based on DF. Example: REPE CMPSB
repeats while equal.”
What does SCASB
do?
“SCASB: Compares AL with [ES:DI]. Flags: ZF=1 if AL == [ES:DI]. DI updated based on DF. Example: REPNE SCASB
scans until AL is found.”
How does LODSW
function?
“LODSW: Loads a word from [DS:SI] into AX. SI incremented/decremented based on DF. Example: LODSW
→ AX = [DS:SI], SI += 2 (if DF=0).”
What is the purpose of STOSW
?
“STOSW: Stores AX into [ES:DI]. DI incremented/decremented based on DF. Example: REP STOSW
fills CX words with AX.”
How do REP prefixes work?
“REP: Repeats until CX=0 (e.g., REP MOVSB
). REPE/REPZ: Repeats while ZF=1. REPNE/REPNZ: Repeats while ZF=0. Example: REPE CMPSB
stops on mismatch or CX=0.”
What flags are affected by CMPSB
?
“Flags: CF (carry), ZF (zero), SF (sign), PF (parity), OF (overflow). ZF=1 if bytes are equal, CF=1 if source < destination.”
How to copy a block of memory using string instructions?
“1. LEA SI, source
; LEA DI, destination
. 2. CLD
(set forward direction). 3. MOV CX, count
. 4. REP MOVSB
. Copies CX bytes from DS:SI to ES:DI.”
What happens during REP STOSB
?
“REP STOSB: Stores AL into [ES:DI] CX times. DI increments/decrements after each store. Example: Fills a buffer with AL value.”
How to find a byte in a string using SCASB
?
“1. LEA DI, string
. 2. CLD
. 3. MOV AL, target
. 4. MOV CX, length
. 5. REPNE SCASB
. Stops when AL matches or CX=0.”
What is the difference between LODSB
and LODSW
?
“LODSB: Loads a byte into AL, SI ±1. LODSW: Loads a word into AX, SI ±2. Example: LODSW
→ AX = [DS:SI], SI += 2 (DF=0).”
How does REPE CMPSB
terminate?
“Stops when CX=0 (end of string) or ZF=0 (mismatch found). Example: Compares strings until they differ.”
What registers are used in string operations?
“SI: Source index (DS segment). DI: Destination index (ES segment). CX: Counter for REP. AX/AL: Data for LODS/STOS/SCAS.”
What is an example of STOSB
usage?
“LEA DI, buffer
; CLD
; MOV AL, 'A'
; MOV CX, 10
; REP STOSB
. Fills 10 bytes in buffer with ‘A’.”
How to reverse a string using STD
?
“1. LEA SI, end_addr
; LEA DI, dest_addr
. 2. STD
(DF=1). 3. MOV CX, len
; REP MOVSB
. Copies backward.”
What happens if CX=0 before REP MOVSB
?
“No operation. REP checks CX first; if CX=0, the instruction skips.”
How does SCASW
differ from SCASB
?
“SCASW: Compares AX with [ES:DI] (word). DI ±2 based on DF. SCASB: Compares AL with [ES:DI] (byte). DI ±1.”
What is the result of CMPSW
if [DS:SI]=1234h and [ES:DI]=5678h?
Flags: CF=1 (1234h < 5678h), ZF=0. SI/DI incremented/decremented by 2 (DF=0).