Table of Contents

Interruptions
File managing

Interruptions

Table of Contents

Go to the main page

INTERNAL HARDWARE INTERRUPTIONS

Internal interruptions are generated by certain events which come up during the execution of a program.

This type of interruptions are managed on their totality by the hardware and it is not possible to modify them.

A clear example of this type of interruptions is the one which updates the counter of the computer internal clock, the hardware makes the call to this interruption several times during a second in order to maintain the time up to date.

Even though we cannot directly manage this interruption, since we cannot control the time updating by means of software, it is possible to use its effects on the computer to our benefit, for example to create a "virtual clock" updated continuously thanks to the clock's internal counter. We only have to write a program which reads the actual value of the counter and to translates it into an understandable format for the user.

Go to the interruptions section

EXTERNAL HARDWARE INTERRUPTIONS

External interruptions are generated by peripheral devices, such as keyboards, printers, communication cards, etc. They are also generated by coprocessors.

It is not possible to deactivate external interruptions.

These interruptions are not sent directly to the CPU, but rather they are sent to an integrated circuit whose function is to exclusively handle this type of interruptions. The circuit, called PIC8259A, is controlled by the CPU using for this control a series of communication ways called paths.

Go to the interruptions section

SOFTWARE INTERRUPTIONS

Software interruptions can be directly activated by the assembler invoking the number of the desired interruption with the INT instruction.

The use of interruptions helps us in the creation of programs, and by using them our programs are shorter, it is easier to understand them and they usually have a better performance mostly due to their smaller size.

This type of interruptions can be separated in two categories: the operative system DOS interruptions and the BIOS interruptions.

The difference between the two is that the operative system interruptions are easier to use but they are also slower since these interruptions make use of the BIOS to achieve their goal, on the other hand the BIOS interruptions are much faster but they have the disadvantage that since they are part of the hardware, they are very specific and can vary depending even on the brand of the maker of the circuit.

The election of the type of interruption to use will depend solely on the characteristics you want to give your program: speed, using the BIOS ones, or portability, using the ones from the DOS.

Go to the interruptions section

Most Common interruptions

Table of Contents

Go to the interruptions section

21H INTERRUPTION

Purpose: To call on diverse DOS functions.

Syntax:

Int 21H

Note: When we work in TASM program is necessary to specify that the value we are using is hexadecimal.

This interruption has several functions, to access each one of them it is necessary that the function number which is required at the moment of calling the interruption is in the AH register.

Functions to display information to the video.

02H Exhibits output
09H Chain Impression (video)
40H Writing in device/file
Go to the most common interruption section
Functions to read information from the keyboard.

01H Input from the keyboard
0AH Input from the keyboard using buffer
3FH Reading from device/file
Go to the main page
Functions to work with files.

In this section only the specific task of each function is exposed, for a reference about the concepts used, refer to the section, titled : "Introduction to file handling".

FCB Method

0FH Open file
14H Sequential reading
15H Sequential writing
16H Create file
21H Random reading
22H Random writing
Handles

3CH Create file
3DH Open file
3EH Close file driver
3FH Reading from file/device
40H Writing in file/device
42H Move pointer of reading/writing in file
Go to the main page

02H FUNCTION

Use:

It displays one character to the screen.

Calling registers:

AH = 02H
DL = Value of the character to display.

Return registers:

None.

This function displays the character whose hexadecimal code corresponds to the value stored in the DL register, and no register is modified by using this command.

The use of the 40H function is recommended instead of this function.

09H FUNCTION

Use:

It displays a chain of characters on the screen.

Call registers:

AH = 09H
DS:DX = Address of the beginning of a chain of characters.

Return registers:

None.

This function displays the characters, one by one, from the indicated address in the DS:DX register until finding a $ character, which is interpreted as the end of the chain.

It is recommended to use the 40H function instead of this one.

40H FUNCTION

Use:

To write to a device or a file.

Call registers:

AH = 40H
BX = Path of communication
CX = Quantity of bytes to write
DS:DX = Address of the beginning of the data to write

