Anda di halaman 1dari 29

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

MICROPROCESSOR LAB
MANUAL (SOFTWARE
PROGRAMS)
Note: Only programs have been given. The method of writing the programs and
designing of 8086 program using algorithms is explained in the text book:

Introduction to microprocessors 8086


by:

Prof. Padma Reddy & Prof. Y. Jayasimha

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 1

A. 1

A. 2 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

APPENDIX- LAB PROGRAMS


Problem 1.(a): Search a key element in a list of 'n' 16-bit numbers using the binary
search algorithm (Detailed design r efer section 8.10.3, page 8.77)
; Program Name: BINARY SEARCH (BSRCH.ASM)
; Author
:
A.M. Padma Reddy and YJ
; Purpose
:
To search for the key in a given list using binary search
; Inputs
:
List of elements, number of elements, key
; Outputs
:
Success if key found, Unsuccessful if key not found
.MODEL SMALL
; Macro definitions
; Macro definition to display the message
PRINTF
MACROMSG ; Here, MSG is the parameter to the macro
MOV AH, 09H
MOV DX, OFFSET MSG
INT
21H
ENDM
; Macro to terminate the program (return control to DOS)
EXIT
MACRO
MOV AH, 4CH
INT 21H
ENDM
;------------------ ---- --- -- Data segment starts- --- ----- ---- ----------------.DATA
A DW 1111H, 2222H, 3333H, 4444H, 5555H
N DW ($-A)/2 ; Number of terms of N
KEY DW 5555H ; Key to be searched
LOW_ DW ?
HIGH_ DW ?
MID DW ?
MSG1 DB SUCCESSFUL SEARCH, 0AH, 0DH, 24H
MSG2 DB UNSUCCESSFUL SEARCH, 0AH, 0DH, 24H
;------------------ ---- --- -- Data segment ends-------------------------------

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 2

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

;------------------ ------- --Code segment starts-----------------------------.CODE


begin: ; Initialization of data segment register
MOV AX, @DATA
MOV DS, AX
;Initialization for the loop
MOV LOW, 0 ; low 0
MOV AX, N ; AX N
MOV HIGH_, AX
DEC HIGH_ ; high N -1
;Search using binary search
L1:
MOV SI, LOW_
CMP SI, HIGH_
JG L4 ;if ( low > high ) L4
; mid
(low + high ) / 2
ADD SI, HIGH_
; SI low + high
SHR SI, 1 ; SI (low + high)/2
; mid
MOV MID, SI

(low + high ) / 2

;if ( key != a[mid] ) L2

MOV
MOV
SHL
CMP
JNE

AX, KEY
SI, MID
SI, 1
AX, A[SI]
L2

; SI should be twice mid

PRINTF MSG1 ; print search success


EXIT
; Terminate program
L2: CMP AX, A[SI] ;
JG L3
; high
MOV AX, MID
DEC AX

if ( key > a[mid] ) L3

mid 1
; AX
; AX

mid
mid 1

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 3

A. 3

A. 4 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

MOV HIGH_, AX

; high

mid 1

JMP L1
; low
mid + 1
L3: MOV AX, MID
INC AX
MOV LOW_, AX

; AX
; AX
; low

mid
mid + 1
mid + 1

JMP L1
L4: PRINTF MSG2 ; print search unsuccessful
EXIT
; Terminate program
END begin
;------------------ ---- --- -- Code segment ends-----------------------------Note:Since LOW and HIGH are assembler directives in 8086 assembly language, we use
the identifiers LOW_ and HIGH_ instead of LOW and HIGH in the above program

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 4

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

A. 5

Problem 2: Write ALP macros:


i) to read a character from the keyboard in the module(1) (in a different file)
ii) to display a character in module (2) (from different file)
iii) 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
Note:Type two programs and save using the names 2A.ASM and 2B.ASMas shown
below:
.model small

2A.ASM

