0% found this document useful (0 votes)
145 views15 pages

EEE342-MP-9b-String instructions-CMPS, LODS, STOS

The document discusses string manipulation instructions in the 8086 microprocessor instruction set. It describes instructions like CMPS, SCAS, LODS, and STOS that are used to compare, scan, load, and store strings of bytes or words between memory segments. These instructions use the SI and DI registers to keep track of source and destination offsets, and they automatically increment or decrement these registers after executing depending on the state of the DF flag.

Uploaded by

Bilal Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views15 pages

EEE342-MP-9b-String instructions-CMPS, LODS, STOS

The document discusses string manipulation instructions in the 8086 microprocessor instruction set. It describes instructions like CMPS, SCAS, LODS, and STOS that are used to compare, scan, load, and store strings of bytes or words between memory segments. These instructions use the SI and DI registers to keep track of source and destination offsets, and they automatically increment or decrement these registers after executing depending on the state of the DF flag.

Uploaded by

Bilal Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Microprocessor Systems

and
Interfacing

• String manipulation
• Instructions CMPS, SCAS, LODS, STOS

Slides courtesy:
1. Book ‘The Intel Microprocessors, Architecture,
Programming and Interfacing” , 7ed, by Barry B. Brey
2. http://www.pcpolytechnic.com/computer/ppt/micro/Chap
%203_1.pptx
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions

 String : Sequence of bytes or words

 8086 and above instruction set includes instruction for string movement,
comparison, scan, load and store.

 REP instruction prefix : used to repeat execution of string instructions

 String instructions end with S or SB or SW.


S represents string, SB string byte and SW string word.

 Offset or effective address of the source operand is stored in SI register and


that of the destination operand is stored in DI register.

 Depending on the status of DF, SI and DI registers are automatically


updated.

 DF = 0  SI and DI are incremented by 1 for byte and 2 for word.

 DF = 1  SI and DI are decremented by 1 for byte and 2 for word.

2
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

Compares two sections of memory data as string bytes or string words

CMPS

CMPSB MA = (DS) x 1610 + (SI)


MAE = (ES) x 1610 + (DI)

Its like CMP MAD, MAE Modify flags  (MA) - (MAE)

If (MA) > (MAE), then CF = 0; ZF = 0; SF = 0


If (MA) < (MAE), then CF = 1; ZF = 0; SF = 1
CMPSW
If (MA) = (MAE), then CF = 0; ZF = 1; SF = 0

For byte operation


If DF = 0, then (DI)  (DI) + 1; (SI)  (SI) +
1
If DF = 1, then (DI)  (DI) - 1; (SI)  (SI) - 1

For word operation


If DF = 0, then (DI)  (DI) + 2; (SI)  (SI) +
2 3
If DF = 1, then (DI)  (DI) - 2; (SI)  (SI) - 2
80386 and Above
Microprocessors
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

Compares two sections of memory data as string bytes or string words

CMPS

CMPSD (D for double word)


Only in 80386-Core2

CMPSQ (Q for quad word)


Only in Pentium 4-Core2

4
8086 Microprocessors
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

Example procedure

The contents of the data segment memory location


addressed by SI (LINE) are compared with the contents
of the extra segment memory location addressed by DI
(TABLE).

5
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

• Compares a string byte in AL or word in AX


with a byte or word in memory (extra segment)

• Subtracts memory from AL or AX without affecting


register and memory

• Uses index register DI and extra segment of memory

6
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

SCAS

SCASB MAE = (ES) x 1610 + (DI)


Modify flags  (AL) - (MAE)

If (AL) > (MAE), then CF = 0; ZF = 0; SF = 0


If (AL) < (MAE), then CF = 1; ZF = 0; SF = 1
If (AL) = (MAE), then CF = 0; ZF = 1; SF = 0

If DF = 0, then (DI)  (DI) + 1 (auto increment mode)


If DF = 1, then (DI)  (DI) – 1

SCASW
MAE = (ES) x 1610 + (DI)
Modify flags  (AX) - (MAE)

If (AX) > (MAE ; MAE + 1), then CF = 0; ZF = 0; SF = 0


If (AX) < (MAE ; MAE + 1), then CF = 1; ZF = 0; SF = 1
If (AX) = (MAE ; MAE + 1), then CF = 0; ZF = 1; SF = 0
7
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

Searches the 100bytes BLOCK of memory (in extra segment)


for byte 00H.

8
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

• Loads string byte in to AL or string word in to AX, or


string double word in to EAX (in 80386 and above) from the
data stored in the data segment addressed by SI.

• After loading AL with ONE byte, AX with TWO bytes, or EAX with
FOUR bytes, the contents of SI is either increment (if D=0) by 1, 2 or 4,
Or the contents of SI is decrement (if D=1) by 1, 2 or 4.

9
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

LODS

LODSB MA = (DS) x 1610 + (SI)


(AL)  (MA)

If DF = 0, then (SI)  (SI) + 1


If DF = 1, then (SI)  (SI) – 1

LODSW MA = (DS) x 1610 + (SI)


(AX)  (MA ; MA + 1)

If DF = 0, then (SI)  (SI) + 2


If DF = 1, then (SI)  (SI) – 2

10
8086 and Above
Microprocessors
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

Different forms of LODS instruction

11
8086 and Above
Microprocessors
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

After LODSW is executed

12
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

• Stores AL, AX, or EAX (in 80386 and above) at the extra segment
memory location addressed by DI.

• After AL, AX or EAX is stored in the memory, the contents of DI is


either increment (if D=0) by 1, 2 or 4, Or the contents of SI is
decrement (if D=1) by 1, 2 or 4.

13
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

STOS

STOSB MAE = (ES) x 1610 + (DI)


(MAE)  (AL)

If DF = 0, then (DI)  (DI) + 1


If DF = 1, then (DI)  (DI) – 1

STOSW MAE = (ES) x 1610 + (DI)


(MAE ; MAE + 1 )  (AX)

If DF = 0, then (DI)  (DI) + 2


If DF = 1, then (DI)  (DI) – 2

14
8086 Microprocessor
Instruction Set

4. String Manipulation Instructions


Mnemonics: REP, MOVS, CMPS, SCAS, LODS, STOS

Different forms of STOS instruction

15

You might also like