Skip to content

06

Program 1

Write a program to create a file and write text data into it using DOS interrupts.

.MODEL SMALL
.STACK 20
PRINT MACRO MSG ;Macro Declaration as print
MOV AH, 09H ;DOS function 09h: display a string of ;characters whose

offset is specified by DX.

LEA DX, MSG ;load effective address of MSG to DX
INT 21H
ENDM
READ MACRO STR ;Macro declaration as read
LEA DX, STR ;load the effective address of STR to DX
MOV AH, 0AH ;0AH = Reading a string from keyboard
INT 21H
ENDM
.DATA
MSG1 DB 0DH, 0AH, 'Enter a filename:$'
MSG2 DB 0DH, 0AH, 'File is created$'
MSG3 DB 0DH, 0AH, 'Error in File creation$'
MSG4 DB 0DH, 0AH, 'Enter a text:$'
MSG5 DB 0DH, 0AH, 'Error in File opening$'
MSG6 DB 0DH, 0AH, 'Error in writing$'
MSG7 DB 0DH, 0AH, 'Creating and writing successful$'
FNAME DB 80
DB ?
DB 80 DUP(0)
TEXT DB 80
DB ?
DB 80 DUP(?)

.CODE
START:
MOV AX,@DATA
MOV DS, AX
MOV ES, AX
PRINT MSG1
READ FNAME ;Reading a file name
LEA SI,FNAME+2 ;FNAME is add of file name,
;FNAME+1 is add of file length
MOV CL,FNAME+1 ;Getting the length of file name
MOV CH,00 ;clear the ch register
ADD SI,CX ;To move SI at next location
MOV BYTE PTR[SI],00 ;Terminating file name by Zero
LEA DX,FNAME+2 ;Starting address to write text in file
MOV AH,3CH ;Function code to create file
MOV CX,0000H ;File attributes of the new file
INT 21H
JNC SUCC1 ;If file creation is success CY=0
PRINT MSG3 ;Error msg, if file is not created
JMP EXIT
SUCC1:
PRINT MSG2 ;Success msg of file creation
PRINT MSG4 ;msg to write text in created file
READ TEXT ;Reading text to be written to file
MOV AH,3DH ;Function code to open file
MOV AL,02H ;To open file in read/write mode
LEA DX,FNAME+2
INT 21H
JNC SUCC2 ;If file open is success CY=0
PRINT MSG5 ;error msg in opening the file
JMP EXIT
SUCC2:
MOV BX,AX ;File handling returned during open
MOV AH,40H ;Function code to write text
MOV CH, 00H ;Number of characters to written into the file

MOV CL,TEXT+1
LEA DX,TEXT+2
INT 21H
JNC SUCC3 ; If file writing is success CY=0
PRINT MSG6
MOV AH,3EH ;Function code to close the file.
INT 21H
JMP EXIT
SUCC3:
PRINT MSG7 ;Disp msg, Creation and writing successful.
EXIT:
MOV AH,4CH ;To terminate the program
INT 21H
END START

Program 2

Write a program to read system time and display at the center of the screen

.MODEL SMALL
STACK 20.DATA
MS DB "PRESENT TIME IS: $"
.CODE
START:
MOV AX,@DATA
MOV DS,AX
MOV AH,00 ; Function code to clear screen
MOV AL,03H ; text video mode
INT 10H ; Calling BIOS interrupts
MOV AH,02 ; Function code to set Cursor position.
MOV BH,0 ; video page number
MOV DH,12 ; Row number
MOV DL,30 ; Column number
INT 10H ; Calling BIOS interrupts
LEA DX,MS ; Display the msg
MOV AH,09
INT 21H

MOV AH,2CH ; Function code to read the system time, Hour in CH, ; Minute in

CL, Second in DH

INT 21H
MOV AL,CH ; To display Hour
CALL DISP ; Calling DISP procedure for displaying Hour
MOV DL,':' ; to display ‘:’ after Hour
MOV AH,02 ; display the content of DL
INT 21H
MOV AL,CL ; To display Minute
CALL DISP ; Calling DISP procedure for displaying Minute
MOV DL,':' ; to display ‘:’ after Minute
MOV AH,02 ; WRITE CHARACTER TO STANDARD OUTPUT
INT 21H
MOV AL,DH ; To display Second
CALL DISP ; Calling DISP procedure for displaying second
MOV AH,02 ; WRITE CHARACTER TO STANDARD OUTPUT
MOV BH,0 ; video page number
MOV DH,24 ; ROW
MOV DL,00 ; COLUMN
INT 10H ; Calling BIOS interrupts
MOV AH,4CH ; Exit the program to OS
INT 21H
DISP PROC NEAR
AAM ; stands for ASCII adjust for Multiplication or BCD

; Adjust after Multiply

ADD AX,3030H
MOV BX,AX ; move AX to BX
MOV DL,BH ; move BH to DL
MOV AH,02 ; WRITE CHARACTER TO STANDARD OUTPUT
INT 21H
MOV DL,BL ; move BL to DL
MOV AH,02 ; WRITE CHARACTER TO STANDARD OUTPUT
INT 21H
RET ; Return
ENDP ; end procedure
END START

Program 3

Write a program to read system date and display in this format DD-MM-YEAR.

.MODEL SMALL
.STACK 20
PRINT MACRO MSG
MOV AH,09H
MOV DX,OFFSET MSG
INT 21H
ENDM
.DATA
DAY DB ?, ?, '-'
MONTH DB ?, ?, '-'
YEAR DB ?, ?, ?, ?, '$'
.CODE
START:
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
MOV AH,2AH ; Function code to get Date
INT 21H ; Call DOS service
PUSH CX ; Saving year
PUSH DX ; Saving Day and Month
MOV AL,DL ; move DL to AL
LEA SI,DAY ; load effective address of Day to SI
MOV AH,00H ; Move 00H to AH
CALL CONV ; To call CONV subroutine
POP DX ; Putting ASCII value in memory
MOV AL,DH ; move DH to AL
LEA SI,MONTH ; Load the effective address of Month to SI
MOV AH,00H ; move 00H to AL
CALL CONV ; To convert month to ASCII
POP AX ; Putting ASCII value in memory
LEA SI,YEAR ; Load effective address of YEAR to SI
CALL CONV ; To convert year to ASCII

PRINT DAY ; To display Date
MOV AH,4CH ; Exit program
INT 21H
CONV PROC NEAR ; Procedure convert Hexadecimal
MOV CX,0000H ; Day, Month and Year to BCD
MOV BX,000AH ; and ASCII for display
NEXT:
MOV DX,0000 ; move 0000 to DX
DIV BX ; Separating the digits
ADD DL,30H ; Converting BCD to ASCII
PUSH DX
INC CX ; increment CX
CMP AX,000AH
JGE NEXT
ADD AL,30H
MOV [SI],AL
UP:
POP AX ; Putting ASCII value in memory
INC SI ; increment SI
MOV [SI],AL
LOOP UP
RET ; return
CONV ENDP ; end of the procedure conv
END START
Last Updated: 2023-01-25 ; Contributors: AhmedThahir

Comments