Return registers:

CF = 0 if there was no mistake
        
        AX = Number of bytes written

CF = 1 if there was a mistake

        AX = Error code

The use of this function to display information on the screen is done by giving the BX register the value of 1 which is the preassigned value to the video by the operative system MS-DOS.

01H FUNCTION

Use:

To read a keyboard character and to display it.

Call registers

AH = 01H

Return registers:

AL = Read character

It is very easy to read a character from the keyboard with this function, the hexadecimal code of the read character is stored in the AL register. In case it is an extended register the AL register will contain the value of 0 and it will be necessary to call on the function again to obtain the code of that character.

0AH FUNCTION

Use:

To read keyboard characters and store them on the buffer.

Call registers:

AH = 0AH
DS:DX = Area of storage address
BYTE 0 = Quantity of bytes in the area
BYTE 1 = Quantity of bytes read
from BYTE 2 till BYTE 0 + 2 = read characters

Return characters:

None.

The characters are read and stored in a predefined space on memory. The structure of this space indicate that in the first byte are indicated how many characters will be read. On the second byte the number of characters already read are stored, and from the third byte on the read characters are written.

When all the indicated characters have been stored the speaker sounds and any additional character is ignored. To end the capture of the chain it is necessary to hit [ENTER].

3FH FUNCTION

Use:

To read information from a device or file.

Call registers:

AH = 3FH
BX = Number assigned to the divice
CX = Number of bytes to process
DS:DX = Address of the storage area

Return registers:

CF = 0 if there is no error and AX = number of read bytes.
CF = 1 if there is an error and AX will contain the error code.

0FH FUNCTION

Use:

To open an FCB file

Call registers:

AH = 0FH
DS:DX = Pointer to an FCB

Return registers:

AL = 00H if there was no problem, otherwise it returns to 0FFH

14H FUNCTION

Use:

To sequentially read an FCB file.

Call registers:

AH = 14H
DS:DX = Pointer to an FCB already opened.

Return registers:

AL = 0 if there were no errors, otherwise the corresponding error code will be returned:  1 error at the end of the file, 2 error on the FCB structure and 3 partial reading error.

What this function does is that it reads the next block of information from the address given by DS:DX, and updates this register.

15H FUNCTION

Use:

To sequentially write and FCB file.

Call registers:

AH = 15H
DS:DX = Pointer to an FCB already opened.

Return registers:

AL = 00H if there were no errors, otherwise it will contain the error code:  1 full disk or read-only file, 2 error on the formation or on the specification of the FCB.

The 15H function updates the FCB after writing the register to the present block.

16H FUNCTION

Use:

To create an FCB file. Call registers:

AH = 16H
DS:DX = Pointer to an already opened FCB.

Return registers:

AL = 00H if there were no errors, otherwise it will contain the 0FFH value.

It is based on the information which comes on an FCB to create a file on a disk.

21H FUNCTION

Use:

To read in an random manner an FCB file.

Call registers:

AH = 21H
DS:DX = Pointer to and opened FCB.

Return registers:

A = 00H if there was no error, otherwise AH will contain the code of the error:  1 if it is the end of file, 2 if there is an FCB specification error and 3 if a partial register was read or the file pointer is at the end of the same.

This function reads the specified register by the fields of the actual block and register of an opened FCB and places the information on the DTA, Disk Transfer Area.

22H FUNCTION

Use:

To write in an random manner an FCB file.

Call registers:

AH = 22H
DS:DX = Pointer to an opened FCB.

Return registers:

AL = 00H if there was no error, otherwise it will contain the error code:  1 if the disk is full or the file is an only read and 2 if there is an error on the FCB specification.

It writes the register specified by the fields of the actual block and register of an opened FCB. It writes this information from the content of the DTA.

3CH FUNCTION

Use:

To create a file if it does not exist or leave it on 0 length if it exists, Handle.

Call registers:

AH = 3CH
CH = File attribute
DS:DX = Pointer to an ASCII specification.

