6A. Read and display two strings along with their lengths and equality [VTU 4th sem MP lab program]

vtu mp 6a

Q) Read two strings, store them in locations STR1 and STR2. Check whether they are equal or not and display appropriated messages. Also display the length of stored strings.

This program will ask the user for two strings and store them in STR1 and STR2 respectively. Display the length of both the strings separately and also display whether the strings are equal or not.

Algorithm:
Step 1: Read two strings from keyboard and store them in STR1 and STR2 (using read macro).
Step 2: STR+1 contains length of the first string (STR1)
             STR+2 contains length of the second string (STR2)
Step 3: Compare the length of two strings. If not equal, directly display message as “strings not equal”. Also print length of both strings using DISPLAY and CONVERT macros.
Step 4: If length of the Strings are equal, compare character by character of both STR1 and STR2. If all the characters are equal, display the message as “strings are equal”. Also display their length
Step 5: Read procedure reads the string using 0AH function number.
Step 6: print procedure prints the string using 09H function number.
Step 7: Convert procedure converts the ASCII to hexadecimal or to unpacked BC.
Step 8: Stop.

CODE:



Explanation:
  • .MODEL SMALL
  • DIS_MSG MACRO P1
  • MOV AH,09H
  • LEA DX,P1
  • INT 21H
  • ENDM
SMALL indicates one data and one code segment.
DIS_MSG is the macro to display the string message to screen using 09H service in AH register and on interrupt INT 21H. As discussed in the previous program. (Visit previous programs where in i have explained this macro in detail.)

  • READ_STRING MACRO P2
  • MOV AH,0AH
  • LEA DX,P2
  • INT 21H
  • ENDM
READ_STRING is the macro to read buffered string input from the keyboard.
DOS function number ‘0AH’ when moved to AH register. And when the interrupt 21H is called, the system will accept a buffered string input (until Carriage return is entered) and store it in a location whose offset address is loaded in DX register.
Same is done here.
‘0AH’ is moved to AH.
Location where the string is to be saved is P2. And its offset is loaded into DX.
Interrupt 21H is called to read a buffered input.

There are some rules for this ‘0AH’. When this is called,
  1. The maximum length of characters the buffer can hold is defined in the offset 0 of the storage location. That is P2 is holding the maximum characters buffer can hold.
  2. P2+1 will contain the length of the string/buffer. It will be initially 0. And as we enter characters, it is incremented automatically. This means P2+1 will be containing the total length of the buffer.
  3. The actual saving of characters including the last carriage return starts from the offset P2+2.

  • CONVERT MACRO
  • LOCAL NEXT1
  • CMP DL,0AH
  • JL NEXT1
  • ADD DL,07H
  •  NEXT1:
  • ADD DL,30H
  • MOV AH,02H
  • INT 21H
  • ENDM
CONVERT macro will convert a character into its ASCII equivalent.
LOCAL NEXT1 is written because, there are two NEXT1 labels used in this program.(one in macro and one in code segment) program should not confuse between two. So we define that the label NEXT1 accessed within the macro is the local macro’s label.

Conversion rules of character to ASCII is discussed in the Lab program 4A. And so its not explained here. Visit ‘Lab program 4A’ by clicking here.

This is all about CONVERT macro. Proceed.


  • DISPLAY MACRO P1
  • MOV DL,P1
  • AND DL,0F0H
  • SHR DL,CL
  • CONVERT
  • MOV DL,P1
  • AND DL,0FH
  • CONVERT
  • ENDM
This DISPLAY  macro is used to display the two digit number on the screen by calculating each digit into its ASCII using CONVERT macro which was defined above.
This macro in brief:
P1 is the two digit number to be displayed. Its moved into DL.
The first digit is obtained by ANDing the DL with 1111 0000 (that is F0H) and shifting bits to the right 4 times. And converting the number into ASCII using CONVERT macro. Then display it.

Again DL is loaded with P1.
The second digit is obtained by ANDing DL with 0000 1111 (that is 0FH) and calling CONVERT macro. And then displaying it.
This macro and CONVERT macro’s logic are found in the ‘Lab program 4A’.


  • .DATA
  • M1 DB 10,13,'ENTER THE FIRST STRING:$'
  • M2 DB 10,13,'ENTER THE SECOND STRING:$'
  • M3 DB 10,13,'LENGTH OF FIRST STRING:$'
  • M4 DB 10,13,'LENGTH OF SECOND STRING:$'
  • M5 DB 10,13,'STR1 IS EQUAL TO STR2$'
  • M6 DB 10,13,'STR1 IS NOT EQUAL TO STR2$'
  • STR1 DB 50
  •            DB 0
  •            DB 50 DUP(0)
  • STR2 DB 50
  •            DB 0
  •            DB 50 DUP(0)