.stack
;------------------ --- Data segment starts------------ -.data
extrn a:byte
;Defines that an array variable a is external to the file
msg1 db 0ah,0dh,Enter the characters:,24h
msg2 db 0ah, 0dh,The typed characters:,24h
;------------------ --- Data segment ends--------------;Macro to display the message
printf macro msg
mov
ah, 09h
; function to display the text
lea
dx, msg ; offset value of text to be displayed
int
21h
endm
;macro to terminate the program
exit macro
mov ah, 4ch ; function to terminate the program
int
21h
endm
;------------------ Code segment starts---------------.code
extrn getchar:far ;Function getchar is a defined in another file
extrn putchar:far ;Function putchar is defined in another file

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 5

A. 6 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

begin: mov ax,@data


mov ds,ax ; Initialization of data segment register
printf msg1 ; printf(Enter the characters);
mov si, 00 ; Initialize the index si to 0
next: call getchar
; Read a character
cmp al, 0dh ; Is character read is end of line
je over
mov a[si], al ; Store the character in the array
inc si ; point to next location in array a
jmp next ; Read the next character
over: mov cx, si ; Numer of characters to be displayed
printf msg2 ; printf(The typed characters are:);
mov si, 00 ; Initialize si to point to beginning of a
up:
call
inc
loop

mov al,a[si] ; Obtain the character from array


putchar ; Display the current character
si ; Point si to next character
up

mov ah,4ch
int 21h
end begin
;end of the program
Note: After typing 2A.ASM, Give the following commands:
C:\>MASM 2A;
C:\>EDIT 2B.ASM
and type the program given in next page

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 6

LAB PROGRAMS
.model small
.stack

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

2B.ASM

; ------------------ --- Start of data segment ----------------------------------.data


public a
;The data array is being made public allocating
a db 10H dup(?)
; Reserved 10 locations. external function can access a
;------------------ -- End of data segment -----------------------------------;------------------ --Start of code segment---------------------.code
public getchar ; External program can access getchar()
public putchar ; External program can access putchar()
;Procedure to read key is made public
getchar proc far
mov ah,01 ; Function to read a character
int 21h ; Character read is copied to AL register
ret
getchar endp
;Procedure to write a character
putchar proc far
mov ah,02 ; Function to display the character
mov dl,al ; Character to be displayed
int 21h ; Display the character
ret
putchar endp
end;
;------------------ --- End of code segment -----------------------Note: After typing 2B.ASM, Give the following commands:
C:\>MASM 2b;
If no errors, type the following command
C:\>LINK 2A + 2B
C:> 2A
Enter the characters: PADMAREDDY
The typed characters:PADMAREDDY

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 7

A. 7

A. 8 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 3: Sort a given set of 'n' numbers in ascending and descending orders using the
bubble sort algorithm. (Refer section 8.10.4, page 8.81)
; Program Name: BUBBLE SORT (BSORT.ASM)
; Author
:
A.M. Padma Reddy and YJ
; Purpose
:
To sort the given elements using bubble sort
; Inputs
:
List of elements to sort
; Outputs
:
Sorted array
.MODEL SMALL
;------------------ ---- --- -- Data segment starts- --- ----- ---- ----------------.DATA
A DW 5555H, 4444H, 3333H, 2222H, 1111H
N DW ($-A)/2 ; Number of terms of N
I DW ?
J DW ?
TEMP DW ?
;------------------ ---- --- -- Data segment ends------------------------------;------------------ ---- --- -- Code segment starts-----------------------------.CODE
begin: ; Initialization of data segment register
MOV AX, @DATA
MOV DS, AX
MOV J, 1 ; Initial pass
MOV CX, N
DEC CX ; CX N-1
L1: MOV SI, 0
; of array A

; Index to the first item

MOV DX, N
SUB DX, J ; DX = N J is the
; counter for inner loop
L2: MOV AX, A[SI]
; AX a[si]
CMP AX, A[SI+2] ; If a[si] <= a[si+2])
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 8

LAB PROGRAMS
JLE L3

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

; goto L3