Return registers:

CF = 0 and AX the assigned number to handle if there is no error, in case there is, CF will be 1 and AX will contain the error code:  3 path not found, 4 there are no handles available to assign and 5 access denied.

This function substitutes the 16H function. The name of the file is specified on an ASCII chain, which has as a characteristic being a conventional chain of bytes ended with a 0 character.

The file created will contain the attributes defined on the CX register in the following manner:

        Value           Attributes
         00H              Normal
         02H              Hidden
         04H              System
         06H              Hidden and of system

The file is created with the reading and writing permissions. It is not possible to create directories using this function.

3DH FUNCTION

Use:

It opens a file and returns a handle.

Call registers:

AH = 3DH
AL = manner of access
DS:DX = Pointer to an ASCII specification

Return registers:

CF = 0 and AX = handle number if there are no errors, otherwise CF = 1 and AX = error code: 01H if the function is not valid, 02H if the file was not found, 03H if the path was not found, 04H if there are no available handles, 05H in case access is denied, and 0CH if the access code is not valid.

The returned handled is 16 bits.

The access code is specified in the following way:

        BITS
       7  6  5  4  3  2  1

       .  .  .  .  0  0  0      Only reading
       .  .  .  .  0  0  1      Only writing
       .  .  .  .  0  1  0      Reading/Writing
       .  .  .  x  .  .  .      RESERVED


3EH FUNCTION

Use:

Close file (handle).

Call registers:

AH = 3EH
BX = Assigned handle

Return registers:

CF = 0 if there were no mistakes, otherwise CF will be 1 and AX will contain the error code:  06H if the handle is invalid.

This function updates the file and frees the handle it was using.

3FH FUNCTION

Use:

To read a specific quantity of bytes from an open file and store them on a specific buffer.

10h Interruption

Purpose: To call on diverse BIOS video function

Syntax:

Int 10H

This interruption has several functions, all of them control the video input/output, to access each one of them it is necessary that the function number which is required at the moment of calling the interruption is in the Ah register.

In this tutorial we will see some functions of the 10h interruption.

Common functions of the 10h interruption

02H Function, select the cursor position
09H Function, write attribute and character of the cursor
0AH Function, write a character in the cursor position
0EH Function, Alphanumeric model of the writing characters

Go to the most common interruption section

02h Function

Use:

Moves the cursor on the computer screen using text model.

Call registers:

AH = 02H

BH = Video page where the cursor is positioned.

DH = row

DL = Column

Return Registers:

None.

The cursor position is defined by its coordinates, starting from the position 0,0 to position 79,24. This means from the left upper computer screen corner to right lower computer screen. Therefore the numeric values that the DH and DL registers get in text model are: from 0 to 24 for rows and from 0 to 79 for columns.

09h Function

Use:

Shows a defined character several times on the computer screen with a defined attribute, starting with the actual cursor position.

Call registers:

AH = 09H
AL = Character to display
BH = Video page, where the character will display it;
BL = Attribute to use
number of repetition.

Return registers:

None

This function displays a character on the computer screen several times, using a specified number in the CX register but without changing the cursor position on the computer screen.

0Ah Function

Use:

Displays a character in the actual cursor position.

Call registers:

AH = 0AH
AL = Character to display
BH = Video page where the character will display it
BL = Color to use (graphic mode only).
CX = number of repetitions

Return registers:

None.

The main difference between this function and the last one is that this one doesn't allow modifications on the attributes neither does it change the cursor position.

0EH Function

Use:

Displays a character on the computer screen updates the cursor position.

Call registers:

AH = 0EH
AL = Character to display
BH = Video page where the character will display it
BL = Color to use (graphic mode only).

Return registers:

None

Int 16H

We will see two functions of the 16 h interruption, these functions are called by using the AH register.

Functions of the 16h interruption

00H Function, reads a character from the keyboard.
01H Function, reads the keyboard state.
Go to the most common interruption section
00H Function Use:
Reads a character from the keyboard.
Call registers:
AH = 00H
Return registers:
AH = Scan code of the keyboard
AL = ASCII value of the character
When we use this interruption, the program executing is halted until a character is typed, if this is an ASCII value; it is stored in the Ah register, Else the scan code is stored in the AL register and the AH register contents the value 00h.
The proposal of the scan code is to use it with the keys without ASCII representation as [ALT][CONTROL], the function keys and so on.

01h function

Use:
Reads the keyboard state
Call registers:
AH = 01H
Return registers:
If the flag register is zero, this means, there is information on the buffer memory, else, there is no information in the buffer memory. Therefore the value of the Ah register will be the value key stored in the buffer memory.

17H Interruption

Purpose: Handles the printer input/output.
Syntax:
Int 17H
This interruption is used to write characters on the printer, sets printer up and reads the printer state.

Functions of the 16h interruptions

00H Function, prints value ASCII out
01H Function, sets printer up
02H Function, the printer state

00H Function

Use:
Writes a character on the printer.
Call registers:

AH = 00H
AL = Character to print.
DX = Port to use.
Return registers:

AH = Printer device state.
The port to use is in the DX register, the different values are: LPT1 = 0, LPT2 = 1, LPT3 = 2 ...
The printer device state is coded bit by bit as follows:
BIT 1/0 MEANING
----------------------------------------
0 1 The waited time is over
1 -
2 -
3 1 input/output error
4 1 Chosen printer
5 1 out-of-paper
6 1 communication recognized
7 1 The printer is ready to use
1 and 2 bits are not relevant
Most BIOS support 3 parallel ports, although there are BIOS which support 4 parallel ports.

01h Function

Use:
Sets up parallel port.
Call registers:
AH = 01H
DX = Port to use
Return registers:
AH = Printer status
Port to use is defined in the DX register, for example: LPT=0, LPT2=1, and so on.
The state of the printer is coded bit by bit as follows:
BIT 1/0 MEANING
----------------------------------------
0 1 The waited time is over
1 -
2 -
3 1 input/output error
4 1 Chosen printer
5 1 out-of-paper
6 1 communication recognized
7 1 The printer is ready to use
1 and 2 bits are not relevant
Most BIOS support 3 parallel ports, although there are BIOS which support 4 parallel ports.

02h Function

Uses:
Gets the printer status.
Call registers:

AH = 01H
DX = Port to use
Return registers

AH = Printer status.
Port to use is defined in the DX register, for example: LPT=0, LPT2=1, and so on
The state of the printer is coded bit by bit as follows:
BIT 1/0 MEANING
----------------------------------------
0 1 The waited time is over
1 -
2 -
3 1 input/output error
4 1 Chosen printer
5 1 out-of-paper
6 1 communication recognized
7 1 The printer is ready to use
1 and 2 bits are not relevant
Most BIOS support 3 parallel ports, although there are BIOS which support 4 parallel ports.
Go to the main page

File managing

Table of Contents

Go to the main page

Ways of working with files

There are two ways to work with files, the first one is by means of file control blocks or "FCB" and the second one is by means of communication channels, also known as "handles".

The first way of file handling has been used since the CPM operative system, predecessor of DOS, thus it assures certain compatibility with very old files from the CPM as well as from the 1.0 version of the DOS, besides this method allows us to have an unlimited number of open files at the same time. If you want to create a volume for the disk the only way to achieve this is by using this method.

Even after considering the advantages of the FCB, the use of the communication channels it is much simpler and it allows us a better handling of errors, besides, since it is much newer it is very probable that the files created this way maintain themselves compatible through later versions of the operative system.

For a greater facility on later explanations I will refer to the file control blocks as FCBs and to the communication channels as handles.

Go to the file managing section

FCB method

Table of contents

Go to the file managing section

Introduction

There are two types of FCB, the normal, whose length is 37 bytes and the extended one of 44 bytes.
On this tutorial we will only deal with the first type, so from now on when I refer to an FCB, I am really talking about a 37 bytes FCB.

