You are on page 1of 6

ASSIGNMENT NO:4

ROLL NO :29
BATCH: S2
TITLE: Write menu driven ALP to perform string manipulations. The strings to be accepted from the
user is to be stored in code segment Module_1 and write FAR PROCEDURES in code segment
Module_2 to perform any two of the following string operations:
1)Concatenation of two strings.
2)Finding Number of occurrences of a sub-string in the given string

Module_1
.model small
.data
menu db 10d,13d," MENU"
db 10d,"1. Concatenation"
db 10d,"2. Substring"
db 10d,"3. Exit"
db 10d,"Enter your choice: $"

.code
;concat and substring procedures will be defined in external files
extrn concat:far
extrn substring:far

mov ax,@data
mov ds,ax

main:
lea dx,menu
mov ah,09h ;display menu string
int 21h

mov ah,01h ;single char input


int 21h

cmp al,'1'
je case1
cmp al,'2'
je case2
jmp exit

case1: call concat ;call procedure


jmp main
case2: call substring ;call procedure
jmp main
exit:
mov ah,4Ch
int 21h
end ;end of program
Module_2
.model small
.data
m1 db 10d,13d,"Enter first string: $"
m2 db 10d,13d,"Enter second string: $"
m3 db 10d,13d,"Concatenated string: $"
s1 db 20 dup('$')
s2 db 20 dup('$')
s3 db 40 dup('$')
nwline db 10d,"$"
m4 db 10d,13d,"Second String is Substring.$"
m5 db 10d,13d,"Second String is not a Substring.$"
m6 db 10d,13d,"No. Of Occurrence: $"
count db 1 dup(0)

.code
public concat,substring
;procedures scope has been made public
;accessible to other segments also

;*********Generalised 2 parameters 16 bit macro************


scall macro xx,yy
lea dx,xx
mov ah,yy
int 21h
endm ;end of macro

;*******************CONCATENATION PROCEDURE*****************
concat proc
mov ax,@data
mov ds,ax

scall m1,09h ;display m1 string


scall s1,0Ah ;buffered s1 string input

scall m2,09h ;display m2 string


scall s2,0Ah ;buffered s2 string input

lea si,s1
lea di,s3
inc si
mov cl,[si] ;length of s1 string

;copying entire s1 string to s3


loop1: inc si
mov al,[si]
mov [di],al
inc di
dec cl
jnz loop1
lea si,s2
inc si
mov cl,[si] ;length of s2 string

;copying entire s2 string to s3


loop2: inc si
mov al,[si]
mov [di],al
inc di
dec cl
jnz loop2

mov al,24h ;'$'=24h, putting $ at end of string s3


mov [di],al

scall nwline,09h
scall m3,09h ;display m3 string
scall s3,09h ;display concatenated string

ret ;return from prodecure


endp ;end of prodecure

;****************SUBSTRING PROCEDURE**********************
substring proc
mov ax,@data
mov ds,ax

scall m1,09h ;display m1 string


scall s1,0Ah ;accept s1 string
scall m2,09h ;display m2 string
scall s2,0Ah ;accept s2 string

lea si,s1
inc si
mov cl,[si] ;take length of s1 string in cl
inc si

lea di,s2
inc di
mov ch,[di] ;take length of s2 string in cl
inc di
mov dh,ch ;backup of ch register

mov [count],0 ;initialise count with zero

loop3:
mov al,[si]
mov bp,si ;backup of si pointer
cmp al,[di]
je loop4
inc si
dec cl ;counter for main string
jnz loop3

mov dl,[count]
cmp dl,0 ;dl=0 implies no string found
je fail
jmp result

loop4:
dec ch ;counter for sub-string
cmp ch,0
je success
inc si
inc di
mov al,[si]
cmp al,[di]
je loop4 ;continue this loop till string are same

jmp loop3 ;inc case of mismatch, start again

success:
add [count],01
lea di,s2
add di,2 ;move di to string place
inc bp
mov si,bp ;restore si from bp
dec cl
mov ch,dh ;restore ch from dh

jmp loop3 ;start again till main string ends

fail:
scall m5,09h ;display m5
ret ;return from procedure

result:
scall m4,09h ;display m4 string
scall m6,09h ;display m6 string

mov dl,[count]
add dl,30h
mov ah,02h ;display dl contents
int 21h
ret ;return from procedure
endp ;end of procedure
end ;end of Program
OUTPUT:

C:\TASM1~1.4\Tasm>p1.exe

MENU
1. Concatenation
2. Substring

3. Exit
Enter your choice: 1

Enter first string: pvg

Enter second string:

coe

Concatenated string: pvgcoe

MENU

1. Concatenation
2. Substring
3. Exit
Enter your choice: 2

Enter first string:

pvgcoe Enter second

string: coe

Second String is

Substring. No. Of

Occurrence: 1

MENU
1. Concatenation
2. Substring
3. Exit
Enter your choice: 3

You might also like