MS DOS File IO Text chapter 13. DateTime C:\MASM615>make16 datetime Assembling: datetime.asm Volume in drive C has no label. Volume Serial Number is 07D2-0208.

Slides:



Advertisements
Similar presentations
Link list/file stamps/clusters Odds and ends remaining for test 2.
Advertisements

DOS and BIOS Interrupts DOS and BIOS interrupts are used to perform some very useful functions, such as displaying data to the monitor, reading data from.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Writing and reading files. Creating a file on a disk Get a file handle from system Use INT 21H function 3C to create a directory entry for the file Use.
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
MSDOS 16-bit programming Old chpt 13 Current chapt 14.
Assembly Language for Intel-Based Computers Chapter 15: BIOS-Level Programming (c) Pearson Education, All rights reserved. You may modify and.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
MSDOS 16-bit programming
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
Flow Control Instructions
Assembly Language for Intel-Based Computers Chapter 13: 16-Bit MS-DOS Programming Kip R. Irvine.
Assembly programming A little background on using the software.
Putting together a mouse driven application Divide and conquer.
Kip Irvine: Assembly Language for Intel-Based Computers
Irvine, Kip R. Assembly Language For Intel-Based Computers.data string db "This is a string." COUNT = ($–string) ; calculate string length.code mov cx,COUNT.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 13: 16-Bit MS-DOS Programming (c) Pearson Education, All rights reserved. You may.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CS2422 Assembly Language & System Programming October 24, 2006.
Irvine, Kip R. Assembly Language For Intel-Based Computers XADD Instruction.code mov ax,1000h mov bx,2000h ; AX = 1000h, BX = 2000h xadd ax,bx ; AX = 3000h,
Coding.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
Factorial of a number data segment x1 db 4 fact dw ? data ends
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 13: 16-Bit MS-DOS Programming (c) Pearson Education, All rights reserved.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals Assembling, Linking and Running Programs Example Programs.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#7)
BIOS1 Basic Input Output System BIOS BIOS refers to a set of procedures or functions that enable the programmer have access to the hardware of the computer.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 13: 16-Bit MS-DOS Programming (c) Pearson Education, All rights reserved.
Assembly Language for x86 Processors 6th Edition
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
BIOS and DOS Programming in DOS INT 10 and 21H. Interrupts There are some extremely useful subroutines within BIOS or DOS that are available to the user.
Multiplication and Division Instructions & the 0Ah function.
Assembly Language for Intel-Based Computers Chapter 13: 16-Bit MS-DOS Programming Kip R. Irvine.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
ICS312 Set 14 MACROS. Macros The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions.
File I/O MS-DOS Interrupt 21h has many functions dealing with file and directory I/O services. Both MS-DOS and MS_Windows use 16- bit integers called HANDLES.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#9) By Dr. Syed Noman.
ASSEMBLY LANGUAGE FOR INTEL-BASED COMPUTERS, PROCEDURES.
Writing and Reading Files Methods for processing disk files File Control Blocks (FCBs) –Supported by DOS –Can address drives and filenames.
Computer Organization and Assembly Languages Yung-Yu Chuang 2006/11/13
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
CHAPTER 5: PROCEDURES ASSEMBLY LANGUAGE FOR INTEL- BASED COMPUTERS, 5 TH EDITION (c) Pearson Education, All rights reserved. You may modify and copy.
In Class Program Write, assemble and test a program: –Use the DB directive to define the following list of numbers and name it array: 31h, 32h, 33h, 34h.
Chapter Bit MS-DOS Programming Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 18 Linking to External Library The Book’s Link Library Stack Operations.
Irvine, Kip R. Assembly Language for Intel-Based Computers. Chapter 7: Integer Arithmetic Slides to Accompany Assembly Language for Intel-Based Computers,
Procedure Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/27 with slides by Kip Irvine.
Control Structure vs. Assembly Language NASM. If-then-else If conditional then then_actions jump to endif else else_actions endif.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 13: 16-Bit MS-DOS Programming Interrupts (c) Pearson Education, All rights reserved.
Assembly Language Lecture 2. Lecture Outline Program Structure Memory models Data Segment Stack Segment Code Segment Input and Output Instructions INT.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
Chapter 5: Procedures and Interrupts
Computer Architecture and Assembly Language
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Microprocessor and Assembly Language
Chapter 4: Instructions
Assembly Language Programming
Unit:08 Software Interrupts
By Nasser Halasa Assembly Language.
Presentation transcript:

MS DOS File IO Text chapter 13

DateTime C:\MASM615>make16 datetime Assembling: datetime.asm Volume in drive C has no label. Volume Serial Number is 07D Directory of C:\masm615 12/08/ :40 PM 1,514 DateTime.asm 11/13/ :38 PM 3,018 datetime.obj 11/13/ :38 PM 10,196 datetime.lst 11/13/ :38 PM 7,652 datetime.exe 4 File(s) 22,380 bytes 0 Dir(s) 4,235,657,216 bytes free Press any key to continue... C:\MASM615>datetime Date: , Time: 13:38:55 C:\MASM615>

