Professional Documents
Culture Documents
Writeup 2
Writeup 2
Theory :
String is one data type supported b 80386. String is number of data elements stored in contiguous
memory locations . ( can be thought as single dimensional array) .
The source string has to be in Data segment ( DS) and destination string has to be in extra
segment ( ES).
MOVS : This instruction copies the memory operand ( byte ,or word or Double word) specified
by DS:SI (or DS:ESI) to the memory location specified by ES:DI (or ES:EDI). After that, SI and
DI (or ESI and EDI) will be incremented or decremented ( by 1,2, or 4 based on whether it is
byte/ word/ double word), depending on the state of the direction flag.
If direction flag is =0 , then ( SI & DI) are decremented (direction flag can be cleared by
instruction : CLD )
If direction flag is =1 , then ( SI & DI) are incremented ( direction flag can be set by instruction
STD )
STOSB/ STOSW/ STOSD (store string ): This instruction will write the contents of the
accumulator (AL, AX or EAX) to the memory location specified by ES:DI (ES:EDI for 32 bit
operations). After that, SI and DI (or ESI and EDI) will be incremented or decremented ( by 1,2,
or 4 based on whether it is byte/ word/ double word), depending on the state of the direction flag.
LODSB / LODSW / LODSD ( load string) : This instruction will load the BYTE, WORD, or
DWORD at DS:SI (or DS:ESI) into the accumulator. After that, SI and DI (or ESI and EDI) will
be incremented or decremented by 1/2/4.
SCASB / SCASW / SCASD (Scan string ) : This instruction compares the value in the
accumulator (AL, AX or EAX) with the contents of the memory location specified by ES:DI (or
ES:EDI). The flags are set according to the results of the comparison . After that, DI or EDI
will be incremented or decremented by 1/2/4.
CMPSB/ CMPSW/ CMPSD ( compare strings) This instruction subtracts the memory location
specified by DS:SI (or DS:ESI) from the operand specified by ES:DI (or ES:EDI), setting the
flags and discarding the result,( same operation as that of CMP instruction , except that cmps is
for strings)). After that, SI and DI (or ESI and EDI) will be incremented or decremented by
1/2/4.
Above instructions can do operation on only one data elements of strings at a time. To repeat the
same operation on all elements of string , a REP prefix can be used . For this the register CX /
ECX is used as counter .The contents of the CX register (ECX for 32 bit operation) will be
decremented and the string instruction repeated until CX goes to 0.
REP MOVSB copy number of bytes pointed by DS:SI to ES:DI . After this ( SI, & DI are
incremented or decremented based on Direction flag), CX/ECX is decremented , & if CX/ECX is
not zero , next byte is copied
Algorithm: