Vous êtes sur la page 1sur 29

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A.

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.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 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-------------------------------

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

Page 2

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 3


;---------------------------Code segment starts-----------------------------.CODE begin: ; Initialization of data segment register MOV MOV AX, @DATA DS, AX ; low 0 ; AX N ; high N -1

;Initialization for the loop MOV LOW, 0 MOV MOV DEC AX, N HIGH_, AX HIGH_

L1:

;Search using binary search MOV SI, LOW_ CMP SI, HIGH_ JG L4 ; mid (low + high ) / 2 ADD SI, HIGH_ SHR SI, 1
MOV MID, SI

;if ( low > high ) L4 ; SI low + high ; SI (low + high)/2 ; mid (low + high ) / 2

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

MOV MOV SHL CMP JNE PRINTF EXIT L2: CMP JG

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

; SI should be twice mid

; print search success ; Terminate program ; if ( key > a[mid] ) L3

; high mid 1 MOV DEC AX, MID AX


; AX mid ; AX mid 1

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

Page 3

A.4 LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)


MOV JMP HIGH_, AX L1
; AX mid ; AX mid + 1 ; low mid + 1 ; high mid 1

L3:

; low mid + 1 MOV AX, MID INC AX MOV LOW_, AX JMP PRINTF EXIT L1 MSG2

L4:

; print search unsuccessful ; 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.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

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

Page 5

A.6 LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)


begin: mov mov ax,@data ds,ax

; Initialization of data segment register ; printf(Enter the characters); ; Initialize the index si to 0 ; Read a character ; Is character read is end of line

printf mov next: call cmp je mov inc jmp over: mov printf mov up: mov call inc loop mov int end ;end of the program

msg1 si, 00 getchar al, 0dh over a[si], al si next cx, si msg2 si, 00 al,a[si] putchar si up ah,4ch 21h begin

; Store the character in the array ; point to next location in array a ; Read the next character ; Numer of characters to be displayed ; printf(The typed characters are:); ; Initialize si to point to beginning of a ; Obtain the character from array ; Display the current character ; Point si to next character

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 (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 7


.model small .stack

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.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 MOV MOV MOV DEC L1: MOV AX, @DATA DS, AX J, 1 CX, N CX SI, 0 ; Initial pass ; CX N-1 ; Index to the first item ; of array A ; DX = N J is the ; counter for inner loop ; AX a[si] ; If a[si] <= a[si+2])
Page 8

MOV SUB

DX, N DX, J

L2:

MOV CMP

AX, A[SI] AX, A[SI+2]

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

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 9


JLE MOV MOV MOV MOV L3 TEMP, AX BX, A[SI+2] A[SI], BX A[SI+2], AX ; goto L3 ; temp a[si] ; a[si] a[si+2]

; since AX contains ; temp ; Point to next word ; decrement inner loop ; if (DX !=0) goto L2 ; if (CX !=0) goto L1 ; i.e., goto next pass ; Stop execution at this ; point

ADD L3: DEC JNZ LOOP

SI, 2 DX L2 L1

INT

3H

END begin ;---------------------------Code segment ends------------------------------

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

Page 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 INT 21H ENDM ; Macro display a character PUTCHAR MACRO char MOV AH, 02 MOV DL, char INT 21H ENDM ;Macro to display the message PRINTF MACRO MSG MOV AH, 09H LEA DX, MSG INT 21H ENDM ;Macro to ternminat the program EXIT MACRO MOV AH, 4CH INT 21H ENDM

; Function to read a character ; AL has the character read

; Function to display a character ; Character to be displayed

; Function to display the text ; Offset value of text to be displayed

; Function to terminate the program

;--------------------------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)A. 11


;--------------------------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 ADD BL, 30H

L1:

; If greater than 9, ADD 7 ; 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 ADD BH, 30H PRINTF MSG2 PUTCHAR BH PUTCHAR BL EXIT END begin

L2:

; If greater than 9, add 7 ; Conver into ASCII by adding 30H ; Display "The ASCII Value =" ; display higher nibble of ASCII value ; display lower nibble of ASCII value

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

