File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.

Slides:



Advertisements
Similar presentations
BINARY & HEX I/O. Binary input : read in a binary number from keyboard, followed by a carriage return. character strings of 1’s & 0’ we need to convert.
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.
Physics 413 Chapter 8 IBM PC Assemblers An assembler is a program that takes your assembly language program and converts the instructions into op-codes.
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.
ICS312 Set 6 Operands. Basic Operand Types (1) Register Operands. An operand that refers to a register. MOV AX, BX ; moves contents of register BX to.
More about procedures and Video Processing. Lesson plan Review existing concepts More about procedures and boolean expression Video processing.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
Direct video practice and Keyboard Operations
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
CS2422 Assembly Language & System Programming November 2, 2006.
Chapter 7 Programming with DOS and BIOS Function Calls Objectives: The use of DOS and BIOS function call How to read the PC’s keyboard How to send text.
Chapter 9 Using Disks and Files Objectives: The difference between Floppy and Hard Disks What the Boot Sector is for What the File Allocation Table is.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Flow Control Instructions
Kip Irvine: Assembly Language for Intel-Based Computers
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
8.4 Instruction Execution Times TOBIN PROC FAR SUB AX,AX MOV DX,AX MOV CX,4 NEXTD: PUSH CX SUB BP,BP MOV CX,4 GETNUM: RCL BX,1 RCL BP,1 LOOP GETNUM.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
Assembly Programming Timothy C. Rice Jr., MIT. OUTLINE Basic Structure Exit to Dos Print Character Clear Screen Change BG & FG Color Set Curser Location.
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 ;
Topics Control Flow Structure – Conditional Jump – Unconditional Jump Control Flow Structures – IF-THEN – IF-THEN-ELSE – CASE Branches with Compound Conditions.
Lecture 11 Last notes on interrupts and exam review Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
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.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
Programming the Microprocessor A Course in Microprocessor Electrical Engineering Dept. University of Indonesia.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
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.
1 Screen and Keyboard Operations Suthida Chaichomchuen
Multiplication and Division Instructions & the 0Ah function.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
“It was the night before midterm”. Midterm rule (March 16 nd, 2005) - Student ID is required. Open books, note exam - Don’t: - Leave the exam room after.
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.
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
Video systems. Lesson plan Review the code for the previous exercise Video systems Review for midterm exam.
8086 Microprocessor Interrupts By: Vijay Kumar. K Reference From Slide Share.
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
Writing and Reading Files Methods for processing disk files File Control Blocks (FCBs) –Supported by DOS –Can address drives and filenames.
10H Interrupt. Option 0H – Sets video mode. Registers used: – AH = 0H – AL = Video Mode. 3H - CGA Color text of 80X25 7H - Monochrome text of 80X25 Ex:
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
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.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA 1 Assembly Language Programming.
Assembly language programming
Format of Assembly language
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Computer Organization & Assembly Language Chapter 3
Additional Assembly Programming Concepts
ADDRESSING MODES.
Lecture 4 Control Flow Structures (LOOPS)
ADDRESSING MODES.
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
BIC 10503: COMPUTER ARCHITECTURE
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
CS-401 Computer Architecture & Assembly Language Programming
Shift & Rotate Instructions)
Symbolic Instruction and Addressing
Unit:08 Software Interrupts
UNIT-II Assembly Language Programs Involving Logical
Chapter 6 –Symbolic Instruction and Addressing
By Nasser Halasa Assembly Language.
Presentation transcript:

File Operations

FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing a file means copying data from memory to a file Rewriting a file means replacing a file's content with other data

File Handle When a file is created or opened in a program, DOS assigns it a unique number called the file handle This number is used to identify the file, so the program must save that information Note: a file stream in C/C++/Java is the same thing as a file handle in Assembler

File Handle (Cont.) There are 5 predefined file handles: 0 keyboard 1 screen 2 error output - usually the screen 3 COM1 4 printer Additional user-defined files are assigned file handles starting with 5, 6, 7, etc.

File Errors There can be many types of errors in using INT 21h file handling functions. Each error type is identified by a code number In the following functions, if an error occurs the CF is set and the code number is stored in AX

A list of common file- handling errors Error Code: Meaning: 1 invalid function number 2 file not found 3 path not found 5 access denied (file may already be open by another process) 6 invalid file handle F invalid drive specified

DOS FILE HANDLE INSTRUCTIONS As with the DOS I/O functions, put the function number into the AH Register and execute INT 21h.

OPENING A FILE A file must be created before it can be used To create a new file, or to rewrite an existing file, the user provides a filename and an attribute DOS returns a file handle

OPENING A FILE (Cont.1) INT 21h, Function 3Ch DOS function: Open a new file/rewrite a file Input: AH = 3Ch DS:DX = address of filename (an ASCIIZ string ending with a zero byte) CL = attribute (= 0 for write only, = 1 for read only, =2 for read & write) Output: If successful, AX = file handle Error: if CF = 1, AX = error code (3, 4, or 5)

OPENING A FILE (Cont.2) The filename may include a path: A:\PROGS\PROG1.ASM Possible errors: 1.Path doesn't exist 2.Access denied --- directory is full or file is read-only