MOV TEMP, AX ; temp a[si]


MOV BX, A[SI+2] ; a[si]
MOV A[SI], BX

a[si+2]

MOV A[SI+2], AX ; since AX contains


; temp
ADD SI, 2

; Point to next word

L3: DEC DX ; decrement inner loop


JNZ L2 ; if (DX !=0) goto L2
LOOP L1 ; if (CX !=0) goto L1
; i.e., goto next pass
INT 3H ; Stop execution at this
; point
END begin
;------------------ ---- --- -- Code segment ends------------------------------

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 9

A. 9

A. 10 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 4: Read an alphanumeric character and display its equivalent ASCII code at the
center of the screen. (See section 9.3.1, page 9.12)
.MODEL SMALL
; Macro defintions
; Macro to read a character
GETCHAR MACRO
MOV AH, 01 ; Function to read a character
INT 21H
; AL has the character read
ENDM
; Macro display a character
PUTCHAR MACRO char
MOV AH, 02 ; Function to display a character
MOV DL, char ; Character to be displayed
INT 21H
ENDM
;Macro to display the message
PRINTF MACRO MSG
MOV AH, 09H ; Function to display the text
LEA DX, MSG ; Offset value of text to be displayed
INT 21H
ENDM
;Macro to ternminat the program
EXIT MACRO
MOV AH, 4CH ; Function to terminate the program
INT 21H
ENDM
;------------------ ---- --- -Data segment starts--- --- ---- ---- ---------.DATA
MSG1 DB "Enter a character",0ah, 0dh, 24h
MSG2 DB "The ASCII value: ",24h
;------------------ ---- --- -Data segment ends--------------------------

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 10

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

;------------------ ---- --- -Code segment starts-----------------------.CODE


begin: MOV AX, @DATA ; Initialization of data segment register
MOV DS, AX
PRINTF MSG1
GETCHAR
MOV
MOV

BH, AL
BL, AL

; DISPLAY "Enter a character"


; Read a character from keyboard
; Character in AL is copied to BL
; Copy into BL register also

; Convert the lower nibble into ASCII


AND BL, 0FH
; Mask the higher nibble
CMP BL, 0AH
JL L1
ADD BL, 07H
; If greater than 9, ADD 7
L1:
ADD BL, 30H
; Convert into ASCII by adding 30H
; Obtain ASCII value of higher nibble
AND BH, 0F0H
; Mask the lower nibble
MOV CL, 04
SHR BH, CL
; Move to higher nibble to lower nibble
CMP BH, 0AH
JL L2
ADD BH, 07H ; If greater than 9, add 7
L2:
ADD BH, 30H ; Conver into ASCII by adding 30H
PRINTF MSG2

; Display "The ASCII Value ="

PUTCHAR BH
PUTCHAR BL

; display higher nibble of ASCII value


; display lower nibble of ASCII value

EXIT
END begin

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 11

A. 11

A. 12 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 5: Reverse a string and check whether it is a palindrome or not


(see section 8.9.4, page 8.62)
; Program Name: PALINDROME(PALI.ASM)
; Author
:
A.M. Padma Reddy and YJ
; Purpose
:
To check whether the string is palindrome
; Inputs
:
Give the string
; Outputs
:
Palindrome or not a palindrome
.MODEL SMALL
; Macro definitions
; Macro definition to display the message
PRINTF
MACROMSG ; Here, MSG is the parameter to the macro
MOV AH, 09H
MOV DX, OFFSET MSG
INT
21H
ENDM
; Macro to terminate the program (return control to DOS)
EXIT
MACRO
MOV AH, 4CH
INT 21H
ENDM
;----------- Data segment starts-------------- --- - ; Memory representation
.DATA
STR1 DB LIRIL ; Given string
N DW $ STR1 ; Length of given string
STR2 DB 10 DUP(?) ; Storage space for reversed string
MSG1 DB String is palindrome, 0AH, 0DH, 24H
MSG2 DB String in not a palindrome, 0AH, 0DH, 24H
;------------ Data segment ends----------------;------------------ ---- --- -- Code segment starts-----------------------------.CODE
begin: ; Initialization of data segment register
MOV AX, @DATA
MOV DS, AX
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 12

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

