Learning Material-MP Lab
Learning Material-MP Lab
Program:
DATA SEGMENT
ARR DW 1010H,1011H,1012H,1013H
LEN DW($-ARR)/2
SERKEY EQU 1011H
MSG1 DB"KEY IS FOUND AT "
RES DB" POSITION",13,10,"$"
MSG2 DB "KEY IS NOT FOUND$"
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START : MOV AX,DATA
MOV DS,AX
MOV BX,00
MOV DX,LEN
MOV CX,SERKEY
AGAIN : CMP BX,DX
JA FAILURE
MOV AX,BX
ADD AX,DX
SHR AX,01
MOV SI,AX
ADD SI,SI
CMP CX,ARR[SI]
JAE BIGGER
DEC AX
MOV DX,AX
JMP AGAIN
BIGGER : JE SUCCESS
INC AX
MOV BX,AX
JMP AGAIN
SUCCESS : ADD AL,01
ADD AL,'0'
MOV RES,AL
LEA DX,MSG1
JMP DISPRES
FAILURE : LEA DX, MSG2
DISPRES : MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
2.) Write two ALP modules stored in two different files; one module is to read a character from the keyboard
and the other one is to display a character. Use the above two modules to read a string of characters from the
keyboard terminated by the carriage return and print the string on the display in the next line.
DATA SEGMENT
STRG DB 255 DUP(?)
NEWLINE DB 13,10,"$"
STR1 DB 13,10,'ENTER THE STRING===>$'
STR2 DB 13,10,'ENTEREDSTRINGIS==>$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
INCLUDE C:\8086\2A2.MAC
INCLUDE C:\8086\2A3.MAC
START: MOV AX,DATA
MOV DS,AX
LEA DX,STR1
MOV AH,09H
INT 21H
MOV SI,0
L1: READKB
CMP AL,0DH
JZ L2
MOV STRG[SI],AL
INC SI
JMP L1
L2: LEA DX,NEWLINE
MOV AH,09H
INT 21H
LEA DX,STR2
MOV AH,09H
INT 21H
MOV CX,SI
MOV SI,0
CMP CX,0
JE TERM
L3: MOV AL,STRG[SI]
ECHOCHAR
INC SI
LOOP L3
TERM:MOV AH,4CH
INT 21H
CODE ENDS
END START
3.) Sort a given set of n numbers in ascending order using the Bubble Sort algorithm.
Program:
DATA SEGMENT
A DB 20H,18H,42H,33H,78H,22H,62H,34H,12H,56H
LEN DW $-A
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV AX,DATA
MOV DS,AX
MOV BX,LEN
DEC BX
OUTLOOP:MOV CX,BX
MOV SI,0
INLOOP:MOV AL,A[SI]
INC SI
CMP AL,A[SI]
JB CONTINUE
XCHG AL,A[SI]
MOV A[SI-1],AL
CONTINUE:LOOP INLOOP
DEC BX
JNZ OUTLOOP
INT 3H
CODE ENDS
END START
4.) Read an alphanumeric character and display its equivalent ASCII code at the center of the screen.
DATA SEGMENT
MSG DB 10,13,'ENTER ANY ALPHANUMERIC CHARACTER=>$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
LEA DX,MSG
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
PUSH AX
MOV AH,07H
MOV AL,00H
MOV BH,0FH
MOV CH,00H
MOV CL,00H
MOV DH,34H
MOV DL,79H
INT 10H
MOV AH,02H
MOV DH,17H
MOV DL,40H
MOV BH,00
INT 10H
MOV DH,12
MOV DL,40
MOV BH,00
MOV AH,02H
INT 10H
POP AX
MOV BH,AL
MOV DL,AL
AND DL,0F0H
MOV CL,04H
ROR DL,CL
CMP DL,09
JLE NN
ADD DL,07H
NN: ADD DL,30H
MOV AH,02H
INT 21H
MOV DL,BH
AND DL,09
JLE NN1
ADD DL,07
NN1: ADD DL,30H
MOV AH,02H
INT 21H
MOV AH,08H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
5.) Reverse a given string and check whether it is a palindrome or not.
DATA SEGMENT
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
MOV ES,AX
LEA DX,MSG1
MOV AH,09H
INT 21H
MOV SI,0
L1:MOV AH,01H
INT 21H
CMP AL,0DH
JZ L2
MOV STR1[SI],AL
INC SI
JMP L1
L2:MOV CX,SI
MOV SI,0
MOV DI,CX
DEC DI
MOV DX,CX
PUSH DX
L3:MOV AL,STR1[SI]
MOV REVERSE[DI],AL
INC SI
DEC DI
LOOP L3
MOV REVERSE[SI],'$'
LEA DX,RMSG
MOV AH,09H
INT 21H
lea dx,reverse
mov ah,09h
INT 21H
POP DX
MOV CX,DX
LEA SI,STR1
LEA DI,REVERSE
CLD
REPE CMPSB
CMP CX,00H
JNZ NOTEQUAL
LEA DX,MSG2
MOV AH,09H
INT 21H
JMP FINISH
NOTEQUAL:
LEA DX,MSG3
MOV AH,09H
INT 21H
FINISH:
MOV AH,4CH
INT 21H
CODE ENDS
END START
6.) Read two strings, store them in locations STR1 and STR2. Check whether they are equal or not and
display appropriate messages. Also display the length of the stored strings.
DATA SEGMENT
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
MOV BL,LEN1
CMP BL,LEN2
JNE NOTEQUAL
MOV CL,LEN1
MOV CH,00
LEA SI,STR1
LEA DI,STR2
CLD
REPE CMPSB
CMP CX,00H
JNZ NOTEQUAL
LEA DX,MSG1
MOV AH,09H
INT 21H
JMP FINISH
NOTEQUAL:LEA DX,MSG2
MOV AH,09H
INT 21H
MOV AL,LEN1
MOV AH,00
AAM
ADD AX,3030H
MOV BX,AX
MOV DL,BH
MOV AH,02H
INT 21H
MOV DL,BL
MOV AH,02H
INT 21H
LEA DX,MSG4
MOV AH,09H
INT 21H
MOV AL,LEN2
MOV AH,00
AAM
ADD AX,3030H
MOV BX,AX
MOV DL,BH
MOV AH,02H
INT 21H
MOV DL,BL
MOV AH,02H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
7.)Read your name from the keyboard and display it at a specified location on the screen after the message
What is your name?. You must clear the entire screen before display.
DATA SEGMENT
MSG DB 10,13,'ENTER YOUR NAME====>$'
STR1 DB"WHAT IS YOUR NAME?"
NAM DB 257 DUP(?)
RNO DB 12
CNO DB 25
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
LEA DX,MSG
MOV AH,09H
INT 21H
MOV SI,00
BACK1: MOV AH,01H
INT 21H
CMP AL,0DH
JZ NEXT1
MOV NAM[SI],AL
INC SI
JMP BACK1
NEXT1: MOV NAM[SI],'$'
MOV AH,07H
MOV AL,00H
MOV BH,0FH
MOV CH,00H
MOV CL,00H
MOV DH,34H
MOV DL,70H
INT 10H
MOV BH,00H
MOV DH,RNO
MOV DL,CNO
MOV AH,02H
INT 10H
LEA DX,STR1
MOV AH,09H
INT 21H
MOV AH,08H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START
8.) Compute the factorial of a positive integer n using recursive procedure.
.MODEL SMALL
.DATA
NUM DW 5
RESULT DW ?
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,01
MOV BX,NUM
CMP BX,00 ;Compare num with 00
JE NEXT
CALL FACT
NEXT:MOV RESULT,AX
MOV AH,4CH
INT 21H
FACT PROC NEAR ;Recursive procedure to calculate Factorial
CMP BX,01 ;Compare num with 01
JZ RIP
PUSH BX
DEC BX
CALL FACT
POP BX
MUL BX
RIP:RET
FACT ENDP
END
9.) Compute nCr using recursive procedure. Assume that n and r are non-negative integers.
DATA SEGMENT
N DW 06H
R DW 03H
NCR DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START : MOV AX,DATA
MOV DS,AX
MOV AX,N
MOV BX,R
CALL NCR1
INT 3H
NCR1 PROC
CMP BX,00H
JZ RESULT1
CMP BX,0001H
JZ RUN
DEC AX
CMP AX,BX
JZ INCR
PUSH AX
PUSH BX
CALL NCR1
POP BX
POP AX
DEC BX
PUSH AX
PUSH BX
CALL NCR1
POP BX
POP AX
RET
RESULT1 : MOV NCR,01H
RET
INCR : INC AX
RUN : ADD NCR,AX
RET
NCR1 ENDP
MOV AX,NCR
CODE ENDS
END START
10.) Find out whether a given sub-string is present or not in a main string of characters.
.MODEL SMALL
.DATA
STR DB 'ONEPLUSTWOISEQUALTOTHREE'
L1 DW $-STR
SUBS DB 'PLUS'
L2 DW $-SUBS
MSG1 DB 'SUBSTRING IS PRESENT IN MAIN STRING$'
MSG2 DB 'SUBSTRING IS NOT PRESENT IN MAIN STRING$'
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
MOV CX,L1
MOV BX,L2
LEA DI,STR
LEA SI,SUBS
CLD
RPT:MOV AL,[SI]
REPNE SCASB ; Compares AL with every letter of STR
INC SI
MOV DX,CX
DEC BX
SUB CX,BX ; if the length of main string is < substring display msg2
JB DMSG2
MOV CX,BX
REPZ CMPSB ; else compare the two strings
JNZ NFT ; if strings are not equal display msg2
LEA DX,MSG1 ; else display msg1
MOV AH,9
INT 21H
JMP EXIT
DMSG2: LEA DX,MSG2
MOV AH,9
INT 21H
EXIT: MOV AH,4CH
INT 21H
NFT:MOV CX,DX
LEA SI,SUBS
MOV BX,L2
JMP RPT
END
11.) Generate the first n Fibonacci numbers.
.MODEL SMALL
.STACK
.DATA
N DB 10
FNS DB 32 DUP(0)
.CODE
MOV AX,@DATA
MOV DS,AX
MOV SI,0
MOV FNS[SI],0 ; copy the first number to the array
ADD SI,1
MOV FNS[SI],1 ; copy the second number to the array
MOV CL,N
SUB CL,2 ; CL= n -2 (for the rest of the length)
BAK:MOV AL,FNS[SI-1]
ADD AL,FNS[SI]
ADD SI,1
MOV FNS[SI],AL
LOOP BAK
MOV AH,4CH
INT 21H
END
12.) Read the current time from the system and display it in the standard format on the screen.
.MODEL SMALL
.CODE
MOV AH,2CH ; func(2Ch), to get the system time
INT 21H ; stores hrs, mins, secs in CH,CL,DH respectively
MOV AL,CH
CALL DISP
MOV DL,':'
MOV AH,2
INT 21H
MOV AL,CL
CALL DISP
MOV DL,':'
MOV AH,2
INT 21H
MOV AL,DH
CALL DISP
MOV AH,4CH
INT 21H
DISP PROC NEAR
AAM ; Converts HEX values to unpacked BCD format
ADD AX,3030H
MOV BX,AX
MOV DL,AH
MOV AH,02
INT 21H
MOV DL,BL
INT 21H
RET
DISP ENDP
END
13.) Program to simulate a Decimal Up-counter to display 00-99.
.MODEL SMALL
.CODE
MOV AX,0
NN: PUSH AX
CALL SETC ; to set the cursor position
CALL DISP ; to display the number
CALL DELAY ; delay between numbers
MOV AH,01
INT 16H ; check the input buffer to see if any key pressed
JNZ EXIT
POP AX
ADD AL,1
DAA
CMP AL,0
JNZ NN
EXIT: MOV AH,4CH
INT 21H
SETC: MOV AH,2
MOV DH,12
MOV DL,35
INT 10H
RET
DISP: MOV BL,AL
MOV DL,AL
MOV CL,4
SHR DL,CL
ADD DL,30H
MOV AH,2
INT 21H
MOV DL,BL
AND DL,0FH
ADD DL,30H
INT 21H
RET
DELAY: MOV SI,0FFFH
B2: MOV DI,0FFFFH
B1: DEC DI
JNZ B1
DEC SI
JNZ B2
RET
END
14.) Read a pair of input co-ordinates in BCD and move the cursor to the specified location on the screen.
.MODEL SMALL
.DATA
MSG1 DB 10,13,"ENTER ROW NO:$"
MSG2 DB 10,13,"ENTER COL NO:$"
MSG3 DB 10,13,"PRESS ANY KEY TO STOP$"
ROW DB ?
COL DB ?
.CODE
MOV AX,@DATA
MOV DS,AX
LEA DX,MSG1
MOV AH,9
INT 21H
CALL READ ; read the input row
MOV ROW,AL
LEA DX,MSG2
MOV AH,9
INT 21H
CALL READ ; read the input col
MOV COL,AL
LEA DX,MSG3
MOV AH,9
INT 21H
MOV AH,2 ; move the cursor to the entered row and col
MOV DH,ROW
MOV DL,COL
INT 10H
MOV AH,8 ; wait for a key to be pressed to exit
INT 21H
MOV AH,4CH
INT 21H
READ:MOV AH,1 ; to read the input of two numbers
INT 21H ; and store its equivalent decimal number in al
AND AL, 0FH
MOV BL,AL
MOV AH,1
INT 21H
AND AL,0FH
MOV AH,BL
AAD
RET
END
15) Program to create a file (input file) and to delete an existing file.
DATA SEGMENT
FNAME1 DB "F1.TXT",0
FNAME2 DB "FILE2.TXT",0
MSG1 DB 13,10,"1 FILE CREADTED$"
MSG2 DB 13,10,"FILE NOT CREATED$"
MSG3 DB 13,10,"1 FILE DELETED$"
MSG4 DB 13,10,"FILE CANNOT BE DELETED$"
FCONT DB "ANUSHA"
FHANDLE DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START : MOV AX,DATA
MOV DS,AX
MOV AH,3CH
MOV CX,0
LEA DX,FNAME1
INT 21H
JNC L1
LEA DX,MSG2
MOV AH,09H
INT 21H
JMP L2
L1 : MOV FHANDLE,AX
LEA DX,MSG1
MOV AH,09H
INT 21H
MOV BX,FHANDLE
MOV CX,06
LEA DX,FCONT
MOV AH,40H
INT 21H
L2 : LEA DX,FNAME2
MOV AH,41H
INT 21H
JC L3
LEA DX,MSG3
MOV AH,09H
INT 21H
JMP FINISH
L3 : LEA DX,MSG4
MOV AH,09H
INT 21H
CODE ENDS
END START