5A. Reverse a string and check for palindrome. [VTU 4th sem MP lab program]

vtu mp 5a

Q)  Reverse a given string and check whether it is a palindrome or not.

This program will read a string from the keyboard and reverse it, and display it back to the screen. And it will also check whether the string is palindrome or not.

Algorithm:
Step 1: Read a string (STR) from keyboard using ‘01HDOS service.
Step 2: SI points to the initial memory location and store the string in [SI].
Step 3: If [SI] is entered key then go to Step 4 to display the reversed string.
Step 4: Decrease SI by 1 location and move the content of SI to DI register. DI is assumed to point at the reverse string.
Step 5: Compare the characters which is stored in SI and DI. If equal, display (“Palindrome”) using ‘09H’ service.
Else display (“Not a Palindrome”) using 09H service.
Step 6: Stop.

CODE:


 Explanation:
  • .MODEL SMALL
  • PRINT MACRO MSG
  • PUSH DX
  • MOV AH,09H
  • LEA DX,MSG
  • INT 21H
  • POP DX
  • ENDM

SMALL indicates one data and one code segment.
Next is PRINT macro which takes a parameter MSG. This macro will print the MSG on the screen using 09H service. Before printing, DX value is pushed to the stack just to prevent data loss of DX in any means. After displaying, data is popped back again to DX.
For displaying, 09H is moved to AH and Offset of MSG is loaded to DX using LEA instruction. Interrupt is called to take an input string.
ENDM is the end of the macro.

  • .DATA
  • MSG1 DB 10,13,"ENTER A STRING:$"
  • MSG2 DB 10,13,"THE REVERSED STRING IS:$"
  • MSG3 DB 10,13,"STRING IS A PALINDROME$"
  • MSG4 DB 10,13,"STRING IS NOT A PALINDROME$"
  • STR DB 100 DUP (?)
Data segment with 4 messages shown above for User interface. Terminated with ‘$’ character indicating the end of the strings. 10,13 will display a new line character before displaying the messages.
STR is a location to store the message string which has 100 sub locations, each of byte length and initially initialized to nothing/null [using DUP (?)]


  • .CODE
  • MOV AX,@DATA
  • MOV DS,AX
  • PRINT MSG1
  • LEA SI,STR
Code segment starts and data segment is initialized in the first three lines.
MSG1 is displayed using PRINT macro with MSG1 as the parameter.
Offset address of STR is loaded into SI. Now SI is pointing to the first location in STR.

  • UP1:  
  • MOV AH,01H
  • INT 21H
  • CMP AL,0DH
  • JZ D1
  • MOV [SI],AL
  • INC SI
  • JMP UP1

UP1 is the label.
Character is read from the  keyboard using ‘01H’ service. The entered character will be stored in AL. So contents of AL is compared with ‘Enter’ key.(whose ASCII value is 0DH) using CMP instruction. If they are equal, zero flag is set to ‘0’. If Zero flag is 0, control is jumped to label D1 for further processing. (JZ D1).
If the entered character is not ‘Enter’ key, JZ will be false and then, we have to store the character to the location being pointed by SI. (MOV [SI],AL). SI is increased to store the next character in the next memory location. An unconditional jump is performed to label UP1 to read the next character.

  • D1:
  • DEC SI
  • MOV DI,SI
  • PRINT MSG2
D1 is a label which is accessed from above code when entered character is ‘Enter’ key.
SI will be now pointing to the location next to the location where the last character that we entered is stored.
So we decrease SI by 1 to bring SI back to the location where last character is stored. And it is copied to DI register for later purposes(comparison).
Now MSG2 is printed using the PRINT macro with MSG2 as the parameter.


  • DISP:
  • MOV DL,[SI]
  • MOV AH,02H
  • INT 21H
  • DEC SI
  • CMP SI,OFFSET STR
  • JGE DISP
  • INC SI

DISP is the label.
SI is presently at the last character in the STR string. So we will print all the characters from last to first by decreasing the SI until SI becomes equal to offset address of STR.(the first character). So contents at the location SI is moved to DL and 02H is called with Interrupt 21H, to display the contents of DL. SI is decremented by 1 to display the next character of the reversed string.
SI is then compared to OFFSET STR, to check whether SI has come till the first character of STR. If SI is greater than or equal to Offset of STR, we display the next character by jumping back to DISP label.
When SI becomes less than Offset of STR, it means the SI is pointing to the location back to the first address of STR. So we need to bring the SI back to STR’s base address. INC SI is therefore used.

Now we have displayed the string in the revered order.


Palindrome checking logic:
Now we will perform a logic here to check for palindrome.
SI, as we know is pointing to first character of the array and DI is pointing to Last character. We will check for equality of character present at SI and DI locations. If equal, we will increase SI and Decrease DI by value 1. And check for the equality of next character. If they are not equal, we will directly display the “Not palindrome” message (MSG4) and exit the code.
This comparison will continue until SI is smaller than or equal to DI. If all the characters match till then, Then we can say that string is palindrome by displaying MSG3 and then exit the code.

Now resume to code,
  • NEXT:
  • MOV DL,[SI]
  • CMP DL,[DI]
  • JZ SK
  • PRINT MSG4
  • JMP LAST
  • SK:
  • INC SI
  • DEC DI
  • CMP SI,DI
  • JLE NEXT
  • PRINT MSG3
Content of SI location is copied to DL register and character at DI location is compared to DL using CMP instruction. If result is equal, zero flag will be set to ‘0’. Otherwise set to ‘1’.
If Zero flag is ‘0’ (JZ SK), then we can continue for checking the equality of next characters by jumping to label SK. Otherwise, just display the failure message (MSG4) using PRINT macro. And unconditionally jump to the label LAST, to terminate the program.

SK is the label where you proceed to check the next character’s, equality. So increase SI by 1 and decrease DI by 1. And compare whether SI is less than or equal to DI (JLE NEXT). And jump to label NEXT if the comparison results in true.
If SI is greater than DI, it means we checked all character’s equality. Now we get know that string is a palindrome. Display the same using the message (MSG3) with the help of macro. And terminate the program.

  • LAST:
  • MOV AH,4CH
  • INT 21H
  • END
LAST is the label where program termination code is written. 4CH is moved to AH and on Interrupt 21H, program is terminated.
END means the end of the program.


OUTPUT:
ENTER A STRING: CODESHUFFLE
THE REVERSED STRING IS: ELFFUHSEDOC
STRING IS NOT A PALINDROME

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!