An Assembly language program to find substring in given string – IGNOU MCA Assignment 2014-15

By | July 22, 2014

BACHELOR OF COMPUTER APPLICATIONS
Course Code : BCSL-022
Course Title : Assembly Language Programming Lab
Assignment Number : BCA(II)/BCSL022/Assign/14-15
Maximum Marks : 50
Weightage : 25%

 

Write and run an Assembly language program that finds the occurrence of a given substring, for example, BCS in a given string, for example, AXYBCSDEF (please note that in both the strings a character occurs only once). You may assume that both the string as well as substrings is available in the memory. The program should output the starting index of the location where string is found, otherwise output should be -1. For example, the substring BCS can be found in the string AXYBCSDEF from the index 3. So the output should be 3.

 

DATA SEGMENT
STR DB ‘AXYBCSDEF$’
SUBSTR DB ‘BCS$’
LEN1 DB 0
LEN2 DB 0
MSG1 DB 10,13,’STRING IS : $’
MSG2 DB 10,13,’SUBSTRING IS : $’
MSG3 DB 10,13,’SUBSTRING IS FOUND AT POSITION : $’
POS DB -1
RTN DB ‘-1$’
DATA ENDS

DISPLAY MACRO MSG
MOV AH,9
LEA DX,MSG
INT 21H
ENDM

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX

DISPLAY MSG1
DISPLAY STR
DISPLAY MSG2
DISPLAY SUBSTR

LEA SI,STR
NXT1:
CMP [SI],’$’
JE DONE1
INC LEN1
INC SI
JMP NXT1
DONE1:
LEA DI,SUBSTR
NXT2:
CMP [DI],’$’
JE DONE2
INC LEN2
INC DI
JMP NXT2
DONE2:
DISPLAY MSG3

LEA SI,STR
MOV AL,LEN1
SUB AL,LEN2
MOV CL,AL
MOV CH,0
FIRST:
INC POS
MOV AL,[SI]
CMP AL,SUBSTR[0]
JE CMPR
INC SI
LOOP FIRST

CMPR: INC SI
MOV AL,[SI]
CMP AL,SUBSTR[1]
JNE NOTEQUAL
INC SI
MOV AL,[SI]
CMP AL,SUBSTR[2]
JE EQUAL

NOTEQUAL:
MOV POS,-1
DISPLAY RTN
JMP EXIT

EQUAL:
MOV DL,POS
ADD DL,30H
MOV AH,2
INT 21H

EXIT: MOV AH,4CH
INT 21H

CODE ENDS

END START

Program Code :

[codesyntax lang=”asm” lines=”normal”]

DATA SEGMENT
STR DB ‘AXYBCSDEF$’
SUBSTR DB ‘BCS$’
LEN1 DB 0
LEN2 DB 0
MSG1 DB 10,13,’STRING IS : $’
MSG2 DB 10,13,’SUBSTRING IS : $’
MSG3 DB 10,13,’SUBSTRING IS FOUND AT POSITION : $’
POS DB -1
RTN DB ‘-1$’
DATA ENDS

DISPLAY MACRO MSG
MOV AH,9
LEA DX,MSG
INT 21H
ENDM

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX

DISPLAY MSG1
DISPLAY STR
DISPLAY MSG2
DISPLAY SUBSTR

LEA SI,STR
NXT1:
CMP [SI],’$’
JE DONE1
INC LEN1
INC SI
JMP NXT1
DONE1:
LEA DI,SUBSTR
NXT2:
CMP [DI],’$’
JE DONE2
INC LEN2
INC DI
JMP NXT2
DONE2:
DISPLAY MSG3

LEA SI,STR
MOV AL,LEN1
SUB AL,LEN2
MOV CL,AL
MOV CH,0
FIRST:
INC POS
MOV AL,[SI]
CMP AL,SUBSTR[0]
JE CMPR
INC SI
LOOP FIRST

CMPR: INC SI
MOV AL,[SI]
CMP AL,SUBSTR[1]
JNE NOTEQUAL
INC SI
MOV AL,[SI]
CMP AL,SUBSTR[2]
JE EQUAL

NOTEQUAL:
MOV POS,-1
DISPLAY RTN
JMP EXIT

EQUAL:
MOV DL,POS
ADD DL,30H
MOV AH,2
INT 21H

EXIT: MOV AH,4CH
INT 21H

CODE ENDS

END START

[/codesyntax]

Screen shots :-

Asm_program_Search_SubString

After Execution :-

Asm_program_Search_SubString_Out

Leave a Reply