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 :

  1. DATA SEGMENT
  2. STR DB 'AXYBCSDEF$'
  3. SUBSTR DB 'BCS$'
  4. LEN1 DB 0
  5. LEN2 DB 0
  6. MSG1 DB 10,13,'STRING IS : $'
  7. MSG2 DB 10,13,'SUBSTRING IS : $'
  8. MSG3 DB 10,13,'SUBSTRING IS FOUND AT POSITION : $'
  9. POS DB -1
  10. RTN DB '-1$'
  11. DATA ENDS
  12. DISPLAY MACRO MSG
  13. MOV AH,9
  14. LEA DX,MSG
  15. INT 21H
  16. ENDM
  17. CODE SEGMENT
  18. ASSUME CS:CODE,DS:DATA
  19. START:
  20. MOV AX,DATA
  21. MOV DS,AX
  22. DISPLAY MSG1
  23. DISPLAY STR
  24. DISPLAY MSG2
  25. DISPLAY SUBSTR
  26. LEA SI,STR
  27. NXT1:
  28. CMP [SI],'$'
  29. JE DONE1
  30. INC LEN1
  31. INC SI
  32. JMP NXT1
  33. DONE1:
  34. LEA DI,SUBSTR
  35. NXT2:
  36. CMP [DI],'$'
  37. JE DONE2
  38. INC LEN2
  39. INC DI
  40. JMP NXT2
  41. DONE2:
  42. DISPLAY MSG3
  43. LEA SI,STR
  44. MOV AL,LEN1
  45. SUB AL,LEN2
  46. MOV CL,AL
  47. MOV CH,0
  48. FIRST:
  49. INC POS
  50. MOV AL,[SI]
  51. CMP AL,SUBSTR[0]
  52. JE CMPR
  53. INC SI
  54. LOOP FIRST
  55. CMPR: INC SI
  56. MOV AL,[SI]
  57. CMP AL,SUBSTR[1]
  58. JNE NOTEQUAL
  59. INC SI
  60. MOV AL,[SI]
  61. CMP AL,SUBSTR[2]
  62. JE EQUAL
  63. NOTEQUAL:
  64. MOV POS,-1
  65. DISPLAY RTN
  66. JMP EXIT
  67. EQUAL:
  68. MOV DL,POS
  69. ADD DL,30H
  70. MOV AH,2
  71. INT 21H
  72. EXIT: MOV AH,4CH
  73. INT 21H
  74. CODE ENDS
  75. END START

Screen shots :-

Asm_program_Search_SubString

After Execution :-

Asm_program_Search_SubString_Out

Leave a Reply