11-Write an ALP in 8086 for following operations on string,1- Accept a String,2- Calculate the length of a String,3-Reverse a String, 4- Check the String for a Palindrome.

Write an ALP in 8086 for following operations on string
1) Accept a String.
         2) Calculate the length of a String.
         3) Reverse a String.
4) Check the String for a Palindrome.


.model small
disp macro a
mov ah,09h
lea dx,a
int 21h



endm

.data
   msg1 db 10,13,10,13,"1.Accept a string"
             db 10,13,"2.Length of a string"
             db 10,13,"3.Reverse"
             db 10,13,"4.Palindrome."
             db 10,13,"5.Exit"
             db 10,13,"Enter your choice:$"

msg2 db 10,13,"Invalid choice.Re-enter choice$"
    msg3 db 10,13,10,13,"The string is:$"
    msg4 db 10,13,10,13,"Enter the string:$"
    msg5 db 10,13,10,13,"The length of the string is:$"
msg6 db 10,13,10,13,"It is a palindrome.$"
    msg7 db 10,13,10,13,"It is not a palindrome.$"
msg8 db 10,13,10,13,"The reverse string is:$"

str1 db 20,?,20 dup("$") ;maxlength,actuallength,bufferspace
str2 db 20,?,20 dup("$") ;maxlength,actuallength,bufferspace

result dw 0000h



.code

main:
mov ax,@data
mov ds,ax

menu:
disp msg1
mov ah,01h
int 21h
cmp al,'1'
je accept
cmp al,'2'
je len
cmp al,'3'
je reverse
cmp al,'4'
je palindrome
cmp al,'5'                    
je exit
disp msg2
jmp menu

exit:
mov ah,4ch
int 21h

accept:
disp msg4
mov ah,0AH
lea dx,str1
int 21h
disp msg3
disp str1[2]

jmp menu


len:
disp msg5
mov bl,str1[1]
mov bh,00
mov result,bx
call print

jmp menu


reverse:
mov cl,str1[1]
mov str2[1],cl
lea SI,str1[2]
lea DI,str2[2]
mov ch,00h
add SI,cx
dec SI

up1:
mov bl,[SI]
mov [DI],bl
dec SI
inc DI
dec cl
jnz up1
disp msg8
disp str2[2]

jmp menu


palindrome:


mov cl,str1[1]
lea SI,str1[2]
lea DI,str2[2]

up2:
                        mov bl,[SI]
                        cmp [DI],bl
                        je down1
disp msg7

jmp menu

down1:
inc SI
                        inc DI
dec cl
jnz up2
disp msg6

jmp menu



print proc near

        mov bx,result
          shr bx,0Ch
          cmp bl,09h
          jle l1
          add bl,07h

l1:
        add bl,30h
            mov dl,bl
            mov ah,02h
            int 21h

        mov bx,result
          shr bx,08h
          and bl,0Fh
          cmp bl,09h
          jle l2
          add bl,07h
       
l2:
            add bl,30h
            mov dl,bl
            mov ah,02h
            int 21h

        mov bx,result
          shr bx,04h
          and bl,0Fh
          cmp bl,09h
          jle l3
          add bl,07h

l3:
add bl,30h
            mov dl,bl
            mov ah,02h
                int 21h

        mov bx,result
        and bl,0Fh
          cmp bl,09h
          jle l4
          add bl,07h

l4:
            add bl,30h
            mov dl,bl
            mov ah,02h
            int 21h

ret
print endp

end main

end

---------------------------------------------OUTPUT-----------------------------------------------


D:\tasm\BIN>tlink str.obj
Turbo Link  Version 7.1.30.1. Copyright (c) 1987, 1996 Borland International
Warning: No stack

D:\tasm\BIN>str


1.Accept a string
2.Length of a string
3.Reverse
4.Palindrome.
5.Exit
Enter your choice:1

Enter the string:MADAM

The string is:MADAM

1.Accept a string
2.Length of a string
3.Reverse
4.Palindrome.
5.Exit
Enter your choice:2

The length of the string is:0005

1.Accept a string
2.Length of a string
3.Reverse
4.Palindrome.
5.Exit
Enter your choice:3

The reverse string is:MADAM

1.Accept a string
2.Length of a string
3.Reverse
4.Palindrome.
5.Exit
Enter your choice:4

It is a palindrome.

1.Accept a string
2.Length of a string
3.Reverse
4.Palindrome.
5.Exit
Enter your choice:5
D:\tasm\BIN>


Previous
Next Post »

Disqus Shortname

Comments system