Page 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 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 STR1 DB LIRIL N DW $ STR1 STR2 DB 10 DUP(?) ; Memory representation ; Given string ; Length of given string ; 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 MOV AX, @DATA DS, AX
Page 12

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

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 13


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

; Copy STR1[SI] to ; STR2[DI] ; Point to previous character ; Point to next character ; Copy all characters of STR1

LOOP L1 ; Reversing of string is complete ; Check for the palindrome ; Initialization MOV SI, 0 MOV CX, N L2: MOV CMP JE PRINTF EXIT L3: INC LOOP AL, STR1[SI] AL, STR2[SI] L3 MSG2

; SI points to beginning of STR1 ; No. of characters to compare ; Compare STR1[SI] with STR2[DI]

; String is not a palindrome ; Terminate the program ; Point to next character of STR1&2 ; Compare N times

SI L2

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.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 LEA DX, BUFFER INT 21H ENDM ;Macro to display the message PRINTF MACRO MSG MOV AH, 09H LEA DX, MSG 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 INT 21H ENDM

; Function to read a string ; Address of the buffer ; AL has the character read

; Function to display the text ; Offset value of text to be displayed

; Function to terminate the program

;--------------------------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)A. 15


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 register ; It is must for string manipulation PRINTF SCANF PRINTF SCANF MSG1 BUFFER1 MSG2 BUFFER2 ; printf(Enter the first string); ; scanf(%s,a1); ; printf(Enter the second string); ; scanf(%s,a2);

; Check length of two strings MOV AL, N1 CMP AL, N2 JE L1 ; If lengths are equal compare strings PRINTF JMP MSG4 L3 ; printf(Two strings are not equal);

; Initialization before comparing L1: LEA LEA MOV CLD SI, A1 DI, A2 CL, N1 ; Offset value of first string ; Offset value of second string ; No of characters to be compared ; To increment increment index registers ; Compare as long as they are equal
Page 15

REPE CMPSB

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

A.16 LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)


JCXZ PRINTF JMP L2: L3: PRINTF PRINTF ADD PUTCHAR PRINTF ADD PUTCHAR L2 MSG4 L3 MSG3 MSG5 N1, 30H N1 MSG6 N2, 30H N2 ; If CX is zero, two strings are equal ; printf(Two strings are not equal); ; printf(Two strings are equal); ; printf(No. of characters in first string:); ; Convert to ASCII by adding 30h ; display no of characters of first string ; printf(No. of charas in second string:); ; Convert to ASCII by adding 30h ; 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 mov mov mov mov mov mov int endm

bh,07 ah,06 al,0 ch,00 cl,00 dh,24 dl,80 10h

;Attribute of character ;Activity is to scroll ;No. of lines to scroll-0 blank screen ;y-coordinate of upper left corner ;x-coordinate of upper right corner ;Number of rows; Lower right row ;Number of columns; Lower right column ; clear the screen

;Macro to set the cursor ; setcursor macro row, col mov ah, 02 mov bh, 0 mov dh, row mov dl, col int 10h endm ;Macro equvivalent to ; printf() ; scanf() pri_scn macro mov mov int endm

; row and col are the parameters ; Function to set the cursor ; Current page ; Which row ; Which column ; Set the cursor

; val function to read or display ; msg is the parameter to be displayed dx, offset msg ; DX holds offset of message to be displayed ah, 09 ; Function to display the message 21h ; Display the message val, msg

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 int 21h endm ; ;Macro display the character ; putchar() function of C ; putchar macro val mov mov int ; val is the parameter to be displayed ; Character to be displayed ; Function to write a character ; Display the character

; Function to terminate ; Terminate the program

dl,val ah,02 21h

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 prn_scn clrscr 09, msg 0ah, buffer ;printf("Enter the numbers"); ;scanf("%s",a); ; Clear the screen

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

Page 18

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 19


setcursor row, col prn_scn mov mov mov top: putchar inc loop stop end begin 09, name1 cl,n ch,0 si,00 a[si] si top ; Set the cursor ; printf(What is your name?);

; CX: no. of characters to displayed ; initial offset value w.r.t. array a ; Display the character ; Point to next character ; If not over go to top ; Terminate the program

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

