loading...

To check if the string is palindrome using assembly language

The following code helps us to determine if the string entered is palindrome.

 
print macro m
mov ah,09h
lea dx,m
int 21h
endm
DATA SEGMENT
str db 80 dup(0)
revstr db 80 dup(0)
cr equ 0dh
m1 db 10,13,”enter the string $”
m2 db 10,13,”string is palindrome $”
m3 db 10,13,”string is not palindrome $”
data ends
assume cs:code,ds:data
CODE SEGMENT
Start:             mov ax,data
                   mov ds,ax
                   print m1
                   lea si,str
rdchar1:           mov ah,01h
                   int 21h
                   mov [si],al
                   inc si
                   cmp al,cr
                   jne rdchar1
                   lea si,str
                   mov bx,00h
nxtchar2:          mov al,[si]
                   cmp al,cr
                   je skip1
                   mov [si],al
                   inc si
                   inc bx
                   jmp nxtchar2
skip1:             lea si,str
                   add si,bx
                   lea di, revstr
                   mov cx,bx
nxtchar3:          dec si
                   mov al,[si]
                   mov [di],al
                   inc di
                   loop nxtchar3
                   mov al,’$’
                   mov [di],al
                   lea si,str
                   lea di, revstr
                   mov cx,bx
Up4:               mov al,[si]
                   cmp al,[di]
                   jne dn
                   inc si
                   inc di
                   loop up4
                   print m2
                   jmp exit
dn:                print m3
exit:              mov ah,4ch
                  int 21h
                  code ends
                  end start
 

The output of the above code is –

 Output:
-g= 0000 0070
Enter the string aba
String is palindrome
Ax=4C61 Bx=0003 Cx=0000 Dx=00B4
-g=0000 0070 
Enter the string HELLO
String is not palindrome
Ax=4C68 Bx=0005 Cx=0005 Dx=00CC
 

 

0 2

Leave a Reply

Your email address will not be published.