Now this is the Data segment with appropriate messages stored.
Two locations to store strings STR1 and STR2 with the way suitable for buffered input service as told above.
STR1, which is a byte length location, will hold the data ‘50’ stating that the buffer can hold maximum of 50 characters.

STR1+1 is the location which is initialized with ‘0’ stating that the length of STR is initially ‘Zero’.
STR1+2 will be the actual offset where the string storing begins.

Same thing is done with STR2. Proceed.


  • .CODE
  • MOV AX,@DATA
  • MOV DS,AX
  • MOV CL,04H
This is the beginning of code segment and data segment is initialized here.
CL is loaded with 04H, stating that number of bit rotation is 4 times, during ASCII conversion using DISPLAY and CONVERT macros given above.(you can see the CX being used there as the parameter for SHR as the ‘number of rotation’, the second parameter)


  • DIS_MSG M1
  • READ_STRING STR1
  • DIS_MSG M3
  • DISPLAY STR1+1
  • DIS_MSG M2
  • READ_STRING STR2
  • DIS_MSG M4
  • DISPLAY STR2+1
M1 message is displayed using DIS_MSG macro with M1 as the parameter.
Now M1 is displayed on the screen.
READ_STRING is called for STR1. Buffered input is taken from the keyboard until ‘carriage return’ is pressed. Length of the string(STR+1) is automatically updated.

M3 message is now printed using DIS_MSG macro again.

Length of the first string is printed on the screen using DISPLAY macro with STR1+1 as the parameter. (Since STR1+1 holds the length of the string). Now length of string 1 is displayed on the screen.

Same procedures are done again for STR2 with appropriate parameters to the macros.

Now comes string comparing part (for equality checking):
  • MOV CH,STR1+1
  • MOV CL,STR2+1
  • CMP CH,CL
  • JNZ TERM
CH is loaded with length of STR1, that is contained in STR1+1.
CL is loaded with length of STR2, that is contained in STR2+1.
CX and CL are compared using CMP. If not equal, it means two strings are not equal (which is obvious). In that case, jump to label ‘TERM’ (JNZ TERM) and display appropriate message and terminate the program. If equal, then continue for character by character checking of both the strings.

  • MOV CH,00H
  • LEA SI,STR1
  • LEA DI,STR2
CH is simply loaded with 00H.
Now for character by character checking, we need two index variables for two strings. This is done with loading the offset addresses of both STR1 and STR2 to SI and DI registers respectively.

  • NEXT1:
  • MOV AL,[SI+2]
  • CMP AL,[DI+2]
  • JNE TERM
  • INC SI
  • INC DI
  • LOOP NEXT1
  • DIS_MSG M5
  • JMP STOP
NEXT1 is a label.
First characters of the STR1 (that is at the location SI+2) is loaded to AL register.(since we can not compare two memory using CMP).

It is then compared with the first character of STR2. (that is present at the location DI+2) using CMP instruction. If they are equal, zero flag is set to ‘0’. Otherwise set to ‘1’.
So if Zero flag is ‘not zero’ (JNZ TERM), we come to know that strings are not equal . Directly jump to label’ TERM’ and display appropriate message and terminate the program.

If they are equal, then increment both SI and DI by 1, to compare the next characters of both the strings. LOOP instruction is called to jump the control to the label ‘NEXT1’, to compare the next characters. If All the characters are compared and found to be equal, display M5 message using DIS_MSG macro and terminate the program by unconditionally jumping at label ‘STOP’.

  • TERM:
  • DIS_MSG M6
  • STOP:
  • MOV AH,4CH
  • INT 21H
  • END
TERM is the label accessed from the String characters mismatch condition from above.
So here we need to print M6 message. This is done using DIS_MSG macro.

STOP is a label to terminate the program. As usual, 4CH is moved to AH and on INT 21H, the program is terminated.
END is the end of the program.


OUTPUT:
ENTER THE FIRST STRING: SANDESH
LENGTH OF FIRST STRING: 7
ENTER THE SECOND STRING: CODESHUFFLE
LENGTH OF SECOND STRING: 11
STR1 IS NOT EQUAL TO STR2

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!