Page 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 JNE ADD RET CMP JNE ADD RET DEC CMP JNE INC ADD RET BL, AL L2 RES, 1 BL, 1 L3 RES, AL AL BL, AL L4 AL RES, AL ; if k = n ; return 1;

L2:

; if k = 1 ; return n ; n = n -1 ; if k = n ; return n+1

L3:

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

Page 20

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 21


L4: PUSH PUSH CALL POP POP DEC PUSH PUSH CALL POP POP RET NCK ENDP begin: MOV MOV MOV MOV CALL AX BX NCK BX AX BX AX BX NCK BX AX ; Compute n-1Ck

; k = k -1 ; Compute n-1Ck-1

AX,@DATA DS,AX AL,N BL,K NCK

; Initialization of DS register

;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 14 8F n k nCk = 14H
6

C3 = (20)10 = (14)16
Page 21

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

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 mov ah,02 int 21h pop ax endm

; Character to display ; Function to display the character ; Display the character

;---------------Start of the code segment ----------------.code ; Procedure to convert from HEX to ASCII and display hex_asc proc mov mov div

ah,00 bl,0ah bl

; Divide by 10 to obtain unpacked BCD ; Obtain the unpacked BCD in AH and AL ; Higher nibble in AL ; Lower nibble in AH ; Convert to ASCII value ; Display the character in AL ; Display the character in AH

add putchar putchar ret endp mov int

ax,3030h al ah

disp begin:

ah, 2ch 21h

; Function to read the system time ; Read the system time : CH:CL contains hours:minutes ; DH:DL contains second:millisec
Page 22

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

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 23


mov call putchar ':' mov call putchar ':' mov call exit end al, ch disp ; Display the hour

al,cl disp

; Display the minute

al,dh disp

; Display the second

; Terminate the program begin

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

Page 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 mov ah,02 int 21h 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 putchar putchar putchar ret endp bh bl 0ah 0dh ; Display higher nibble ; Display lower nibble ; Move cursor to next line ; Move cursor to beginning of current line

; Character to be displayed ; Function to read character ; Read a character

dec_asci

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

Page 24

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 25


; Main program starts from here begin: mov al,00 top: daa call cmp je add jmp down: mov int end ah,4ch 21h begin ; Function to terminate the program ; Terminate the program dec_asci al,99h down al,01 top ; convert to decimal ; convert to ASCII and display ; Have you reached 99 ; If yes go down ; Increment count by 1 ; perform earlier task

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

Page 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 int 21h endm ;Macro to read a character getchar macro mov int endm

; Function to terminate the program ; Terminate the program

ah,01h 21h

; Function to read a character ; AL contains the character typed

;Macro to display the message printf macro msg mov ah, 09 mov dx, offset msg int 21h endm .data

; msg is the parameter to be displayed ; Function to display the message ; DX hold offset of message to be displayed ; Display the message

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)A. 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 sub add ret read begin: endp mov mov printf call mov printf call mov col, al row, al msg2 read ax, @data ds, ax msg1 read ; 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 ; Set the cursor at specified row, column ; Read a character ; Terminate the program al, 30h al, bl ; Get next ASCII value ; Convert into unpacked BCD ; Convert 2 unpacked BCD to packed BCD

setcursor row,col getchar exit 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.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 int 21h endm .data msg1 buff len fname msg2 msg3 db "Enter the file :",0ah,0dh,24h db 20 db ? db 20 dup(?) ; Max. no. of characters in the file ; Length of file name ; Space for storing the file name

; Function to terminate the program ; Terminate the program

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 prn_scn 09,msg1 0ah, buff; ; printf(Enter the file name);; ; scanf(%s,fname);

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

Page 28

LAB PROGRAMS (SAI VIDYA INSTITUTE OF TECHNOLOGY)A. 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 mov dx,offset fname int 21h jc fail prn_scn exit fail: prn_scn exit end begin 09,msg2

; Function to delete the file ; DX contains name of file ; Delete the file ; If Carray is set delete failed ; File deleted successfully ; Terminate the program ; File not deleted ; Terminate the program

09h,msg3

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

Page 29

Vous aimerez peut-être aussi