DateTime Display the date: mov dx,OFFSET str1 call WriteString mov ah,2Ah; get system date int 21h movzx eax,dh; month call WriteDec INVOKE Write,'-' movzx eax,dl; day call WriteDec INVOKE Write,'-' movzx eax,cx; year call WriteDec ; Display the time: mov dx,OFFSET str2 call WriteString mov ah,2Ch; get system time int 21h movzx eax,ch; hours call WritePaddedDec INVOKE Write,':' movzx eax,cl; minutes call WritePaddedDec INVOKE Write,':' movzx eax,dh; seconds call WritePaddedDec call Crlf

DateTime Write PROC char:BYTE ; Display a single character. ; push eax push edx mov ah,2 mov dl,char int 21h pop edx pop eax ret 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 WriteDec ret WritePaddedDec ENDP

Buffered Keyboard Input (Keybd.asm) Test function 3Fh, read from file or devicewith the keyboard. Flush the buffer INCLUDE Irvine16.inc.data firstName BYTE 15 DUP(?),0 lastName BYTE 30 DUP(?),0.code main PROC mov mov ds,ax ; Input the first name: mov ah,3Fh mov bx,0; keyboard mov cx,LENGTHOF firstName mov dx,OFFSET firstName int 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,3Fh mov bx,0; keyboard mov cx,LENGTHOF lastName mov dx,OFFSET lastName int 21h quit: call Crlf exit main ENDP ; FlushBuffer PROC ; Flush the standard input buffer. ; Receives: nothing. Returns: nothing ; data oneByte BYTE ?.code pusha L1: 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 popa ret FlushBuffer ENDP END main

Readfile (run… displays file) C:\MASM615\EXAMPLES\CH13>readfile TITLE Read a text file (Readfile.asm) ; Last update: 9/11/01 INCLUDE Irvine16.inc.data BufSize = 5000 fileName BYTE "myfile.txt",0 inHandle WORD ? buffer BYTE BufSize DUP(?) bytesRead WORD ?.code main PROC mov mov ds,ax

Readfile.data BufSize = 5000 infile BYTE "my_text_file.txt",0 outfile BYTE "my_output_file.txt",0 inHandle WORD ? outHandle WORD ? buffer BYTE BufSize DUP(?) bytesRead WORD ?.code main PROC mov mov ds,ax ; Open the input file mov ax,716Ch ; extended create or open mov bx,0 ; mode = read-only mov cx,0; normal attribute mov dx,1; action: open mov si,OFFSET infile int 21h ; call MS-DOS jc quit; quit if error mov inHandle,ax

readfile ; Read the input file mov ah,3Fh; read file or device mov bx,inHandle; file handle mov cx,BufSize; max bytes to read mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error mov bytesRead,ax ; Display the buffer mov ah,40h; write file or device mov bx,1; console output handle mov cx,bytesRead; number of bytes mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error ; Close the file mov ah,3Eh ; function: close file mov bx,inHandle; input file handle int 21h ; call MS-DOS jc quit; quit if error

Readfile ; Create the output file mov ax,716Ch ; extended create or open mov bx,1 ; mode = write-only mov cx,0; normal attribute mov dx,12h; action: create/truncate mov si,OFFSET outfile int 21h ; call MS-DOS jc quit; quit if error mov outHandle,ax; save handle ; Write buffer to new file mov ah,40h; write file or device mov bx,outHandle; output file handle mov cx,bytesRead; number of bytes mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error ; Close the file mov ah,3Eh ; function: close file mov bx,outHandle; output file handle int 21h ; call MS-DOS

Extended Open/Create (Fileio.asm) excerpts in text.data Date WORD ? handle WORD ? actionTaken WORD ? FileName BYTE "long_filename.txt",0 NewFile BYTE "newfile.txt",0.code main PROC mov mov ds,ax ;Create new file, fail if it already exists: mov ax,716Ch ; Extended Open/Create mov bx,2; read-write mov cx,0 ; normal attribute mov dx,10h; action: create mov si,OFFSET NewFile int 21h jc failed mov handle,ax ; file handle mov actionTaken,cx ; action taken to open file

Fileio.asm ;Open existing file mov ax,716Ch ; Extended Open/Create mov bx,0; read-only mov cx,0 ; normal attribute mov dx,1; open existing file mov si,OFFSET Filename int 21h jc failed mov handle,ax ; file handle mov actionTaken,cx ; action taken to open file ;Create new file or truncate existing file: mov ax,716Ch ; Extended Open/Create mov bx,2; read-write mov cx,0 ; normal attribute mov dx,10h + 02h; action: create + truncate mov si,OFFSET NewFile int 21h jc failed mov handle,ax ; file handle mov 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 07D Directory of C:\masm615 12/09/ :47 AM 1,928 Readfile.asm 11/13/ :34 PM 2,968 readfile.obj 11/13/ :34 PM 10,906 readfile.lst 11/13/ :34 PM 12,596 readfile.exe 4 File(s) 28,398 bytes 0 Dir(s) 4,239,589,376 bytes free Press any key to continue... C:\MASM615>readfile TITLE Read a text file (Readfile.asm) ; Last update: 9/11/01 INCLUDE Irvine16.inc.data BufSize = 5000 fileName BYTE "myfile.txt",0 inHandle WORD ? buffer BYTE BufSize DUP(?) bytesRead WORD ?.code main PROC mov mov ds,ax

