Custom Search

Wednesday, 22 October 2014

Write a Program in assembly language to Reverse a string and print whether it is Palindrome or Not. (8086 program)


Following program will check the string given at compile time as input in assembly language; and will print its reversed string and check whether the given string is palindrome or not.
Here the string is google; which is not palindrome.


Program:

data segment
       stri db "google$"
       str1len equ ($-stri)
       str2 db " reversr string $"
       str3 db " not palidrom$"
       str4 db " palidrome$"
data ends

code segment
       assume ds:data,cs:code,es:data

start:
       mov ax,data
       mov ds,ax
       mov es,ax
       mov ah,09h
       lea dx,stri
       int 21h
       mov ah,09h
       lea dx,str2
       int 21h
       lea bx,stri
       mov si,bx
       add si,str1len
       dec si
       dec si

   b: cmp bx,si
       jae a
       mov al,[bx]
       mov ah,[si]
       mov [si],al
       mov [bx],ah
       cmp al,ah
       jne e
       mov cx,01h

   e: inc bx
       dec si
       jmp b

   a: mov ah,09h
       lea dx,stri
       int 21h
       cmp cx,01h
       je c
       mov ah,09h
       lea dx,str3
       int 21h
       jmp skip

   c: mov ah,09h
       lea dx,str4
       int 21h

skip: code ends

end start



Output of the above 8086 program will be like this:


google
Reverse Str:elgoog
Not palindrome

No comments:

Post a Comment

Laptops