;Initialization for reversing


MOV SI, 0 ; STR1[SI] points to first character of STR1
MOV DI, 0 ; STR2[DI] point to first character of STR2
MOV CX, N ; where N = 5
ADD SI, CX
DEC SI
L1: MOV AL, STR1[SI] ; Copy STR1[SI] to
MOV STR2[ DI], AL ; STR2[DI]
DEC SI
INC DI

; Point to previous character


; Point to next character

LOOP L1 ; Copy all characters of STR1


; Reversing of string is complete
; Check for the palindrome
; Initialization
MOV SI, 0
; SI points to beginning of STR1
MOV CX, N ; No. of characters to compare
L2: MOV AL, STR1[SI] ; Compare STR1[SI] with STR2[DI]
CMP AL, STR2[SI]
JE L3
PRINTF MSG2 ; String is not a palindrome
EXIT
; Terminate the program
L3: INC SI
LOOP L2

; Point to next character of STR1&2

; Compare N times

PRINTF MSG1 ; String is a palindrome


EXIT
; Terminate the program
END begin
; ------------------ ---- --- --- Code segment ends --------------------------------

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 13

A. 13

A. 14 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 6 : Read two strings, store them in locations STR1 and STR2.check whether
they are equal or not and display appropriated messages. Also display the length of
the
stored strings. (see section 9.3.2, page 9.14)
.MODEL SMALL
; Macro definitions
; Macro to read a character
SCANF MACRO BUFFER
MOV AH, 0AH ; Function to read a string
LEA DX, BUFFER ; Address of the buffer
INT 21H
; AL has the character read
ENDM
;Macro to display the message
PRINTF MACRO MSG
MOV AH, 09H ; Function to display the text
LEA DX, MSG ; Offset value of text to be displayed
INT 21H
ENDM
;Macro to display a character
PUTCHAR MACRO CHAR
MOV AH, 02
MOV DL, CHAR
INT 21H
ENDM
;Macro to terminate the program
EXIT MACRO
MOV AH, 4CH ; Function to terminate the program
INT 21H
ENDM
;------------------ ---- --- -Data segment starts--- --- ---- ---- ---------.DATA
MSG1 DB 0AH, 0DH,"Enter the first string",0ah, 0dh, 24h
MSG2 DB 0AH,0DH,"Enter the second string, 0ah, 0dh,24h
MSG3 DB 0AH,0DHThe two strings are equal, 0AH, 0DH, 24H
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 14

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

MSG4 DB 0AH,0DHThe two strings are not equal, 0AH, 0DH, 24H
MSG5 DB 0AH,0DHNo. of characters in first string :, 24H
MSG6 DB 0AH, 0DH, No. of characters in second string :, 24H
BUFFER1 DB 10
N1 DB ?
A1 DB 10 DUP(?)
BUFFER2 DB 10
N2 DB ?
A2 DB 10 DUP(?)
;------------------ ---- --- -Data segment ends-------------------------;------------------ ---- --- -Code segment starts-----------------------.CODE
begin: MOV AX, @DATA ; Initialization of data segment register
MOV DS, AX
MOV ES, AX ; Intialization of extra segment r egister
; It is must for string manipulation
PRINTF MSG1 ; printf(Enter the first string);
SCANF BUFFER1 ; scanf(%s,a1);
PRINTF MSG2 ; printf(Enter the second string);
SCANF BUFFER2 ; scanf(%s,a2);
; Check length of two strings
MOV AL, N1
CMP AL, N2
JE L1 ; If lengths are equal compare strings
PRINTF MSG4 ; printf(Two strings are not equal);
JMP L3
; Initialization before comparing
L1: LEA SI, A1 ; Offset value of first string
LEA DI, A2 ; Offset value of second string
MOV CL, N1 ; No of characters to be compared
CLD
; To increment increment index registers
REPE CMPSB ; Compare as long as they are equal
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 15

