Operation on Strings in Assembly Language
Algorithm for Operation on Strings
Step I : Initialize the data and stack memory.
Step II : Using Macro display Menu.
1. Accept
2. Length
3. Reverse
4. Palindrome
5. Exit.
Step III : Accept choice from user using INT 21H, function 01H.
Step IV : IS choice = 1 jump to step XI else goto step V.
Step V : IS choice = 2 jump to step XIV else goto step VI.
Step VI : IS choice = 3 jump to step XVII else goto step VII.
Step VII : IS choice = 4 jump to step XX else goto step VIII.
Step VIII : IS choice = 5 jump to step XXIII else goto step IX.
Step IX : Display Wrong choice.
Step X : Jump to step II.
Step XI : Call procedure accept.
Step XII : Accept string using INT 21H, function 0AH.
Step XIII : Return to main program and goto step II.
Step XIV : Call procedure length.
Step XV : Calculate the length of string and display it using INT 21H, function 02H.
Step XVI : Return back to main program and jump to step II.
Step XVII : Call procedure reverse.
Step XVIII : Reverse the string and display it.
Step XIX : Return back to main program and jump to step II.
Step XX : Call procedure palindrome.
Step XXI : Check if string is palindrome. If yes display string is palindrome else string is not a palindrome.
Step XXII : Return back to main program and jump to step II.
Step XXIII : Terminate the program and stop.
TITLE STRING OPERATIONS MESS MACRO MSG ; DEFINITION OF MACRO MESS MOV AH, 09H LEA DX, MSG INT 21H ENDM .MODEL SMALL .STACK 100H .DATA STR1 DB 25 , ? , 25 DUP('$') STR3 DB 25 , ? , 25 DUP('$') MSG1 DB 0AH, 0DH, 'MENU $' MSG21 DB 0AH, 0DH, '1.ACCEPT $' MSG22 DB 0AH, 0DH, '2.LENGTH $' MSG23 DB 0AH, 0DH, '3.REVERSE $' MSG24 DB 0AH, 0DH, '4.PALINDROME $' MSG25 DB 0AH, 0DH, '5.EXIT $' MSG3 DB 0AH, 0DH, 'ENTER YOUR CHOICE : $' MSG4 DB 0AH, 0DH, 'WRONG CHOICE $' MSG5 DB 0AH, 0DH, 'ENTER THE STRING : $' MSG6 DB 0AH, 0DH, 'STRING IS : $' MSG7 DB 0AH, 0DH, 'LENGTH IS : $' MSG8 DB 0AH, 0DH, 'THE STRING IS A PALINDROME $' MSG9 DB 0AH, 0DH, 'THE STRING IS NOT A PALINDROME $' .CODE mov ax, @data ; Intialize data and extra segment mov ds, ax mov es, ax ak : mess msg1 ; display menu mess msg21 mess msg22 mess msg23 mess msg24 mess msg25 mess msg3 ; accept choice mov ah, 01h int 21h mov bl, al ; Choice BL cmp bl, 31h ; if choice=1 je acc ; Accept string cmp bl, 32h ; if choice=2 je len ; Find lenth ; of string cmp bl, 33h ; if choice=3 je rev ; Reverse string cmp bl, 34h ; if choice=4 je pal ; Check if string is ; palindrome cmp bl, 35h ; if choice=5 je endd ; exit mess msg4 ; Wrong Choice jmp ak acc : call accept jmp ak len : call lent jmp ak rev : call reverse jmp ak pal: call pall jmp ak endd: mov ah, 4ch int 21h ; accept procedure accept proc near mess msg5 mov ah, 0ah ; Accept String lea dx, str1 int 21h RET accept endp ; length procedure lent proc near mess msg7 mov dl, str1+1 ; Dl contains length of String or dl, 30h mov ah, 02h ; Display Length int 21h ret lent endp ; reverse procedure reverse proc near mess msg6 mov ch, 00h mov cl, str1+1 ; Cl has length of string sub cl, 01h lea si, str1+2 ; DESTINATION STRING lea di, str1+2 ; DESTINATION STRING repz movsb ; COPY TO TRAVERSE TILL END OF FIRST STR mov cl, str1+1 lea di, str3+2 ; DESTINATION STRING loop1: mov dx, [si] ; dx contains rightmost character mov ah, 02h int 21h ; display character mov [di], dx ; copy character to destination dec si inc di dec cl cmp cl, 00h jne loop1 ret reverse endp ; palindrome procedure pall proc near mess msg6 mov ah, 09h lea dx, str1+2 ; str1 contains original string int 21h call reverse ; str3 has reversed string lea di, str3+2 mov ah, 00h mov dh, 00h lea si , str1+2 mov cl, str1+1 ; CL contains Length of string loop 2 : mov al, byte ptr[si] mov bl, byte ptr[di] dec cl ; Decrement count cmp cl, 00h je loopa cmp al, bl ; Compare characters je loop3 ; if same goto loop3 loopa : cmp cl, 00h ; if checked all characters je loop4 mess msg9 ; the strings are not same jmp loop5 loop4 : mess msg8 ; the strings are same loop5: ret loop3 : inc si inc di jmp loop2 ; now check next character pall endp end
How to Run this Program
For Running this program you should have installed Tasm on you computer . If you have not installed Tasm yet please install from Here .
Leave a Reply