You are on page 1of 14

Experiment 5 - Strings

Theory:

● Source block has to be present in DS


● Destination block has to be present in ES
● The offset address of source string has to be given by SI
● The offset address of destination string has to be given by DI
● Count has to be given by CX register
● Directions has to be decided by the direction flag register either auto increment or
decrement

Instruction sets -

● MOV Moves data from R2R, R2M, etc
● LOOP Jump to the defined label until CX=0
● INC/DEC Increment/decrement content of register or memory by 1
● CMP Used to compare two provided byte/word
● JMP Used to jump to the provided address to proceed to the next instruction
● JZ Used to jump if zero flag ZF=1
● REP Used to repeat the given instruction till CX=0
● MOVSB/MOVSW Move string byte/word
● LEA Used to load the address of operand into the provided register
● CLD Clears the direction flag (string operations will increment the index registers (SI
and/or DI))
● REP Work as loop instruction Loop repeats until CX= 0

Q1

AIM: Write an ALP to move a block of string from one location to another
Location

ALGORITHM:

1. Assign the SI register with the offset address of the S 1 in the data segment
2. Assign the DI register with the offset address of the S 2 in the data segment
3. Assign the register CX with the length of the string
4. Clear the direction flag
5. Copy MOVSB) the content stored at the offset address of SI into the offset
address of DI
6. Decrement CX
7. Repeat steps 5 6 till CX becomes 0
8. Print the moved string
9. End

CODE:

SIMULATION:
VERIFICATION AND RESULT:
The input string is seen in 076a:0000 and the same string is copied in the expected
output address 076a:0010.

Q2

AIM: Write an ALP to reverse a given string

ALGORITHM:

1. Assign the SI register with the offset address of the S 1 in the data segment
2. Assign the DI register with the offset address of the S 2 in the data segment
3. Assign the register CX with the length of the string
4. Add register DI with CX and store in DI
5. Increment DI
6. Assign the memory block at the offset address of DI in the data segment with
7. Decrement DX
8. Set the direction flag to 1
9. Copy MOVSB) the content stored at the offset address of SI into the offset
address of DI
10. Decrement CX
11. Increment the SI register twice
12. Repeat steps 9 to 11 till CX becomes 0
13. Print the reversed string
14. End

CODE:
SIMULATION:
VERIFICATION AND RESULT:
Input string in 076a:0000 is ‘string’ and the reversed string in output address 076a:0010
is ‘gnirts’.

Q3

AIM: Write an ALP to compare two strings

ALGORITHM:

1. Assign the SI register with the offset address of the S 1 in the data segment
2. Assign the DI register with the offset address of the S 2 in the data segment
3. Assign the register CX with the length of the string
4. Add register DI with CX and store in DI
5. Set the direction flag to 0
6. Compare(CMPSB) the content stored at the offset address of SI and DI
7. Repeat step 6 until either the zero flag becomes 1 or CX becomes 0
8. Compare the register CX and 0000
9. If the Zero flag is set print ‘different’
10. If the Zero flag is not set print ‘same’
11. End

CODE:
MANUAL VERIFICATION:
Both strings are different Output will also be different

Q4

AIM: Write an ALP to find length of a string

ALGORITHM:
1. Assign the SI register with the offset address of the S 1 in the data segment
2. Assign the register CX with the 0000 H
3. Decrement SI
4. Increment SI and CX
5. Copy MOV) the content stored at the offset address of SI into the register DL
6. Compare DL and
7. If not equal repeat steps 5 to 7
8. Decrement CX ..(CX Length of string)
9. End

CODE:
SIMULATION:
VERIFICATION AND RESULT:
Input string is ‘string’ which has 6 letters. Output is stored in SI register which shows ‘6’

You might also like