data INCLUDE Irvine16.inc.data BufSize = 5000 infile BYTE "my_text_file.txt",0 outfile BYTE "my_output_file.txt",0 inHandle WORD ? outHandle WORD ? buffer BYTE BufSize DUP(?) bytesRead WORD ?

Some of code segment Open the input file mov ax,716Ch ; extended create or open mov bx,0 ; mode = read-only mov cx,0; normal attribute mov dx,1; action: open mov si,OFFSET infile int 21h ; call MS-DOS jc quit; quit if error mov inHandle,ax ; Read the input file mov ah,3Fh; read file or device mov bx,inHandle; file handle mov cx,BufSize; max bytes to read mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error mov bytesRead,ax ; Display the buffer mov ah,40h; write file or device mov bx,1; console output handle mov cx,bytesRead; number of bytes mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error

Some of code segment ; Close the file mov ah,3Eh ; function: close file mov bx,inHandle; input file handle int 21h ; call MS-DOS jc quit; quit if error ; Create the output file mov ax,716Ch ; extended create or open mov bx,1 ; mode = write-only mov cx,0; normal attribute mov dx,12h; action: create/truncate mov si,OFFSET outfile int 21h ; call MS-DOS jc quit; quit if error mov outHandle,ax; save handle ; Write buffer to new file mov ah,40h; write file or device mov bx,outHandle; output file handle mov cx,bytesRead; number of bytes mov dx,OFFSET buffer; buffer pointer int 21h jc quit; quit if error ; Close the file mov ah,3Eh ; function: close file mov bx,outHandle; output file handle int 21h ; call MS-DOS

BinFile: Create a binary file containing an array of doublewords C:\MASM615>binfile E2, F6, 00000E87, , DF, 00000C10, A, 00000E78, , , B4, , B4, 00000BB0, C9, 00000B59, , E, 00000BCE, 00000CDB, DF, 00000C51, 00000E86, , F6, 00000E1C, 00000DF5, 00000C86, E, , F, 00000ED7, DB, B, 00000D49, AA, B2, 00000B16, 00000B76, B4, 00000FE6, A, 00000AEA, 00000DE7, B, A, E9, 00000F79, 00000D36, BB, Press any key to continue E2, F6, 00000E87, , DF, 00000C10, A, 00000E78, , , B4, , B4, 00000BB0, C9, 00000B59, , E, 00000BCE, 00000CDB, DF, 00000C51, 00000E86, , F6, 00000E1C, 00000DF5, 00000C86, E, , F, 00000ED7, DB, B, 00000D49, AA, B2, 00000B16, 00000B76, B4, 00000FE6, A, 00000AEA, 00000DE7, B, A, E9, 00000F79, 00000D36, BB,

binfile.data myArray DWORD 50 DUP(?) fileName BYTE "binary array file.bin",0 fileHandle 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 open mov bx,0; mode: read-only mov cx,0 ; attribute: normal mov dx,1; open existing file mov si,OFFSET fileName; filename int 21h ; call MS-DOS jc quit; quit if error mov fileHandle,ax; save handle ; Read the input file, then close the file. mov ah,3Fh; read file mov bx,fileHandle; file handle mov cx,SIZEOF myArray; max bytes to read mov dx,OFFSET myArray; buffer pointer int 21h jc quit; quit if error mov ah,3Eh ; function: close file mov bx,fileHandle; output file handle int 21h ; call MS-DOS quit: ret ReadTheFile ENDP

binfile ; DisplayTheArray PROC ; ; Display the array ; Receives: nothing. Returns: nothing ; mov CX,LENGTHOF myArray mov si,0 L1: mov eax,myArray[si]; get a number call WriteHex; display the number mov edx,OFFSET commaStr; display a comma call WriteString add si,TYPE myArray; next array position loop L1 ret DisplayTheArray ENDP ; FillTheArray PROC ; Fill the array with random integers. ; Receives: nothing. Returns: nothing ; mov CX,LENGTHOF myArray mov si,0 L1: mov eax,1000h; generate random integers call RandomRange; between in EAX mov myArray[si],eax; store in the array add si,TYPE myArray; next array position loop L1 ret FillTheArray ENDP

binfile ; CreateTheFile PROC ; ; Create a file containing binary data ; Receives: nothing. Returns: nothing ; mov ax,716Ch ; create file mov bx,1; mode: write only mov cx,0 ; normal file mov dx,12h; action: create/truncate mov si,OFFSET fileName; filename int 21h ; call MS-DOS jc quit; quit if error mov fileHandle,ax; save handle ; Write integer array to the file. mov ah,40h; write file or device mov bx,fileHandle; output file handle mov cx,SIZEOF myArray; number of bytes mov dx,OFFSET myArray; buffer pointer int 21h jc quit; quit if error ; Close the file. mov ah,3Eh ; function: close file mov bx,fileHandle; output file handle int 21h ; call MS-DOS quit: ret CreateTheFile ENDP