Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Experiment 2

Title Length of a string


Problem statement Write an X86/64 ALP to accept a string and to display its length

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) .

For string operation , the default registers used are

➢ The SI or ESI (source index) register


➢ The DI or EDI (destination index) register
➢ The CX (count) register
➢ The AL or AX or EAX register and
➢ The direction flag in FLAGS register

The source string has to be in Data segment ( DS) and destination string has to be in extra
segment ( ES).

80386 has a group of instructions , which can support operations on strings.

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 )

Eg MOVSB ( move byte ) , MOVSW ( move word) , MOVD ( Double word)

INSB/ INSW/INSD : read data from input device

OUTSB/ OUTW/ OUTD : write data to output device

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

We can also combine some condition along with REP

REPE / REPZ Repeat while CX (ECS) is not 0 and ZF is set

REPNE / REPNZ Repeat while CX (ECX) is not 0 and ZF is clear

Algorithm:

1. Reserve memory locations to store string given by user


2. Display message to the user to enter a string
3. Accept string inputted by user
4. system call returns size/ length of string
5. convert the length to decimal format
6. display the length

Program source code

Screen shot of output

You might also like