A. 15

A. 16 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

JCXZ L2 ; If CX is zero, two strings are equal


PRINTF MSG4 ; printf(Two strings are not equal);
JMP L3
L2: PRINTF MSG3 ; printf(Two strings ar e equal);
L3: PRINTF MSG5 ; printf(No. of characters in first string:);
ADD N1, 30H ; Convert to ASCII by adding 30h
PUTCHAR N1 ; display no of characters of first string
PRINTF MSG6 ; printf(No. of charas in second string:) ;
ADD N2, 30H ; Convert to ASCII by adding 30h
PUTCHAR N2 ; display no. of characters of second string
EXIT
; Terminate the program
END begin
;------------------ ---- --- ---- --End of code segment-------------- -------------------------------

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 16

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

A. 17

Problem 7 : Read your name from the keyboard and display it at a specified location on
the screen in front of the message WHAT IS YOUR NAME? you must clear the
entire
screen before display.
.model small
;Macro to clear the screen
clrscr macro
mov
bh,07 ;Attribute of character
mov
ah,06
;Activity is to scroll
mov
al,0
;No. of lines to scroll-0 blank screen
mov
ch,00
;y-coordinate of upper left corner
mov
cl,00 ;x-coordinate of upper right corner
mov
dh,24 ;Number of rows; Lower right row
mov
dl,80 ;Number of columns; Lower right column
int
10h ; clear the screen
endm
;Macro to set the cursor
;
setcursor macro row, col ; row and col are the parameters
mov
ah, 02 ; Function to set the cursor
mov
bh, 0 ; Current page
mov
dh, row ; Which row
mov
dl, col ; Which column
int
10h ; Set the cursor
endm
;Macro equvivalent to
;
printf()
;
scanf()
pri_scn
macro val, msg ; val function to read or display
; msg is the parameter to be displayed
mov
dx, offset msg ; DX holds offset of message to be displayed
mov
ah, 09 ; Function to display the message
int
21h ; Display the message
endm

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 17

A. 18 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

;Macro terminate the program


;Return control to dos
;
stop
macro
mov
ah, 4ch ; Function to terminate
int
21h ; Terminate the program
endm
;
;Macro display the character
; putchar() function of C
;
putchar macro val ; val is the parameter to be displayed
mov
dl,val ; Character to be displayed
mov
ah,02 ; Function to write a character
int
21h ; Display the character
endm
;-------------Start of the data segment--------.data
name1 db "What is your name?",24h
msg
db "Enter the name",0ah,0dh,24h
buffer db 10
; Max. no. of charcters in the name
n
db ?
; Space to store length of name
a
db 20 dup(?) ; Space to store the name
row
col

db 10
db 20

; Display at this row


; Display at this column

;---------------Start of code segment ----------.code


mov
ax, @data
mov
ds, ax
prn_scn 09, msg
prn_scn 0ah, buffer
clrscr

;printf("Enter the numbers");


;scanf("%s",a);

; Clear the screen

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 18

LAB PROGRAMS
setcursor row, col

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

; Set the cursor

prn_scn 09, name1 ; printf(What is your name?);


mov
mov
mov

cl,n
ch,0
si,00

top:
inc
loop

putchar a[si] ; Display the character


si ; Point to next character
top ; If not over go to top

; CX: no. of characters to displayed


; initial offset value w.r.t. array a

stop
; Terminate the program
end begin

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 19

A. 19

