ICS 312 Lecture 13 String Instructions What is

  • Slides: 27
Download presentation
ICS 312 Lecture 13 String Instructions

ICS 312 Lecture 13 String Instructions

What is String n String - a sequence of characters Note. The only string

What is String n String - a sequence of characters Note. The only string instructions you will be expected to know are: movsb cmpsb stosb The other string instructions described are just for background reading.

Operations that can be performed with string instructions n n Copy a string into

Operations that can be performed with string instructions n n Copy a string into another string Store characters in a string Compare strings of characters alphanumerically Search a string for a particular byte or word (not covered in our course)

Direction Flag n n one of Pentium's processor control flags. controls the direction of

Direction Flag n n one of Pentium's processor control flags. controls the direction of string operations: DF = 0 => forward (left to right) processing DF = 1 => backward (right to left) processing CLD - clears the DF; sets DF = 0 STD - sets DF = 1

Moving (Copying) Strings Instructions: MOVSB - copies contents of BYTE given by DS: SI

Moving (Copying) Strings Instructions: MOVSB - copies contents of BYTE given by DS: SI into ES: DI MOVSW - copies contents of WORD given by DS: SI into ES: DI MOVSD - copies contenst of DOUBLE WORD given by DS: SI into ES: DI

Moving (Copying) Strings (Cont. 1) Notes: The string instructions use DS: SI as the

Moving (Copying) Strings (Cont. 1) Notes: The string instructions use DS: SI as the Source string and ES: DI as the destination string. (For the string instructions only, DI references an offset in the ES by default).

Moving (Copying) Strings (Cont. 2) Example: . DATA STRING 1 DB 'HELLO' STRING 2

Moving (Copying) Strings (Cont. 2) Example: . DATA STRING 1 DB 'HELLO' STRING 2 DB 5 DUP (0). CODE MOV AX, @DATA MOV DS, AX MOV ES, AX LEA SI, STRING 1 LEA DI, STRING 2 CLD MOVSB ; init DS ; init ES ; source ; destination ; DF = 0 ; mov 1 st byte ; mov 2 nd byte Note: MOVSB moves only 1 byte at a time. Instead … Set CX = count and use the REP prefix to move a specified number of bytes.

Moving (Copying) Strings (Cont. 3) Example: CLD PUSH DS POP ES LEA SI, STRING

Moving (Copying) Strings (Cont. 3) Example: CLD PUSH DS POP ES LEA SI, STRING 1 LEA DI, STRING 2 MOV CX, 5 REP MOVSB ; forward direction ; set ES = DS ; set SI to source ; set DI to destination ; copies 5 chars Note: both SI and DI are incremented for each byte that is copied from the offset given in SI to the offset given in DI (in the forward direction)

Moving (Copying) Strings (Cont. 4) Reverse: STD PUSH DS POP ES LEA SI, STRING

Moving (Copying) Strings (Cont. 4) Reverse: STD PUSH DS POP ES LEA SI, STRING 1+4 LEA DI, STRING 2+4 MOV CX, 5 REP MOVSB ; reverse direction ; ES = DS ; end of string ; copy 5 chars Note: both SI and DI are decremented by one for each byte that is copied from the offset given in SI to the offset given in (in the reverse direction).

Moving (Copying) Strings (Cont. 5) MOVSW works the same way as MOVSB except that

Moving (Copying) Strings (Cont. 5) MOVSW works the same way as MOVSB except that it moves one word (2 bytes) at a time. Consequently, SI and DI will be incremented or decremented by 2 bytes for each word copied. MOVSD moves one double word (4 bytes) at a time. Consequently, SI and DI will be incremented or decremented by 4 for each word copied.

Moving (Copying) Strings (Cont. 6) Example MES 1 LMES 1 MES 2 DB ‘We

Moving (Copying) Strings (Cont. 6) Example MES 1 LMES 1 MES 2 DB ‘We are ready’ EQU $-MES 1 DB 30 DUP (? ) ; to move characters from MES 1 to MES 2 CLD LEA SI, MES 1 LEA DI, MES 2 PUSH DS POP ES MOV CX, LMES 1 REP MOVSB

Moving (Copying) Strings (Cont. 7) MES DW 10, 25, 40, 50, 60, 70, ?

Moving (Copying) Strings (Cont. 7) MES DW 10, 25, 40, 50, 60, 70, ? To insert a word containing 30 into the sequence INCORRECT CODE CLD PUSH DS POP ES LEA SI, MES+4 LEA DI, MES+6 MOV CX, 4 REP MOVSW

Moving (Copying) Strings (Cont. 8) CORRECT CODE STD PUSH DS POP ES LEA SI,

Moving (Copying) Strings (Cont. 8) CORRECT CODE STD PUSH DS POP ES LEA SI, MES+10 LEA DI, MES+12 MOV CX, 4 REP MOVSW MOV MES+4, 30

Storing Strings Instructions: STOSB - copies contents of AL to BYTE address given by

Storing Strings Instructions: STOSB - copies contents of AL to BYTE address given by ES: DI. DI is incremented/decremented by 1. STOSW - copies the contents of AX to the WORD address given by ES: DI. DI is incremented/decremented by 2. STOSD - copies contents of EAX to the DOUBLE WORD address given by ES: DI. DI is incremented/decremented by 4.

