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 ‘01H’ DOS 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 (?)
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
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
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
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
END means the end of the program.
OUTPUT:
ENTER A STRING: CODESHUFFLE
THE REVERSED STRING IS: ELFFUHSEDOC
STRING IS NOT A PALINDROME
Share your views about this article!