You are on page 1of 4

BCSL-022

Qns 2(a): 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 are 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.
Ans:
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

Qns 2(b): Write and run (using appropriate calling program) a near procedure in assembly
language that converts two unpacked BCD digits to a packed BCD digit. Both the
unpacked BCD digits are passed as parameters on the stack. The packed BCD byte is
returned back on the AL register itself.
Ans:

DATA SEGMENT
BCD DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE
START:
MOV AX,DATA
MOV DS,AX
PUSH 04H
PUSH 05H
CALL PACKBCD
MOV BCD,AL
MOV AH,4CH
INT 21H
CODE ENDS
PACKBCD PROC NEAR
POP DX
POP AX
MOV AH,AL
POP BX
MOV AL,BL
MOV CL,4
ROL AH,CL
ADD AL,AH
PUSH DX
RET
PACKBCD ENDP
END START

Qns 2(c): Write and run an assembly language program that accepts a two digit input from the
keyboard, and convert this two digit ASCII to equivalent binary value. The output
should be stored in the AL register.
Ans:
DATA SEGMENT
MSG DB 10,13,ENTER 2 DIGIT NUMBER : $
ASC1 DB ?
ASC2 DB ?
BIN DB ?
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:
MOV AX,DATA
MOV DS,AX

LEA DX,MSG
MOV AH,9H
INT 21H
MOV AH,1
INT 21H
MOV ASC1,AL
SUB AL,30H
MOV BL,AL
MOV AH,1
INT 21H
MOV ASC2,AL
SUB AL,30H
MOV CL,04H
ROL BL,CL
OR AL,BL
MOV BIN,AL
MOV AH,4CH
INT 21H
CODE ENDS
END START

You might also like