Menu driven program for string operations(Accept String,Display String,Length,Reverse, Palindrome ) in TASM

On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
Objective:
Write a menu driven  assembly language program to accept ,display the string, find the length of string, reverse the string, check the string is palindrome or not and scan the string to check whether a accepted letter is present or not
Prerequisite:
TASM assembler
Description:
There are several string instructions, used to move large blocks of data or stings and perform operations on strings.
·         Memory string source  - DS:SI
·         Memory string destination  - ES:DI
·         String can be processed in forward and reverse direction.
·         If DF=1 auto decrement for SI and DI
·         If DF=0 auto increment for SI and DI
Algorithm:
1.      Start
2.      Initialize data segment through AX register in the DS register.
3.      If choice =1 then goto step no.4 (accept the string)else goto the step no.14
4.      Display the message “Enter the string”
5.      Initialize the SI with 1000h(source index)
6.      Initialize the DI with 2000h(destination index)
7.      Accept the  character through keyboard(AL=52h i.e ASCII hex value of ‘m’)
8.      Move the AL contents to a location pointed by SI and DI.
9.      Increment the CL register contents by 1
10.  Increment SI and DI by 1
11.  Repeat the step 7 to 10 till Enter key get pressed(i.e AL=0Dh ASCII hex value for enter key used to detect the end of the string.)
12.  Decrement the CL by 1 ( to avoid the enter key as a part of the string to obtain correct length value)
13.  Preserve the length in temporary variable  say count from CL
14.  If choice = 2 then goto step no.15 (display the string)else goto step no. 21
15.  Initialize SI again with 1000h (string source)
16.  Move the content of location pointed by SI to DL
17.  Display the character on the screen
18.  Increment SI by 1and decrement the CL by 1
19.  Repeat the step from 15 to 18 till Zero flag will come set (i.e CL reaches to zero).
20.  Load the String length from count to CL register back.
21.   If choice=3 (display the length of string) then goto step no. 22 else goto step no. 24
22.  Move the contents of CL to AL and add 30h to AL
23.  Move the AL contents to DL and display the length of string.
24.  If choice=4 (Reverse the string) then goto step no. 24 else goto step no.30
25.  Add CX (e.g CX=0005)to SI to make SI to point to the last letter of String.
26.  Decrement to SI by 1 as SI will start from 0 index (e.x 1000 to 10004 are the locations if string is of 5 letters)
27.  Move the letter pointed by SI to DL and display it on the screen
28.  Decrement the SI by 1 and decrement the CL by 1
29.  Repeat step 25 to 29 till CL reaches to zero if zero flag get set.
30.  If choice = 5 then goto step no. 31 else goto the step no.43
31.  Initialize SI with 1000h again
32.  Initialize DI with 2000h again
33.  Load CL with original length of string from count
34.  Add DI with CX (e.x CX=0005)so DI will point to the last letter of string
35.  Move the letter from location pointed by SI to AL
36.  Move the letter from location pointed by DI to BL
37.  Compare the AL and BL if zero flag is not set then goto step no. 38 else goto step no 39
38.  Display the string is not palindrome.
39.  Increment the SI by 1 and Decrement DI by 1
40.  Decrement CL by 1.
41.  Repeat step no. 34 to 38 till CL reaches to zero
42.  If zero flag is set then display the string is palindrome.
43.  If choice=6 then goto step no. 44 else goto step no 49
44.  Accept the letter to be searched in AL.
45.  Load CL with original length of string from count
46.  Initialize the DI with 2000h
47.  Use REPNE SCASB instruction this instruction scans the string for a letter stops when it finds the first occurrence of a letter. It decrements CX also by 1 for every scan. When this instruction stops CX will contain value as position-1 value.
48.   Obtain the position of a letter by subtracting total length-CX value and display it on the screen.

49.  Stop.
TASM Program :
Data Segment
 chc db 0dh,0ah,"Choose Operation: $"
 chc1 db 0dh,0ah," 1. Accept String $"
 chc2 db 0dh,0ah," 2. Display String $"
 chc3 db 0dh,0ah," 3. Display Length $"
 chc4 db 0dh,0ah," 4. Display Reverse $"
 chc5 db 0dh,0ah," 5. Check Palindrome $"
 chc6 db 0dh,0ah," 6. Exit $"
 len db ?
 msg db 0dh,0ah,"Please enter the String: $"
 msg1 db 0dh,0ah,"The length of the entered String is: $"
 msg2 db 0dh,0ah,"The entered String is: $"
 msg3 db 0dh,0ah,"The reverse of the entered String is: $"
 pal db 0dh,0ah,"The String is a Palindrome. $"
 npal db 0dh,0ah,"The String is not a Palindrome. $"
 newl db 0dh,0ah," $"
Data ends
Code Segment
 assume DS:Data,CS:Code
Start:
 mov ax,Data
 mov DS,ax

 call AcceptString