A. 20 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 8 : Compute nCr using recursive procedure. Assume that 'n' and 'r' are nonnegative integers (See section 9.5.1.2, page 9.32 for details)
.MODEL SMALL
.STACK 64
;------------------ ---- --- ---- -Start of data segment-------------------------------.DATA
N DB 6
K DB 3
RES DB 0
;------------------ ---- --- --- --Start of data segment-------------------------------;------------------ ---- --- ---- -Start of code segment-------------------------------.CODE
NCK PROC
CMP BL, 0
JNE L1 ; if k = 0
ADD RES, 1 ; return 1;
RET
L1: CMP BL, AL
JNE L2 ; if k = n
ADD RES, 1 ; return 1;
RET
L2: CMP BL, 1
JNE L3 ; if k = 1
ADD RES, AL ; return n
RET
L3: DEC AL ; n = n -1
CMP BL, AL
JNE L4 ; if k = n
INC AL ; return n+1
ADD RES, AL
RET

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 20

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

L4: PUSH AX ; Compute


PUSH BX
CALL NCK
POP BX
POP AX
DEC BX ; k = k -1
PUSH AX ; Compute
PUSH BX
CALL NCK
POP BX
POP AX
RET
NCK ENDP

n-1

Ck

n-1

Ck-1

A. 21

begin: MOV AX,@DATA ; Initialization of DS register


MOV DS,AX
MOV AL,N
MOV BL,K
CALL NCK

;Compute nCk

INT 3H ; Terminate the program


END begin
;------------------ ---- --- ---- --end of the code segment
-----------------------------------------Output: Give the following commands
C:\> MASM NCK;
C:\> LINK NCK;
C:\> DEBUG NCK.EXE
-G
-D DS:0000
The following memory dump is obtained:
14D4:0000 D8 A0 0C 00 8A 1E 0D 00-E8 B5 FF CC

06 03 148F
n k nCk = 14H
6C =
3

(20) 10
= (14)
16

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 21

A. 22 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 9 : Read the current time from the system and display it in the standard format
on the screen.
.model small
.stack
;Macro display the character
putchar macro val
push ax
mov dl,val ; Character to display
mov ah,02 ; Function to display the character
int 21h ; Display the character
pop
ax
endm
;---------------Start of the code segment ----------------.code
; Procedure to convert from HEX to ASCII and display
hex_asc proc
mov
ah,00
mov
bl,0ah ; Divide by 10 to obtain unpacked BCD
div
bl ; Obtain the unpacked BCD in AH and AL
; Higher nibble in AL
; Lower nibble in AH
add

ax,3030h ; Convert to ASCII value

putchar al ; Display the character in AL


putchar ah ; Display the character in AH
ret
disp

endp

begin: mov
ah, 2ch ; Function to read the system time
int
21h
; Read the system time
: CH:CL contains hours:minutes
; DH:DL contains second:millisec

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 22

LAB PROGRAMS
mov
al, ch
call
disp
putchar ':'

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

; Display the hour

mov
al,cl ; Display the minute
call
disp
putchar ':'
mov
call

al,dh ; Display the second


disp

exit
; Terminate the program
end begin

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 23

A. 23

A. 24 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 10 : Program to simulate a decimal up-counter to display 00-99

.model small
.stack
;Macro to display a character
putchar macro val
push dx
push ax
mov dl,val ; Character to be displayed
mov ah,02 ; Function to read character
int 21h ; Read a character
pop ax
pop dx
endm
;Code segment starts
.code
;Procedure to convert packed BCD to ASCII and display the character
dec_asci
proc
mov
cl,04
mov
bh,al ; AL has the packed bcd
shr
bh,cl ; Move the higher nibble to lower nibble
mov
bl,al
and
bl,0fh ; Mask the higher nibble
add
bx,3030h ; Convert to ASCII
putchar bh ; Display higher nibble
putchar bl ; Display lower nibble
putchar 0ah ; Move cursor to next line
putchar 0dh ; Move cursor to beginning of current line
ret
dec_asci
endp

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 24

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

; Main program starts from here


begin: mov
al,00
top:

daa

; convert to decimal

call

dec_asci ; convert to ASCII and display

cmp
je

al,99h ; Have you reached 99


down ; If yes go down

add

al,01 ; Increment count by 1

jmp

top ; perform earlier task

down:
mov
ah,4ch
; Function to terminate the program
int
21h ; Terminate the program
end begin

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 25

A. 25

A. 26 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 11 : read a pair of input co=ordinates in BCD and move the cursor to the
specified location on the screen.
.model small
; Cursor to set at the specified row and column
setcursor macro row, col
mov
ah, 02 ; Function to set cursor
mov
bh, 00 ; page number
mov
dh, row ; Set at this row
mov
dl, col ; Set at this column
int
10h
endm
;Macro to terminate the program
exit macro
mov
ah, 4ch ; Function to terminate the program
int
21h ; Terminate the program
endm
;Macro to read a character
getchar macro
mov
ah,01h ; Function to read a character
int
21h ; AL contains the character typed
endm
;Macro to display the message
printf
macro msg ; msg is the parameter to be displayed
mov
ah, 09 ; Function to display the message
mov
dx, offset msg ; DX hold offset of message to be displayed
int
21h ; Display the message
endm
.data
row db ?
; Space to store row value
col db ?
; Space to store column value
msg1 db "Enter the row value :",0ah,0dh,24h
msg2 db "Enter the col value :",0ah,0dh,24h
.code
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 26

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

; Procedure to read a byte and convert it to packed BCD


read
proc
getchar
; Get an ASCII value
sub
al, 30h ; Convert into unpacked BCD
mov
cl, 4
shl
al, cl ; Make it higher nibble
mov
bl, al ; Save higher nibble in BL
getchar
; Get next ASCII value
sub
al, 30h ; Convert into unpacked BCD
add
al, bl ; Convert 2 unpacked BCD to packed BCD
ret
read endp
begin:
mov

mov
ds, ax

printf
call

msg1
read

mov

row, al

printf
call
mov

msg2
read
col, al

ax, @data ; Initialization of DS register


; printf("Enter row value");
; scanf("%d",&row);
; Save the row value
; printf("Enter col value");
; scanf("%d",&col);
; Save the column value

setcursor row,col ; Set the cursor at specified row, column


getchar
exit

; Read a character
; Terminate the program

end begin
C:\MASM615>cursor
Enter the row value :10
Enter the col value :20
Note: Observe on the screen that cursor will be at row 10 and column 20

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 27

A. 27

A. 28 LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

Problem 12 : program to create a file (input file) and to delete an existing file
.model small
.stack
; Macro to display the message and read the string
prn_scn macro val,msg ; val = 09 display the message msg
; val = 09 read into msg
mov
ah,val ; Function to read or print
mov
dx,offset msg ; Offset value of message or buffer
int
21h ; Read or write
endm
;Macro to terminate the program
exit macro
mov
ah,4ch ; Function to terminate the program
int
21h ; Terminate the program
endm
.data
msg1

db "Enter the file :",0ah,0dh,24h

buff
db 20 ; Max. no. of characters in the file
len
db ? ; Length of file name
fname db 20 dup(?) ; Space for storing the file name
msg2
msg3

db 0ah,0dh,"File successfully deleted",24h


db 0ah,0dh,"Error in deleting: File may not exist or readonly",24h

;------------------ --Start of code segment ------------------.code


mov
ax,@data
mov
ds,ax
prn_scn 09,msg1 ; printf(Enter the file name);;
prn_scn 0ah, buff; ; scanf( %s,fname);

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 28

LAB PROGRAMS

(SAI VIDYA INSTITUTE OF TECHNOLOGY)

;Attach ASCIIZ at the end of the file


mov
bx,00
add
bl,len ; Length of file name
mov
fname[bx] ,0 ; Store ASCIIZ at the end of file name
;Delete the file
mov
ah,41h ; Function to delete the file
mov
dx,offset fname ; DX contains name of file
int
21h ; Delete the file
jc
fail ; If Carray is set delete failed
prn_scn
09,msg2 ; File deleted successfully
exit
; Terminate the program
fail: prn_scn 09h,msg3 ; File not deleted
exit
; Terminate the program
end begin

Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore

Page 29

A. 29

Anda mungkin juga menyukai