OPENING A FILE (Cont.3) Example: Write instructions to open a new read-only file called "FILE1" FNAME DB 'FILE1', 0 HANDLE DW ?.CODE MOV MOV DS, AX ; initialize DS MOV AH, 3CH ; open file function LEA DX, FNAME ; copy address to DX MOV CL, 1 ; read-only attribute INT 21H ; open the file JC OPEN_ERROR ; jump if error MOV HANDLE, AX ; handle or err code

OPENING A FILE (Cont.4) INT 21h, function 3Dh DOS Function: Open an existing file Input: AH = 3Dh DS:DX = address of filename (an ASCII string ending with a zero byte) AL = access code: 0 = open for reading (where attribute is 1 or 2) 1 = open for writing (where attribute is 0 or 2) 2 = open for read/write (where attribute is 2) Output: If successful, AX = file handle If CF = 1, AX = error code (2, 4, 5, 12)

CLOSING FILES A file should be closed after it has been processed. This frees the file handle for use with another file If the file is being written, closing the file causes any data remaining in memory to be written to the file, and the file's time, date, and size will be updated in the directory entry

CLOSING FILES (Cont.1) INT 21h, function 3Eh DOS Function: Close a file Input: AH = 3Eh BX = file handle Output: Error if CF = 1, AX = error code (6)

CLOSING FILES (Cont.2) Example: Write instructions to close a file. Assume the file handle is stored in a variable called HANDLE MOV AH, 3EH ; close file function MOV BX, HANDLE ; copy handle to bx INT 21H ; execute closing JC CLOSE_ERROR ; jump if error The only error here would be if there is no file using the file handle (e.g., if the file had already been closed or an incorrect file handle had been stored.)

READING A FILE The read file function reads a specified number of bytes from a file and stores them in memory

READING A FILE (Cont.1) INT 21h, function 3Fh DOS Function: Read a file Input: AH = 3Fh BX = file handle CX = number of bytes to read DS:DX = memory buffer address Output: AX = count of bytes actually read. If AX = 0 or AX < CX, EOF If CF = 1, AX = error code (5, 6)

READING A FILE (Cont.2) Example: Write instructions to read a 512-byte sector from a file..DATA HANDLE DW ? BUFFER DB 512 DUP (0).CODE MOV MOV DS, AX ; initialize DS MOV AH, 3FH ; read file MOV BX, HANDLE ; copy handle into BX MOV CX, 512 ; set count to read LEA DX, BUFFER INT 21H ; AX = bytes read JC READ_ERROR ; jump if error

READING A FILE (Cont.3) In some cases, we may want to read the entire file (i.e., until EOF). Compare AX and CX to check for EOF: CMP AX, CX ; EOF? JL EXIT ; yes, stop reading JMP READ_LOOP ; no, keep reading

WRITING A FILE INT 21h, function 40h DOS Function: Write a file Input: AH = 40h BX = file handle CX = number of bytes to write DS:DX = data address Output: AX = count of bytes written. If AX < CX, error (disk full). If CF = 1, AX = error code (5, 6)

WRITING A FILE (Cont.1) It is possible there is not enough room on the disk to accept the data. This is NOT an error, so the program must check for it by comparing AX and CX Function 40h writes data to a file, but file handles 1 or 4 can be used to send data to the screen or printer

WRITING A FILE (Cont.2) Example: Use function 40h to display a message on the screen..DATA MSG DB 'DISPLAY THIS MESSAGE‘.CODE MOV MOV DS, AX MOV AH, 40H ; write file MOV BX, 1 ; file handle for screen MOV CX, 20 ; len of message (bytes) LEA DX, MSG ; copy address to DX INT 21H ; display MSG executed

THE FILE POINTER The file pointer is used to locate a position in a file When the file is opened, the file pointer is positioned at the beginning of the file After a read operation, the file pointer indicates the next byte to be read; after writing a new file, the file pointer is at EOF The following function can be used to move the file pointer

THE FILE POINTER (Cont.1) DOS Function: Move File Pointer INT 21h, function 42h Input: AH = 42h AL = movement code: 0 = relative to beginning of file 1 = relative to current file pos 2 = move relative to end of file BX = file handle CX:DX = num bytes to move (signed) Output: DX:AX = new location in bytes from the beginning of the file If CF = 1, AX = error code (1, 6)

THE FILE POINTER (Cont.2) In the case where we are moving the file from its current position (AL = 1), if CX:DX is: move pointer backward > 0 => move pointer forward If CX:DX is too large, the pointer could be moved past the beginning or end of the file. This is not an error, but will cause an error when the next read or write is executed If AL = 0 => move pointer from beginning of file (forward) If AL = 1 => move pointer from current position (forward or backward) If AL = 2 => move pointer from end of file (backward)

THE FILE POINTER (Cont.3) Code to move pointer to end of file and determine file size: MOV AH, 42H ; move file ptr MOV BX, HANDLE ; file handle MOV CX, 0 ; clear CX MOV DX, 0 ; 0 bytes to move MOV AL, 2 ; relative to end INT 21H ; move pointer to end. DX:AX = file size JC MOVE_ERROR ; error if CF = 1