Download - MS DOS File IO
MS DOS File IO
Text chapter 13
DateTimeC:\MASM615>make16 datetime Assembling: datetime.asm Volume in drive C has no label. Volume Serial Number is 07D2-0208
Directory of C:\masm615
12/08/2001 11:40 PM 1,514 DateTime.asm11/13/2006 01:38 PM 3,018 datetime.obj11/13/2006 01:38 PM 10,196 datetime.lst11/13/2006 01:38 PM 7,652 datetime.exe 4 File(s) 22,380 bytes 0 Dir(s) 4,235,657,216 bytes freePress any key to continue . . .
C:\MASM615>datetimeDate: 11-13-2006, Time: 13:38:55
C:\MASM615>
DateTime Display the date:
mov dx,OFFSET str1call WriteStringmov ah,2Ah; get system dateint 21hmovzx eax,dh ; monthcall WriteDecINVOKE Write,'-'movzx eax,dl ; daycall WriteDecINVOKE Write,'-'movzx eax,cx ; yearcall WriteDec
; Display the time:mov dx,OFFSET str2call WriteStringmov ah,2Ch; get system timeint 21hmovzx eax,ch ; hourscall WritePaddedDecINVOKE Write,':'movzx eax,cl ; minutescall WritePaddedDecINVOKE Write,':'movzx eax,dh ; secondscall WritePaddedDeccall Crlf
DateTimeWrite PROC char:BYTE; Display a single character.;---------------------------------------------
push eaxpush edxmov ah,2mov dl,charint 21hpop edxpop eaxret
Write ENDP;---------------------------------------------WritePaddedDec PROC; Display unsigned integer in EAX, padding; to two digit positions with a leading zero.;---------------------------------------------
.IF eax < 10 push eax push edx mov ah,2 mov dl,'0' int 21h pop edx pop eax.ENDIF
call WriteDecret
WritePaddedDec ENDP
Buffered Keyboard Input (Keybd.asm)Test function 3Fh, read from file or
devicewith the keyboard. Flush the bufferINCLUDE Irvine16.inc
.datafirstName BYTE 15 DUP(?),0lastName BYTE 30 DUP(?),0
.codemain PROC mov ax,@data mov ds,ax
; Input the first name:mov ah,3Fhmov bx,0 ; keyboardmov cx,LENGTHOF firstNamemov dx,OFFSET firstNameint 21h
; Disable the following line to see what happens; when the buffer is not flushed:
call FlushBuffer
Buffered Keyboard Input (Keybd.asm); Input the last name:
mov ah,3Fhmov bx,0 ; keyboardmov cx,LENGTHOF lastNamemov dx,OFFSET lastNameint 21h
quit:call Crlf
exitmain ENDP;------------------------------------------FlushBuffer PROC; Flush the standard input buffer.; Receives: nothing. Returns: nothing;-----------------------------------------.dataoneByte BYTE ?.code
pushaL1: mov ah,3Fh ; read file/device
mov bx,0 ; keyboard handle mov cx,1 ; one byte mov dx,OFFSET oneByte ; save it here int 21h ; call MS-DOS cmp oneByte,0Ah ; end of line yet? jne L1 ; no: read another
poparet
FlushBuffer ENDPEND main
Readfile (run… displays file)
C:\MASM615\EXAMPLES\CH13>readfileTITLE Read a text file (Readfile.asm)
; Last update: 9/11/01
INCLUDE Irvine16.inc
.dataBufSize = 5000
fileName BYTE "myfile.txt",0inHandle WORD ?buffer BYTE BufSize DUP(?)bytesRead WORD ?
.codemain PROC mov ax,@data mov ds,ax
Readfile.dataBufSize = 5000infile BYTE "my_text_file.txt",0outfile BYTE "my_output_file.txt",0inHandle WORD ?outHandle WORD ?buffer BYTE BufSize DUP(?)bytesRead WORD ?.codemain PROC mov ax,@data mov ds,ax; Open the input file
mov ax,716Ch ; extended create or openmov bx,0 ; mode = read-onlymov cx,0 ; normal attributemov dx,1 ; action: openmov si,OFFSET infileint 21h ; call MS-DOSjc quit ; quit if errormov inHandle,ax
readfile
; Read the input filemov ah,3Fh ; read file or devicemov bx,inHandle ; file handlemov cx,BufSize ; max bytes to readmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if errormov bytesRead,ax
; Display the buffermov ah,40h ; write file or devicemov bx,1 ; console output handlemov cx,bytesRead ; number of bytesmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if error
; Close the filemov ah,3Eh ; function: close filemov bx,inHandle ; input file handleint 21h ; call MS-DOSjc quit ; quit if error
Readfile; Create the output file
mov ax,716Ch ; extended create or openmov bx,1 ; mode = write-onlymov cx,0 ; normal attributemov dx,12h ; action: create/truncatemov si,OFFSET outfileint 21h ; call MS-DOSjc quit ; quit if errormov outHandle,ax ; save handle
; Write buffer to new filemov ah,40h ; write file or devicemov bx,outHandle ; output file handlemov cx,bytesRead ; number of bytesmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if error
; Close the filemov ah,3Eh ; function: close filemov bx,outHandle ; output file handleint 21h ; call MS-DOS
Extended Open/Create (Fileio.asm)excerpts in text
.dataDate WORD ?handle WORD ?actionTaken WORD ?FileName BYTE "long_filename.txt",0NewFile BYTE "newfile.txt",0
.codemain PROC
mov ax,@datamov ds,ax
;Create new file, fail if it already exists:mov ax,716Ch ; Extended Open/Createmov bx,2 ; read-writemov cx,0 ; normal attributemov dx,10h ; action: createmov si,OFFSET NewFileint 21hjc failedmov handle,ax ; file handlemov actionTaken,cx ; action taken to open file
Fileio.asm;Open existing file
mov ax,716Ch ; Extended Open/Createmov bx,0 ; read-onlymov cx,0 ; normal attributemov dx,1 ; open existing filemov si,OFFSET Filenameint 21hjc failedmov handle,ax ; file handlemov actionTaken,cx ; action taken to open file
;Create new file or truncate existing file:mov ax,716Ch ; Extended Open/Createmov bx,2 ; read-writemov cx,0 ; normal attributemov dx,10h + 02h ; action: create + truncatemov si,OFFSET NewFileint 21hjc failedmov handle,ax ; file handlemov actionTaken,cx ; action taken to open file
failed:exit
main ENDP
Readfile.asm: Read, display, and copy a text file (containing asm prog)
C:\MASM615>make16 readfile Assembling: readfile.asm Volume in drive C has no label. Volume Serial Number is 07D2-0208 Directory of C:\masm615
12/09/2001 10:47 AM 1,928 Readfile.asm11/13/2006 01:34 PM 2,968 readfile.obj11/13/2006 01:34 PM 10,906 readfile.lst11/13/2006 01:34 PM 12,596 readfile.exe 4 File(s) 28,398 bytes 0 Dir(s) 4,239,589,376 bytes freePress any key to continue . . .C:\MASM615>readfileTITLE Read a text file (Readfile.asm)
; Last update: 9/11/01INCLUDE Irvine16.inc.dataBufSize = 5000
fileName BYTE "myfile.txt",0inHandle WORD ?buffer BYTE BufSize DUP(?)bytesRead WORD ?.codemain PROC mov ax,@data mov ds,ax
data
INCLUDE Irvine16.inc
.dataBufSize = 5000infile BYTE "my_text_file.txt",0outfile BYTE "my_output_file.txt",0inHandle WORD ?outHandle WORD ?buffer BYTE BufSize DUP(?)bytesRead WORD ?
Some of code segmentOpen the input file
mov ax,716Ch ; extended create or openmov bx,0 ; mode = read-onlymov cx,0 ; normal attributemov dx,1 ; action: openmov si,OFFSET infileint 21h ; call MS-DOSjc quit ; quit if errormov inHandle,ax
; Read the input filemov ah,3Fh ; read file or devicemov bx,inHandle ; file handlemov cx,BufSize ; max bytes to readmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if errormov bytesRead,ax
; Display the buffermov ah,40h ; write file or devicemov bx,1 ; console output handlemov cx,bytesRead ; number of bytesmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if error
Some of code segment; Close the file
mov ah,3Eh ; function: close filemov bx,inHandle ; input file handleint 21h ; call MS-DOSjc quit ; quit if error
; Create the output filemov ax,716Ch ; extended create or openmov bx,1 ; mode = write-onlymov cx,0 ; normal attributemov dx,12h ; action: create/truncatemov si,OFFSET outfileint 21h ; call MS-DOSjc quit ; quit if errormov outHandle,ax ; save handle
; Write buffer to new filemov ah,40h ; write file or devicemov bx,outHandle ; output file handlemov cx,bytesRead ; number of bytesmov dx,OFFSET buffer ; buffer pointerint 21hjc quit ; quit if error
; Close the filemov ah,3Eh ; function: close filemov bx,outHandle ; output file handleint 21h ; call MS-DOS
BinFile: Create a binary file containing an array
of doublewordsC:\MASM615>binfile000009E2, 000003F6, 00000E87, 00000471, 000001DF, 00000C10, 0000060A, 00000E78,00000219, 00000072, 000009B4, 00000109, 000001B4, 00000BB0, 000009C9, 00000B59,00000315, 0000069E, 00000BCE, 00000CDB, 000007DF, 00000C51, 00000E86, 00000944,000004F6, 00000E1C, 00000DF5, 00000C86, 0000067E, 00000793, 0000075F, 00000ED7,000003DB, 0000028B, 00000D49, 000008AA, 000003B2, 00000B16, 00000B76, 000006B4,00000FE6, 0000038A, 00000AEA, 00000DE7, 0000099B, 0000087A, 000005E9, 00000F79,00000D36, 000004BB, Press any key to continue...
000009E2, 000003F6, 00000E87, 00000471, 000001DF, 00000C10, 0000060A, 00000E78,00000219, 00000072, 000009B4, 00000109, 000001B4, 00000BB0, 000009C9, 00000B59,00000315, 0000069E, 00000BCE, 00000CDB, 000007DF, 00000C51, 00000E86, 00000944,000004F6, 00000E1C, 00000DF5, 00000C86, 0000067E, 00000793, 0000075F, 00000ED7,000003DB, 0000028B, 00000D49, 000008AA, 000003B2, 00000B16, 00000B76, 000006B4,00000FE6, 0000038A, 00000AEA, 00000DE7, 0000099B, 0000087A, 000005E9, 00000F79,00000D36, 000004BB,
binfile
.datamyArray DWORD 50 DUP(?)
fileName BYTE "binary array file.bin",0fileHandle WORD ?commaStr BYTE ", ",0
; Set CreateFile to zero if you just want to; read and display the existing binary file.CreateFile = 1
• .IF CreateFile EQ 1• call FillTheArray• call DisplayTheArray• call CreateTheFile• call WaitMsg• call Crlf• .ENDIF• call ReadTheFile• call DisplayTheArray
• quit:• call Crlf• exit• main ENDP
binfile;------------------------------------------------------ReadTheFile PROC;; Open and read the binary file.; Receives: nothing. Returns: nothing;------------------------------------------------------
mov ax,716Ch ; extended file openmov bx,0 ; mode: read-onlymov cx,0 ; attribute: normalmov dx,1 ; open existing filemov si,OFFSET fileName ; filenameint 21h ; call MS-DOSjc quit ; quit if errormov fileHandle,ax ; save handle
; Read the input file, then close the file.mov ah,3Fh ; read filemov bx,fileHandle ; file handlemov cx,SIZEOF myArray ; max bytes to readmov dx,OFFSET myArray ; buffer pointerint 21hjc quit ; quit if errormov ah,3Eh ; function: close filemov bx,fileHandle ; output file handleint 21h ; call MS-DOS
quit:ret
ReadTheFile ENDP
binfile;------------------------------------------------------DisplayTheArray PROC;; Display the array; Receives: nothing. Returns: nothing;------------------------------------------------------
mov CX,LENGTHOF myArraymov si,0
L1:mov eax,myArray[si] ; get a numbercall WriteHex; display the numbermov edx,OFFSET commaStr ; display a commacall WriteStringadd si,TYPE myArray ; next array positionloop L1ret
DisplayTheArray ENDP;------------------------------------------------------FillTheArray PROC; Fill the array with random integers.; Receives: nothing. Returns: nothing;------------------------------------------------------
mov CX,LENGTHOF myArraymov si,0
L1:mov eax,1000h ; generate random integerscall RandomRange ; between 0 - 999 in EAXmov myArray[si],eax ; store in the arrayadd si,TYPE myArray ; next array positionloop L1ret
FillTheArray ENDP
binfile;------------------------------------------------------CreateTheFile PROC;; Create a file containing binary data; Receives: nothing. Returns: nothing;------------------------------------------------------
mov ax,716Ch ; create filemov bx,1 ; mode: write onlymov cx,0 ; normal filemov dx,12h ; action: create/truncatemov si,OFFSET fileName ; filenameint 21h ; call MS-DOSjc quit ; quit if errormov fileHandle,ax ; save handle
; Write integer array to the file.mov ah,40h ; write file or devicemov bx,fileHandle ; output file handlemov cx,SIZEOF myArray ; number of bytesmov dx,OFFSET myArray ; buffer pointerint 21hjc quit ; quit if error
; Close the file.mov ah,3Eh ; function: close filemov bx,fileHandle ; output file handleint 21h ; call MS-DOS
quit:ret
CreateTheFile ENDP