Storing Strings (Cont. 1) Example: MOV AX, @DATA MOV ES, AX LEA DI, STRING

Storing Strings (Cont. 1) Example: MOV AX, @DATA MOV ES, AX LEA DI, STRING 1 CLD MOV AL, 'A‘ ; initialize ES ; assume BYTE string STOSB ; store 1 st byte of A STOSB ; store 2 nd byte of A ; best to put no. of bytes in CX and use REP STOSB

Storing Strings (Cont. 2) EXAMPLE Buffer DB ; to set a CLD PUSH POP

Storing Strings (Cont. 2) EXAMPLE Buffer DB ; to set a CLD PUSH POP LEA MOV REP 800 DUP (? ) buffer to nulls DS ES DI, Buffer CX, 800 AL, ‘ ‘ STOSB ; set AL to null

Compare String Instructions: CMPSB - compares BYTE at ES: DI with BYTE at DS:

Compare String Instructions: CMPSB - compares BYTE at ES: DI with BYTE at DS: SI and sets flags. CMPSW - compares WORD at ES: DI with WORD at DS: SI and sets flags. CMPSD - compares DOUBLE WORD at ES: DI with WORD at DS: SI and sets flags.

Compare String (Cont. 1) Example: To jump to “unequal” if the strings do not

Compare String (Cont. 1) Example: To jump to “unequal” if the strings do not match. DATA STRING 1 DB 'ACD' STRING 2 DB 'ABC‘. CODE MOV AX, @DATA MOV DS, AX MOV ES, AX CLD LEA SI, STRING 1 LEA DI, STRING 2 MOV CX, 3 REPE CMPSB JNE unequal ; string length ; repeat while strings match

Compare String (Cont. 2) Note: It increments (or decrements) each string pointer and successively

Compare String (Cont. 2) Note: It increments (or decrements) each string pointer and successively compares bytes until there is a mismatch between the bytes being compared, or until CX = 0. CMPSB can be used to determine whether two strings match, or whether one string is a substring of another string.

Load String (OPTIONAL) Instructions: LODSB - moves the BYTE at address DS: SI into

Load String (OPTIONAL) Instructions: LODSB - moves the BYTE at address DS: SI into AL. SI is incremented/decremented by 1. LODSW - moves the WORD at address DS: SI into AX. SI is incremented/decremented by 2. LODSD - moves the DOUBLE WORD at address DS: SI into EAX. SI is incremented/decremented by 4.

Scan String (OPTIONAL) Instructions: SCASB - compares BYTE at ES: DI with AL and

Scan String (OPTIONAL) Instructions: SCASB - compares BYTE at ES: DI with AL and sets flags according to result. SCASW - compares WORD at ES: DI with AX and sets flags. SCASD - compares DOUBLE WORD at ES: DI with EAX and sets flags.

Scan String (Cont. 1) Example: . DATA STRING 1 DB 'ABC‘. CODE MOV AX,

Scan String (Cont. 1) Example: . DATA STRING 1 DB 'ABC‘. CODE MOV AX, @DATA MOV AX, ES CLD ; initialize ES ; left to right LEA DI, STRING 1 MOV AL, 'B' ; target character SCASB ; scan first byte

Scan String (Cont. 2) Note: when the target ("B") is found, ZF (the zero

Scan String (Cont. 2) Note: when the target ("B") is found, ZF (the zero flag) = 1 and DI points to the byte following the target since DI is automatically incremented by SCASB. So: Set CX = count and use: REPNE SCASB REPNZ SCASB to repeat the scan until the target byte is found, or until the entire string has been searched (i. e. , CX = 0).

Scan String (Cont. 3) ; to count the MES DB LMES EQU Count DB

Scan String (Cont. 3) ; to count the MES DB LMES EQU Count DB number of ‘A’s in a string ‘ABRACADABRA’ $-MES ? . CODE … CLD PUSH DS POP ES MOV Count, 0 LEA DI, MES MOV CX, LMES MOV AL, ‘A’

Scan String (Cont. 4) AGAIN EXIT JCXZ REPNE JNE ADD JMP EXIT SCASB EXIT

Scan String (Cont. 4) AGAIN EXIT JCXZ REPNE JNE ADD JMP EXIT SCASB EXIT Count, 1 AGAIN

BYTE, WORD, and DOUBLE WORD form of string instructions INSTRUCTION SOURCE DEST BYTE WORD

BYTE, WORD, and DOUBLE WORD form of string instructions INSTRUCTION SOURCE DEST BYTE WORD DOUBLE WORD Move String DS: SI ES: DI MOVSB MOVSW MOVSD Compare String DS: SI ES: DI CMPSB CMPSW CMPSD Store string AL/AX/EAX ES: DI STOSB STOSW STOSD Load string DS: SI AL/AX/EAX LODSB LODSW LODSD Scan string AL/AX/EAX ES: DI SCASB SCASW SCASD The operands are IMPLICIT.

Textbook Reading (Jones): Chapter 17

Textbook Reading (Jones): Chapter 17