150 likes | 322 Views
CEN 226 : Computer Organization & Assembly Language : CSC 225 (Lec#10). By Dr. Syed Noman. String manipulation in assembly. Computer applications along with arithmetical processing involve work such as text processing, searching of files, sorting of names etc.
E N D
CEN 226: Computer Organization & Assembly Language :CSC 225(Lec#10) By Dr. Syed Noman
String manipulation in assembly • Computer applications along with arithmetical processing involve work such as text processing, searching of files, sorting of names etc. • The core of such tasks is string, which is an arbitrary sequence of characters. • The 8086/88 is endowed with instructions for handling text or strings.
String manipulation instructions • There are 5 primitive string manipulation instructions: • MOVS • STOS • LODS • CMPS • SCAS • REP instruction is used to provide a loop for the repeating the above instructions.
String manipulation instructions • The primitive string instructions are controlled with the CX, SI and DI registers. • CX used with rep to count number of locations remaining for the operation. • SI contains the source operand address of memory, with respect to DS. • DI contains the destination operand address of memory, with respect to ES. • Direction flag DF=0, means operation with increasing memory locations, ‘1’ means decreasing.
MOVS • This instruction takes one byte/word pointed to by SI register and moves it to the location pointed to by the DI register. • It automatically increments/decrements SI and DI register depending on the value of direction flag. • It automatically decrements CX register.
Example program .model small .data Str1 db “college of computer” Str2 db 30 dup(‘$’) .code Start: Movax,@data Mov ds, ax Moves, ax Lea si, str1 Lea di, str2 Mov cx,19 Cld Rep movsb Mov ah,9 Lea dx,str2 Int 21h Mov ax, 4c00h Int 21h End start end
STOS • Filling blocks of memory with the same byte/word. • STOSB/STOSW stores the contents of AL/AX in the memory location addressed by the DI register. Mov DI, offset buf Mov cx,256 Mov al,0 Cld Rep stosb
LODS • The reverse of STOS, but rep is hardly ever used with LODs as it is unlikely that contents of one location are stored in AL/AX and then straight away move the contents of the next location into the same register. • It takes one byte/word from the location whose address is in SI register.
LODS Mov SI, 200 Mov DI, 300 Mov cx, 50 Cld nextLoc: Lodsb Add al, 20h Stosb Loop nextLoc
CMPS and REPZ • Both in combination are used to compare strings but stop when either: • Cx becomes 0; or • Mismatch of character is found
CMPS and REPZ Cld Mov cx, 50 Mov SI, 200 Mov DI, 300 Repzcmpsb Jnzdiff_strings Mov dl, ’Y’ Jmp Matched
SCAS and REPNZ • Scans memory pointed by DX for a byte/word held in AL/AX. • It stops, if: • CX becomes zero (all locations searched) • Required byte/word has been found.
SCAS and REPNZ cld mov di, 200 mov cx, 50 next: mov al, ‘x’ repnzscasb jnz next dec di mov al, ‘y’ ; replace each ‘x’ with ‘y’ stosb inc cx loop next
Assignment 3c • Use string instructions to print number of times (maximum 9) a substring “abc” is found in “abbabcnabdemabcdabcd” • Use scasb and cmpsb