The FCB is composed of information given by the programmer and by information which it takes directly from the operative system. When these types of files are used it is only possible to work on the current directory since the FCBs do not provide support for the use of the organization by directories of DOS.

The FCB is formed by the following fields:


   POSITION		LENGTH		MEANING
     00H		 1 Byte	       Drive
     01H		 8 Bytes       File name
     09H 		 3 Bytes       Extension       
     0CH		 2 Bytes       Block number
     0EH  		 2 Bytes       Register size
     10H		 4 Bytes       File size
     14H 		 2 Bytes       Creation date
     16H  		 2 Bytes       Creation hour
     18H		 8 Bytes       Reserved
     20H		 1 Bytes       Current register
     21H		 4 Bytes       Random register

To select the work drive the next format is followed: drive A = 1; drive B = 2; etc. If 0 is used the drive being used at that moment will be taken as option.

The name of the file must be justified to the left and in case it is necessary the remaining bytes will have to be filled with spaces, and the extension of the file is placed the same way.

The current block and the current register tell the computer which register will be accessed on reading or writing operations. A block is a group of 128 registers. The first block of the file is the block 0. The first register is the register 0, therefore the last register of the first block would be the 127, since the numbering started with 0 and the block can contain 128 registers in total.

OPENING FILES

To open an FCB file the 21H interruption, 0FH function is used. The unit, the name and extension of the file must be initialized before opening it.

The DX register must point to the block. If the value of FFH is returned on the AH register when calling on the interruption then the file was not found, if everything came out well a value of 0 will be returned.

If the file is opened then DOS initializes the current block to 0, the size of the register to 128 bytes and the size of the same and its date are filled with the information found in the directory.

CREATING A NEW FILE

For the creation of files the 21H interruption 16H function is used.

DX must point to a control structure whose requirements are that at least the logic unit, the name and the extension of the file be defined.

In case there is a problem the FFH value will be returned on AL, otherwise this register will contain a value of 0.

SEQUENTIAL WRITING

Before we can perform writing to the disk it is necessary to define the data transfer area using for this end the 1AH function of the 21H interruption.

The 1AH function does not return any state of the disk nor or the operation, but the 15H function, which is the one we will use to write to the disk, does it on the AL register, if this one is equal to zero there was no error and the fields of the current register and block are updated.

SEQUENTIAL READING

Before anything we must define the file transfer area or DTA.

In order to sequentially read we use the 14H function of the 21H interruption.

The register to be read is the one which is defined by the current block and register. The AL register returns to the state of the operation, if AL contains a value of 1 or 3 it means we have reached the end of the file. A value of 2 means that the FCB is wrongly structured.

In case there is no error, AL will contain the value of 0 and the fields of the current block and register are updated.

Random reading and writing

The 21H function and the 22H function of the 21H interruption are the ones in charge of realizing the random readings and writings respectively.

The random register number and the current block are used to calculate relative position of the register to read or write.

The AL register returns the same information for the sequential reading of writing. The information to be read will be returned on the transfer area of the disk, likewise the information to be written resides on the DTA.

CLOSING A FILE

To close a file we use the 10H function of the 21H interruption.

If after invoking this function, the AL register contains the FFH value, this means that the file has changed position, the disk was changed or there is error of disk access.

Channels of communication

Table of contents

Go to the file managing section

Working with handles

The use of handles to manage files greatly facilitates the creation of files and programmer can concentrate on other aspects of the programming without worrying on details which can be handled by the operative system.

The easy use of the handles consists in that to operate o a file, it is only necessary to define the name of the same and the number of the handle to use, all the rest of the information is internally handled by the DOS.

When we use this method to work with files, there is no distinction between sequential or random accesses, the file is simply taken as a chain of bytes.

FUNCTIONS TO USE HANDLES

The functions used for the handling of files through handles are described in the interruption section: Interruptions, in the section dedicated to the 21H interruption.

Go to the file managing section

Go to the main page