1 / 16

Assembly 07

Assembly 07. String Processing. Definition. Defining strings Using sting length Explicitly string DB ’Error message’ str_len DW 13 Relative string DB ’Error message’ str_len DW $ - string Zero Ending string1 DB ’This is OK’,0 string2 DB ’Price = $9.99’,0.

Download Presentation

Assembly 07

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assembly 07 String Processing

  2. Definition • Defining strings • Using sting length • Explicitly string DB ’Error message’ str_len DW 13 • Relative string DB ’Error message’ str_len DW $ - string • Zero Ending string1 DB ’This is OK’,0 string2 DB ’Price = $9.99’,0

  3. String instruction • String instruction may require a source operand, a destination operand, or both. • String instructions use ESI and EDI registers to point to the source and destination operands. • The source operand is assumed to be at ESI and the destination operand at EDI in memory. • For 16-bit segments SI and DI are used • The registers/indices are update automatically as the instruction is performed

  4. Rep instruction • Rep instruction • unconditional repeat rep REPeat • conditional repeat repe REPeat while Equal repz REPeat while Zero repne REPeat while Not Equal repnz REPeat while Not Zero • This instruction causes the instruction to repeat according to the value in ECX/CX while (ECX = 0) execute the string instruction; ECX := ECX–1; end while

  5. Direction Flag • The direction of string operations depends on the value of the direction flag. • Direction flag (DF) is clear (DF = 0) • String operations proceed in the forward direction, from head to tail of a string. • Direction flag (DF) is set (DF = 1) • String processing is done in the opposite direction, from head to tail of a string. • DF instruction std set direction flag (DF = 1) cld clear direction flag (DF = 0)

  6. String instructions • String move • Formats: movs dest_string,source_string movsb movsw Movsd The suffix b, w, or d is used to indicate byte, word, or doubleword operands. • The source string value is pointed to by ESI and the destination string location is indicated by EDI in memory. • After copying, the ESI and EDI registers are updated according to the value of the direction flag.

  7. Code Example .DATA string1 db ’The original string’,0 strLen EQU $ - string1 .UDATA string2 resb 80 .CODE .STARTUP mov AX, DS ; set up ES mov ES, AX ; to the data segment mov ECX, strLen ; strLen includes NULL mov ESI, string1 mov EDI, string2 cld ; forward direction rep movsb

  8. Load String Load a String (lods) This instruction copies the value from the source string (pointed to by ESI) in memory to AL (for byte operands—lodsb),AX (forword operands—lodsw), or EAX (for doubleword operands—lodsd). lodsb— load a byte string AL := (ESI) ; copy a byte if (DF = 0) ; forward direction then ESI := ESI+1 else ; backward direction ESI := ESI−1 end if

  9. Store a String (stos) This instruction performs the complementary operation. It copies the value in AL (for stosb), AX (for stosw), or EAX (for stosd) to the destination string (pointed to by ES:EDI) in memory. stosb— store a byte string ES:EDI := AL ; copy a byte if (DF = 0) ; forward direction then EDI := EDI+1 else ; backward direction EDI := EDI−1 end if Flags affected: none

  10. Initializing an array Initializing array with −1. .UDATA array1 resw 100 .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,100 mov EDI,array mov AX,-1 cld ; forward direction rep stosw

  11. String Compare Instruction The cmps instruction can be used to compare two strings. Like the cmp instruction, cmps performs ESI − EDI cmpsb— compare two byte strings Compare the two bytes at ESI and EDI and set flags if (DF = 0) then ; forward direction ESI := ESI+1 EDI := EDI+1 else ; backward direction ESI := ESI−1 EDI := EDI−1 end if Flags affected: As per cmp instruction

  12. Using cmps We can use conditional jumps like ja, jg, jc, etc. to test the relationship of the two values. As usual, the ESI and EDI registers are updated according to the value of the direction flag and the operand size. The cmps instruction is typically used with the repe/repz or repne/repnz prefix. .DATA string1 db ’abcdfghi’,0 strLen EQU $ - string1 string2 db ’abcdefgh’,0 .CODE

  13. Using cmps .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,strLen mov ESI,string1 mov EDI,string2 cld ; forward direction repe cmpsb leaves ESI pointing to g in string1 and EDI to f in string2. Therefore, adding dec ESI dec EDI leaves ESI and EDI pointing to the last character that differs. Then we can use, ja str1Above

  14. Scanning a String The scas (scanning a string) instruction is useful in searching for a particular value or character in a string. The value should be in AL (for scasb), AX (for scasw), or EAX (for scasd), and ES:EDI should point to the string to be searched. scasb— scan a byte string Compare AL to the byte at ES:EDI and set flags if (DF = 0) then; forward direction EDI := EDI+1 else ; backward direction EDI := EDI−1 end if Flags affected: As per cmp instruction

  15. Using scas .DATA string1 db ’abcdefgh’,0 strLen EQU $ - string1 .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,strLen mov EDI,string1 mov AL,’e’ ; character to be searched cld ; forward direction repne scasb dec EDI This program leaves EDI pointing to e in string1. The following example can be used to skip the initial blanks.

  16. Example: Skip initial blanks .DATA string1 db ’ abc’,0 strLen EQU $ - string1 .CODE .STARTUP mov AX,DS ; set up ES mov ES,AX ; to the data segment mov ECX,strLen mov EDI,string1 mov AL,’ ’ ; character to be searched cld ; forward direction repe scasb dec EDI

More Related