l: mov dx,offset chc
 mov ah,09h
 int 21h
 mov dx,offset chc1
 mov ah,09h
 int 21h
 mov dx,offset chc2
 mov ah,09h
 int 21h
 mov dx,offset chc3
 mov ah,09h
 int 21h
 mov dx,offset chc4
 mov ah,09h
 int 21h
 mov dx,offset chc5
 mov ah,09h
 int 21h
 mov dx,offset chc6
 mov ah,09h
 int 21h

 mov dx,offset newl
 mov ah,09h
 int 21h

 mov ah,01h
 int 21h
 sub al,30h

 cmp al,06h
 jz ex

 cmp al,05h
 jne n1
 call CheckPal

n1: cmp al,04h 
 jne n2
 call DisplayRev

n2: cmp al,03h 
 jne n3
 call DisplayLength

n3: cmp al,02h 
 jne n4
 call DisplayString

n4: cmp al,01h
 jne l
 call AcceptString 
 jmp l

ex: mov ah,4ch
 int 21h
AcceptString proc
 mov si,1000h
 mov di,1000h
 mov cx,0000h

 mov dx,offset msg
 mov ah,09h
 int 21h

    back: 
 mov ah,01h
 int 21h
 
 cmp al,0dh
 je comp
 inc cx
 mov [si],al
 mov [di],al
 inc si
 inc di
 jmp back
comp: mov len,cl
 ret
endp
DisplayString proc
 mov dx,offset msg2
 mov ah,09h
 int 21h

 mov cl,len
 mov ch,00h
 mov si,1000h

    disp: 
 mov dl,[si]
 mov ah,02h
 int 21h
 
 inc si
 loop disp
 ret
endp
DisplayLength proc
 mov dx,offset msg1
 mov ah,09h
 int 21h

 mov bl,len
 call DispNum
 ret
endp
DispNum proc
 mov al,bl
 and al,0f0h
 ror al,4

 mov dl,al
 call HexDisp
 mov ah,02h
 int 21h

 mov al,bl
 and al,0fh
 
 mov dl,al
 call HexDisp
 mov ah,02h
 int 21h
endp
HexDisp proc
 cmp dl,0ah
 jc nothex
 add dl,07h
 nothex: add dl,30h
 ret
endp
DisplayRev proc
 mov dx,offset msg3
 mov ah,09h
 int 21h

 mov cl,len
 mov ch,00h
 mov si,1000h
 add si,cx
 dec si

     dispr: 
 mov dl,[si]
 mov ah,02h
 int 21h
 
 dec si
 loop dispr
 ret
endp
CheckPal proc
 mov al,len
 mov ah,00h
 mov bl,02h
 div bl
 mov cl,len
 dec cl
 mov ch,00h
 mov di,1000h
 mov si,1000h
 add di,cx
 mov cl,al
 mov ch,00h
 
     pchk: 
 mov al,[si]
 cmp al,[di]
 jnz np
 inc si
 dec di
 loop pchk

 mov dx,offset pal
 mov ah,09h
 int 21h
 ret
 
      np: 
 mov dx,offset npal
 mov ah,09h
 int 21h
 ret
endp
Code ends
end Start
output:-
C:\TASM\BIN>tasm string.asm
Turbo Assembler  Version 4.1  Copyright (c) 1988, 1996 Borland International

Assembling file:   string.asm
Error messages:    None
Warning messages:  None
Passes:            1
Remaining memory:  453k

C:\TASM\BIN>tlink string.obj
Turbo Link  Version 7.1.30.1. Copyright (c) 1987, 1996 Borland International
Warning: No stack

C:\TASM\BIN>string

Please enter the String: Micro Processors

Choose Operation:
 1. Accept String
 2. Display String
 3. Display Length
 4. Display Reverse
 5. Check Palindrome
 6. Exit
2
The entered String is: Micro Processors
Choose Operation:
 1. Accept String
 2. Display String
 3. Display Length
 4. Display Reverse
 5. Check Palindrome
 6. Exit
3
The length of the entered String is: 10
Choose Operation:
 1. Accept String
 2. Display String
 3. Display Length
 4. Display Reverse
 5. Check Palindrome
 6. Exit
4
The reverse of the entered String is: srossecorP orciM
Choose Operation:
 1. Accept String
 2. Display String
 3. Display Length
 4. Display Reverse
 5. Check Palindrome
 6. Exit
5
The String is not a Palindrome. 
Choose Operation:
 1. Accept String
 2. Display String
 3. Display Length
 4. Display Reverse
 5. Check Palindrome
 6. Exit
1
Please enter the String: abcba

Choose Operation:
 1. Accept String
 2. Display String
 3. Display Length
 4. Display Reverse
 5. Check Palindrome
 6. Exit
5
The String is a Palindrome. 
Choose Operation:
 1. Accept String
 2. Display String
 3. Display Length
 4. Display Reverse
 5. Check Palindrome
 6. Exit
6
--------------------------------

0 comments: