Upload
brijesh-balaram
View
573
Download
1
Tags:
Embed Size (px)
Citation preview
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 1
1
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
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 2
2
AAPPPPEENNDDIIXX -- LLAABB PPRROOGGRRAAMMSS
Problem 1.(a): Search a key element in a list of 'n' 16-bit numbers using the binary
search algorithm (Detailed design refer 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 MACRO MSG ; 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-------------------------------
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 3
3
;---------------------------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
MOV MID, SI ; mid (low + high ) / 2
;if ( key != a[mid] ) L2
MOV AX, KEY
MOV SI, MID
SHL SI, 1 ; SI should be twice mid
CMP AX, A[SI]
JNE L2
PRINTF MSG1 ; print search success
EXIT ; Terminate program
L2: CMP AX, A[SI] ; if ( key > a[mid] ) L3
JG L3
; high mid – 1
MOV AX, MID ; AX mid
DEC AX ; AX mid – 1
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 4
4
MOV HIGH_, AX ; high mid – 1
JMP L1
; low mid + 1
L3: MOV AX, MID ; AX mid
INC AX ; AX mid + 1
MOV LOW_, AX ; low 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
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 5
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.ASM as shown
below:
.model small
.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
2A.ASM
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 6
6
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: mov al,a[si] ; Obtain the character from array
call putchar ; Display the current character
inc si ; Point si to next character
loop 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
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 7
7
.model small
.stack
; ---------------------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
2B.ASM
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 8
8
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 ; Index to the first item
; of array A
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])
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 9
9
JLE L3 ; goto L3
MOV TEMP, AX ; temp ← a[si]
MOV BX, A[SI+2] ; a[si] a[si+2]
MOV A[SI], BX
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------------------------------
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 10
10
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--------------------------
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 11
11
;--------------------------Code segment starts------------------------
.CODE
begin: MOV AX, @DATA ; Initialization of data segment register
MOV DS, AX
PRINTF MSG1 ; DISPLAY "Enter a character"
GETCHAR ; Read a character from keyboard
MOV BH, AL ; Character in AL is copied to BL
MOV BL, AL ; 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 ; display higher nibble of ASCII value
PUTCHAR BL ; display lower nibble of ASCII value
EXIT
END begin
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 12
12
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 MACRO MSG ; 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
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 13
13
;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 ; Point to previous character
INC DI ; 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 ; Point to next character of STR1&2
LOOP L2 ; Compare N times
PRINTF MSG1 ; String is a palindrome
EXIT ; Terminate the program
END begin
; ----------------------------Code segment ends --------------------------------
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 14
14
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,0DH“The two strings are equal”, 0AH, 0DH, 24H
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 15
15
MSG4 DB 0AH,0DH“The two strings are not equal”, 0AH, 0DH, 24H
MSG5 DB 0AH,0DH“No. 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 register
; 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
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 16
16
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 are 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----------------------------------------------
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 17
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
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 18
18
;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 db 10 ; Display at this row
col db 20 ; Display at this column
;---------------Start of code segment -----------
.code
mov ax, @data
mov ds, ax
prn_scn 09, msg ;printf("Enter the numbers");
prn_scn 0ah, buffer ;scanf("%s",a);
clrscr ; Clear the screen
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 19
19
setcursor row, col ; Set the cursor
prn_scn 09, name1 ; printf(“What is your name?”);
mov cl,n
mov ch,0 ; CX: no. of characters to displayed
mov si,00 ; initial offset value w.r.t. array a
top: putchar a[si] ; Display the character
inc si ; Point to next character
loop top ; If not over go to top
stop ; Terminate the program
end begin
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 20
20
Problem 8 : Compute nCr using recursive procedure. Assume that 'n' and 'r' are non-
negative 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
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 21
21
L4: PUSH AX ; Compute n-1
Ck
PUSH BX
CALL NCK
POP BX
POP AX
DEC BX ; k = k -1
PUSH AX ; Compute n-1
Ck-1
PUSH BX
CALL NCK
POP BX
POP AX
RET
NCK ENDP
begin: MOV AX,@DATA ; Initialization of DS register
MOV DS,AX
MOV AL,N ;Compute nCk
MOV BL,K
CALL 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 14 8F
n k nCk = 14H
6C3 = (20)10
= (14)16
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 22
22
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
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 23
23
mov al, ch ; Display the hour
call disp
putchar ':'
mov al,cl ; Display the minute
call disp
putchar ':'
mov al,dh ; Display the second
call disp
exit ; Terminate the program
end begin
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 24
24
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
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 25
25
; Main program starts from here
begin: mov al,00
top: daa ; convert to decimal
call dec_asci ; convert to ASCII and display
cmp al,99h ; Have you reached 99
je 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
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 26
26
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
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 27
27
; 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 ax, @data ; Initialization of DS register
mov ds, ax
printf msg1 ; printf("Enter row value");
call read ; scanf("%d",&row);
mov row, al ; Save the row value
printf msg2 ; printf("Enter col value");
call read ; scanf("%d",&col);
mov col, al ; Save the column value
setcursor row,col ; Set the cursor at specified row, column
getchar ; Read a character
exit ; 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
A. LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 28
28
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 db 0ah,0dh,"File successfully deleted",24h
msg3 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);
LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.
Prof. A.M. Padma Reddy (HOD), SVIT, CSE Dept, Bangalore Page 29
29
;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