Upload
ramphani-nunna
View
221
Download
0
Embed Size (px)
Citation preview
8/7/2019 9.STRING COMPARISION
1/7
STRING COMPARISION
Aim:
To write an ALP to compare two strings and display whether two strings are
equal or different
Apparatus:
y MASM softwarey Personal computer
Register sused: AX,CX,DX,DS,ES,SI,DI
Portsused: None
Problemanalysis:
Let us assume that string1 and string2 are available in the data
segment area. Similarly the prompt1 and prompt2 are also available in data
segment area. These prompts are useful to display the message after processing
whether the strings are same or different.
In the comparison, one character at a time from string1 is read
and compared with string2.The operation has to be repeated till all the characters
are compared. Counter may be used to check whether all the characters are
compared. After comparison, if both the strings are same then invoke routine to
display the message available in prompt1.Otherwise display the message availble
in prompt2.Even if the one character is different immediately display the message
available in prompt2.
8/7/2019 9.STRING COMPARISION
2/7
Flow chart:
NO
yes
START
Initialize the data and extra segmet registers
Load offset address of string1 in SI and string2 In DI
clear the direction flag
Intialize the counter with no. of characters
Load the offset address of prompt2 and display
the message available in prompt2
Invoke the interrupt routine to come to DOS
prompt
STOP
Load the offset address
of prompt1 & display th
message available at
prompt1
Is
Str1!=str2
8/7/2019 9.STRING COMPARISION
3/7
Algorithm:
1. Start2. Initialize the data and extra segment registers3. Load the offset address of string1 and string24. Clear the direction flag5. Initialize the counter with number of characters6. Compare the string and repeat,if the string is not equal goto step 87. If the string is same,load the offset address of prompt1 and display
the message available in prompt
8. Load the offset address of prompt2 and display the messageavailable in prompt
9. Invoke the interrupt routine to come to DOS prompt10.End of the program
Program:
ASSUME CS:code,DS:data,ES:data
Data segment
Str1 DB 0AH,ODH,BHARAT ISMAHAN HAI,0AH,0DH,$
Str2 DB 0AH,ODH,BHARAT ISMAHAN HAI,0AH,0DH,$
Prompt1 DB 0AH,ODH,THE STRINGS ARE EQUAL,0AH,0DH,$
Prompt2 DB 0AH,ODH,THE STRINGS ARE EQUAL,0AH,0DH,$
Data ends
8/7/2019 9.STRING COMPARISION
4/7
Code segment
Start:Mov ax,data
Mov ds,ax
Mov es,ax
Mov ss,ax
Mov cx,0015h
CLD
Repeat:CMPS str1,str
JNE mismatch
LEA dx,prompt1
Mov ah,09h
INT 21h
JMP over
Mismatch:LEA dx,prompt2
Mov ah,09h
INT 21h
Over: mov ah,4ch
INT 21h
Code ends
End start.
8/7/2019 9.STRING COMPARISION
5/7
Procedure:
Preparing an assembly language program for execution:
1)with the help of an editor,TYPE the program,with an extension.asm
Ex: pragati.asm
2) use an assembler (ex:MASM.exe)to create.obj file, without any error
If any error occur during the assembly , then use the editor to re-edit the user
program and correct the syntax errors and other specified errors.
Once there is no error in the assembly language program , the OBJ file is created
after assembly.
3) use the linker to create a.EXE file from the OBJ file. If there is no error in linking,
then go to next step. if there is any error go back to the step 1and make necessary
corrections in the .ASM
File and then follow the step 2&3
4)Execute the created EXE file, check the output. If the expected result is
obtained, then it means the logic of the program is correct
If the expected result is not obtained , then make use of DEBUG routine to check
the logic of the user program may be using G command
Codetable:
Effective
address
Hex code Mnemonics Operands Comments
0000 B8430B MOV AX,0B43 Store data into ax
8/7/2019 9.STRING COMPARISION
6/7
register
0003 8ED8 MOV DS,AX Store AX value into D
register
0005 8EC0 MOV ES,AX Store AX value into E
register0007 B92000 MOV CX,0020 Store 0020 into CX
register
000A FC CLD Clear direction flag
000B 8D360000 LEA SI,[0000] Load the effective
address of str1 into S
register
000F 8D3E1500 LEA DI,[0015] Load the effective
address of str2 into D
register0013 F3 REPZ Repeat process until
count is zero
0014 A6 CMPSB Compare str1 with st
0015 750A JNZ 0021 Jump if not zero
0017 BA2900 MOV DX,0029 Set offset address of
prompt1 to DX regist
001A B409 MOV AH,09 Display prompt1 mes
001C CD21 INT 21 Interrupt
001E EB08 JMP 0028 Jump to over
0021 BA3A00 MOV DX,003A Store offset address o
prompt2
0024 B409 MOV AH,09 Display prompt2 mes
0026 CD21 INT 21 Interrupt
0028 B44C MOV AH,4C Move to DOS prompt
002A CD21 INT 21 Interrupt
Result:
Input: BHARAT ISMAHAN HAI
BHARAT ISMAHAN HAI
8/7/2019 9.STRING COMPARISION
7/7
Output: the given strings are equal