Palindrome or Not (Assembly Language)

; Program to check if the given string is a palindrome or not

DATA SEGMENT ;INITIALIZE THE DATA SEGMENT
STR1 DB “NITIN” ;INITIALIZE THE STRING
STR2 DB 5 DUP(?) ;INITIALIZE AN EMPTY STRING OF SAME STRING TO DUPLICATE IT
PAL DB 0 ;INITIALIZE PAL AS A FLAG
COUNT DW 5 ;INITIALIZE COUNT EQUAL TO STRING LENGHT
DATA ENDS ;END OF DATA SEGMENT

CODE SEGMENT ;INITIALIZE CODE SEGMENT
START: ;START THE CODE
ASSUME CS:CODE,DS:DATA ;ASSUMPTION OF CODE AND DATA
MOV AX,DATA ;MOVE DATA INTO ACCUMULATOR AX REGISTER
MOV DS,AX ;MOVE DATA FROM ACCUMULATOR INTO DATA SEGMENT DS REGISTER
MOV ES,AX ;MOVE DATA FROM ACCUMULATOR INTO EXTRA SEGMENT EX REGISTER
MOV CX,COUNT ;MOVE COUNT INTO CX REGISTER AS CX REGISTER IS USED TO MAKE COUNT
LEA SI,STR1 ;LOAD EFFECTIVE ADDRESS OF STRING 1 INTO SOURCE INDEX
;SOURCE INDEX POINTS TO START OF THE SOURCE STRING
LEA DI,STR2 ;LOAD EFFECTIVE ADDRESS OF STRING 2 INTO DESTINATION INDEX
;DESTINATION POINTS TO START OF THE DESTINATION STRING
;FOR STRING OPERATION WE MAKE USE OF SOURCE INDEX (SI)
;AND DESTINATION INDEX (DI)
ADD DI,CX ;ADDING THE CONTENT OF CX REGISTER AND DESTINATION INDEX
;BY DOING SO WE SPECIFY THE LENGTH OF THE DESTINATION INDEX AS 5
;SINCE CX = COUNT WHERE COUNT = 05H DECLARED IN DATA SEGMENT
DEC DI ;DECREMENT DI REGISTER

;WE USE A LOOP TO COPY THE STRING FROM SOURCE INDEX INTO DESTINATION INDEX

BACK: CLD ;CLEAR DIRECTION FLAG TO USE INCREMENTING
LODSB ;COPIES A BYTE POINTED BY SOURCE INDEX INTO ACCUMULATOR AX
STD ;SET THE DIRECTION FLAG SO THAT SI WILL BE DECREMENTED FOR STRING OPERATION
STOSB ;STORE A BYTE POINTED FROM ACCUMULATOR INTO THE EXTRA SEGMENT POINTED BY
;DESTINATION INDEX
LOOP BACK ;LOOP BACK TO BACK LABEL

;AFTER COPYING THE STRINGS INTO SOURCE INDEX AND DESTINATION INDEX
;WE SHALL COMPARE IT WITH EACH OHTER IN THE LOOP NAMED AS OVER
;IF THE RESULT OF THE OPERATION IS ZERO THAT MEANS THAT THE STRING IS A PALINDROME

LEA SI,STR1 ;LOAD EFFECTIVE ADDRESS OF STRING 1 INTO SOURCE INDEX
;SOURCE INDEX POINTS TO START OF THE SOURCE STRING
;SI HAS STRING “NITIN”
LEA DI,STR2 ;LOAD EFFECTIVE ADDRESS OF STRING 2 INTO DESTINATION INDEX
;DESTINATION POINTS TO START OF THE DESTINATION STRING
;DI HAS STRING “NITIN”
MOV CX,COUNT ;MOVE COUNT INTO THE CX REGISTER
REPE CMPSB ;REPE – IT WILL REPEAT THE STRING UNTILL THE ZERO FLAG =0 AND CARRY FLAG =0
;CMPSB – COMPARE STRING BYTE TO CHECK IF THEY ARE SAME
JNZ OVER ;JUMP IF NOT ZERO TO OVER LABEL TO END THE LABEL
;THIS WILL MAKE THE PROGRAM TERMINATE IF THE RESULT OF THE OPERATION IS NOT ZERO
MOV PAL,01H ;SET PAL TO 01H
;THIS IS DONE SO THAT THE USER CAN DISPLAY ANY CUSTOMIZED MESSAGE
OVER: MOV AH,4CH ;END OF OVER LABEL
;END OF CODE
INT 21H ;END OF CODE
CODE ENDS ;END OF CODE SEGMENT
END START ;END OF START
;INPUT
; STR1 – “NITIN”

;OUTPUT
; STR1 – “NITIN”
; STR2 – “NITIN”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s