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.
1 2 |
TITLE STRING OPERATIONS <br/> MESS MACRO MSG ; DEFINITION OF MACRO MESS <br/> MOV AH, 09H <br/> LEA DX, MSG <br/> INT 21H <br/> ENDM <br/> .MODEL SMALL <br/> .STACK 100H <br/> .DATA <br/> STR1 DB 25 , ? , 25 DUP('$') <br/> STR3 DB 25 , ? , 25 DUP('$') <br/> MSG1 DB 0AH, 0DH, 'MENU $' <br/> MSG21 DB 0AH, 0DH, '1.ACCEPT $' <br/> MSG22 DB 0AH, 0DH, '2.LENGTH $' <br/> MSG23 DB 0AH, 0DH, '3.REVERSE $' <br/> MSG24 DB 0AH, 0DH, '4.PALINDROME $' <br/> MSG25 DB 0AH, 0DH, '5.EXIT $' <br/> MSG3 DB 0AH, 0DH, 'ENTER YOUR CHOICE : $' <br/> MSG4 DB 0AH, 0DH, 'WRONG CHOICE $' <br/> MSG5 DB 0AH, 0DH, 'ENTER THE STRING : $' <br/> MSG6 DB 0AH, 0DH, 'STRING IS : $' <br/> MSG7 DB 0AH, 0DH, 'LENGTH IS : $' <br/> MSG8 DB 0AH, 0DH, 'THE STRING IS A PALINDROME $' <br/> MSG9 DB 0AH, 0DH, 'THE STRING IS NOT A PALINDROME $' <br/> .CODE <br/> mov ax, @data ; Intialize data and extra segment <br/> mov ds, ax <br/> mov es, ax <br/> ak : mess msg1 ; display menu <br/> mess msg21 <br/> mess msg22 <br/> mess msg23 <br/> mess msg24 <br/> mess msg25 <br/> mess msg3 ; accept choice <br/> mov ah, 01h <br/> int 21h <br/> mov bl, al ; Choice BL <br/> cmp bl, 31h ; if choice=1 <br/> je acc ; Accept string <br/> cmp bl, 32h ; if choice=2 <br/> je len ; Find lenth <br/> ; of string <br/> cmp bl, 33h ; if choice=3 <br/> je rev ; Reverse string <br/> cmp bl, 34h ; if choice=4 <br/> je pal ; Check if string is <br/> ; palindrome <br/> cmp bl, 35h ; if choice=5 <br/> je endd ; exit <br/> mess msg4 ; Wrong Choice <br/> jmp ak <br/> acc : call accept <br/> jmp ak <br/> len : call lent <br/> jmp ak <br/> rev : call reverse <br/> jmp ak <br/> pal: call pall <br/> jmp ak <br/> endd: mov ah, 4ch <br/> int 21h <br/> ; accept procedure <br/> accept proc near <br/> mess msg5 <br/> mov ah, 0ah ; Accept String <br/> lea dx, str1 <br/> int 21h <br/> RET <br/> accept endp <br/> ; length procedure <br/> lent proc near <br/> mess msg7 <br/> mov dl, str1+1 ; Dl contains length of String <br/> or dl, 30h <br/> mov ah, 02h ; Display Length <br/> int 21h <br/> ret <br/> lent endp <br/> ; reverse procedure <br/> reverse proc near <br/> mess msg6 <br/> mov ch, 00h <br/> mov cl, str1+1 ; Cl has length of string <br/> sub cl, 01h <br/> lea si, str1+2 ; DESTINATION STRING <br/> lea di, str1+2 ; DESTINATION STRING <br/> repz movsb ; COPY TO TRAVERSE TILL END OF FIRST STR <br/> mov cl, str1+1 <br/> lea di, str3+2 ; DESTINATION STRING <br/> loop1: mov dx, [si] ; dx contains rightmost character <br/> mov ah, 02h <br/> int 21h ; display character <br/> mov [di], dx ; copy character to destination <br/> dec si <br/> inc di <br/> dec cl <br/> cmp cl, 00h <br/> jne loop1 <br/> ret <br/> reverse endp <br/> ; palindrome procedure <br/> pall proc near <br/> mess msg6 <br/> mov ah, 09h <br/> lea dx, str1+2 ; str1 contains original string <br/> int 21h <br/> call reverse ; str3 has reversed string <br/> lea di, str3+2 <br/> mov ah, 00h <br/> mov dh, 00h <br/> lea si , str1+2 <br/> mov cl, str1+1 ; CL contains Length of string <br/> loop 2 : mov al, byte ptr[si] <br/> mov bl, byte ptr[di] <br/> dec cl ; Decrement count <br/> cmp cl, 00h <br/> je loopa <br/> cmp al, bl ; Compare characters <br/> je loop3 ; if same goto loop3 <br/> loopa : cmp cl, 00h ; if checked all characters <br/> je loop4 <br/> mess msg9 ; the strings are not same <br/> jmp loop5 <br/> loop4 : mess msg8 ; the strings are same <br/> loop5: ret <br/> loop3 : inc si <br/> inc di <br/> jmp loop2 ; now check next character <br/> pall endp <br/> 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 .
Source projectgeek.com