Saturday, 5 December 2009

cj add

add shaadi

Data representation methods in a computer.

2.2.2.1 ASCII code

ASCII is an acronym of American Standard Code for Information Interchange. This code assigns the letters of the alphabet, decimal digits from 0 to 9 and some additional symbols a binary number of 7 bits, putting the 8th bit in its off state or 0. This way each letter, digit or special character occupies one byte in the computer memory.

We can observe that this method of data representation is very inefficient on the numeric aspect, since in binary format one byte is not enough to represent numbers from 0 to 255, but on the other hand with the ASCII code one byte may represent only one digit. Due to this inefficiency, the ASCII code is mainly used in the memory to represent text.


2.2.2.2 BCD Method

BCD is an acronym of Binary Coded Decimal. In this notation groups of 4 bits are used to represent each decimal digit from 0 to 9. With this method we can represent two digits per byte of information.

Even when this method is much more practical for number representation in the memory compared to the ASCII code, it still less practical than the binary since with the BCD method we can only represent digits from 0 to 99. On the other hand in binary format we can represent all digits from 0 to 255.

This format is mainly used to represent very large numbers in mercantile applications since it facilitates operations avoiding mistakes.

2.2.2.3 Floating point representation

This representation is based on scientific notation, this is, to represent a number in two parts: its base and its exponent.

As an example, the number 1234000, can be represented as 1.123*10^6, in this last notation the exponent indicates to us the number of spaces that the decimal point must be moved to the right to obtain the original result.

In case the exponent was negative, it would be indicating to us the number of spaces that the decimal point must be moved to the left to obtain the original result.

calculation for haxadecimal and others

2.2.1.1 Information Units

In order for the PC to process information, it is necessary that this information be in special cells called registers. The registers are groups of 8 or 16 flip-flops.

A flip-flop is a device capable of storing two levels of voltage, a low one, regularly 0.5 volts, and another one, commonly of 5 volts. The low level of energy in the flip-flop is interpreted as off or 0, and the high level as on or 1. These states are usually known as bits, which are the smallest information unit in a computer.

A group of 16 bits is known as word; a word can be divided in groups of 8 bits called bytes, and the groups of 4 bits are called nibbles.

2.2.1.2 Numeric systems

The numeric system we use daily is the decimal system, but this system is not convenient for machines since the information is handled codified in the shape of on or off bits; this way of codifying takes us to the necessity of knowing the positional calculation which will allow us to express a number in any base where we need it.

It is possible to represent a determined number in any base through the following formula:



Where n is the position of the digit beginning from right to left and numbering from zero. D is the digit on which we operate and B is the used numeric base.


2.2.1.3 converting binary numbers to decimals

When working with assembly language we come on the necessity of converting numbers from the binary system, which is used by computers, to the decimal
system used by people.

The binary system is based on only two conditions or states, be it on(1) or off(0), thus its base is two.

For the conversion we can use the positional value formula:

For example, if we have the binary number of 10011, we take each digit from right to left and multiply it by the base, elevated to the new position they are:

Binary: 1 1 0 0 1

Decimal: 1*2^0 + 1*2^1 + 0*2^2 + 0*2^3 + 1*2^4

= 1 + 2 + 0 + 0 + 16 = 19 decimal.

The ^ character is used in computation as an exponent symbol and the * character is used to represent multiplication.


2.2.1.4 Converting decimal numbers to binary

There are several methods to convert decimal numbers to binary; only one
will be analyzed here. Naturally a conversion with a scientific calculator is much easier, but one cannot always count with one, so it is convenient to at least know one formula to do it.

The method that will be explained uses the successive division of two, keeping the residue as a binary digit and the result as the next number to divide.


Let us take for example the decimal number of 43.

43/2=21 and its residue is 1

21/2=10 and its residue is 1

10/2=5 and its residue is 0

5/2=2 and its residue is 1

2/2=1 and its residue is 0

1/2=0 and its residue is 1

Building the number from the bottom , we get that the binary result is
101011

2.2.1.5 Hexadecimal system

On the hexadecimal base we have 16 digits which go from 0 to 9 and from the letter A to the F, these letters represent the numbers from 10 to 15. Thus we count 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F.

The conversion between binary and hexadecimal numbers is easy. The first thing done to do a conversion of a binary number to a hexadecimal is to divide it in groups of 4 bits, beginning from the right to the left. In case the last group, the one most to the left, is under 4 bits, the missing places are filled with zeros.

Taking as an example the binary number of 101011, we divide it in 4 bits groups and we are left with:

10;1011

Filling the last group with zeros (the one from the left):

0010;1011

Afterwards we take each group as an independent number and we consider its
decimal value:

0010=2;1011=11

But since we cannot represent this hexadecimal number as 211 because it would be an error, we have to substitute all the values greater than 9 by their respective representation in hexadecimal, with which we obtain:

2BH, where the H represents the hexadecimal base.

In order to convert a hexadecimal number to binary it is only necessary to invert the steps: the first hexadecimal digit is taken and converted to binary, and then the second, and so on.

cj add

Computer is What for assemble?

Computer System.

We call computer system to the complete configuration of a computer, including the peripheral units and the system programming which make it a useful and functional machine for a determined task.

2.1.1 Central Processor.

This part is also known as central processing unit or CPU, which in turn is made by the control unit and the arithmetic and logic unit. Its functions consist in reading and writing the contents of the memory cells, to forward data between memory cells and special registers, and decode and execute the instructions of a program. The processor has a series of memory cells which are used very often and thus, are part of the CPU. These cells are known with the name of registers. A processor may have one or two dozen of these registers. The arithmetic and logic unit of the CPU realizes the operations related with numeric and symbolic calculations. Typically these units only have capacity of performing very elemental operations such as: the addition and subtraction of two whole numbers, whole number multiplication and division, handling of the registers' bits and the comparison of the content of two registers. Personal computers can be classified by what is known as word size, this is, the quantity of bits which the processor can handle at a time.


2.1.2 Central Memory.

It is a group of cells, now being fabricated with semi-conductors, used for general processes, such as the execution of programs and the storage of information for the operations.

Each one of these cells may contain a numeric value and they have the property of being addressable, this is, that they can distinguish one from another by means of a unique number or an address for each cell.

The generic name of these memories is Random Access Memory or RAM. The main disadvantage of this type of memory is that the integrated circuits lose the information they have stored when the electricity flow is interrupted. This was the reason for the creation of memories whose information is not lost when the system is turned off. These memories receive the name of Read Only Memory or ROM.

2.1.3 Input and Output Units.

In order for a computer to be useful to us it is necessary that the processor communicates with the exterior through interfaces which allow the input and output of information from the processor and the memory. Through the use of these communications it is possible to introduce information to be processed and to later visualize the processed data.

Some of the most common input units are keyboards and mice. The most common output units are screens and printers.

2.1.4 Auxiliary Memory Units.

Since the central memory of a computer is costly, and considering today's applications it is also very limited. Thus, the need to create practical and economical information storage systems arises. Besides, the central memory loses its content when the machine is turned off, therefore making it inconvenient for the permanent storage of data.

These and other inconvenience give place for the creation of peripheral units of memory which receive the name of auxiliary or secondary memory. Of these the most common are the tapes and magnetic discs.

The stored information on these magnetic media means receive the name of files. A file is made of a variable number of registers, generally of a fixed size; the registers may contain information or programs.

Introduction of the Assembly

1.1 What's new in the Assembler material

After of one year that we've released the first Assembler material on-line. We've received a lot of e-mail where each people talk about different aspects about this material. We've tried to put these comments and suggestions in this update assembler material. We hope that this new Assembler material release reach to all people that they interest to learn the most important language for IBM PC.

In this new assembler release includes:

A complete chapter about how to use debug program
More example of the assembler material
Each section of this assembler material includes a link file to Free
On-line of Computing by Dennis Howe
Finally, a search engine to look for any topic or item related with this updated material.


1.2 Presentation

The document you are looking at, has the primordial function of introducing you to assembly language programming, and it has been thought for those people who have never worked with this language.

The tutorial is completely focused towards the computers that function with processors of the x86 family of Intel, and considering that the language bases its functioning on the internal resources of the processor, the described examples are not compatible with any other architecture.

The information was structured in units in order to allow easy access to each of the topics and facilitate the following of the tutorial.

In the introductory section some of the elemental concepts regarding computer systems are mentioned, along with the concepts of the assembly language itself, and continues with the tutorial itself.


1.3 Why learn assembler language

The first reason to work with assembler is that it provides the opportunity of knowing more the operation of your PC, which allows the development of software in a more consistent manner.

The second reason is the total control of the PC which you can have with the use of the assembler.

Another reason is that the assembly programs are quicker, smaller, and have
larger capacities than ones created with other languages.

Lastly, the assembler allows an ideal optimization in programs, be it on their size or on their execution.


1.4 We need your opinion

Our goal is offers you easier way to learn yourself assembler language. You send us your comments or suggestions about this 96' edition. Any comment will be welcome.

Assembler 2

5.1 Internal hardware interruptions

Internal interruptions are generated by certain events which come 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 actualizes 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 to date.

Even though we cannot directly manage this interruption, since we cannot control the time dating by means of software, it is possible to use its effects on the computer to our benefit, for example to create a "virtual clock" dated 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.


5.2 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.


5.3 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.

Assembler 3

5.41 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

Functions to read information from the keyboard.

01H Input from the keyboard
0AH Input from the keyboard using buffer
3FH Reading from device/file

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 unit 7, 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

Assembler 4

VIDEO DISPLAY FUNCTIONS



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.

Assembler 5

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.



KEYBOARD INFORMATION FUNCTIONS


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 device
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.
FILE WORKING FUNCTIONS:

FCB FUNCTIONS:
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

Assembler 5

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,

What this function does is that it reads the next block of information from the address given by DS:DX, and dates 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 dates 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

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.

cj add

Assembler 6

HANDLES:

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
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 dates 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.

Assembler 7

5.4.2 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

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 per 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 dates 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

5.4.3 16H INTERRUPTION

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.

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.


5.4.4 17H INTERRUPTION

Purpose: Handles the printer input/output.

Syntax:

Int 17H

This interruption is used to write characters on the printer, sets printer and reads the printer state.


Functions of the 16h interruptions

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

Assembler 7

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 sport 3 parallel ports, although there are BIOS which sport 4 parallel ports.

01H FUNCTION

Use:

Sets 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 sport 3 parallel ports, although there are BIOS which sport 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 sport 3 parallel ports, although there are BIOS which sport 4
parallel ports.


5.5 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.

cj j ad

Assembler 8

5.6.1 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 thesetypes of files are used it is only possible to work on the current directory since the FCBs do not provide sport 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.

5.6.2 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.

5.6.3 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.

Assembler 9

5.6.4 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 dated.

5.6.5 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 dated.

5.6.6 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 the 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.

5.6.7 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.






5.7 Channels of communication

Contents

5.7.1 Working with handles
5.7.2 Functions to use handles


5.7.1 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.

5.7.2 Functions to use handles

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

shaadi adds

Assembler 10

Procedure

Definition of procedure

A procedure is a collection of instructions to which we can direct the flow of our program, and once the execution of these instructions is over control is given back to the next line to process of the code which called on the procedure.

Procedures help us to create legible and easy to modify programs.

At the time of invoking a procedure the address of the next instruction of the program is kept on the stack so that, once the flow of the program has been transferred and the procedure is done, one can return to the next line of the original program, the one which called the procedure.

Syntax of a Procedure

There are two types of procedures, the intrasegments, which are found on the same segment of instructions, and the inter-segments which can be stored on different memory segments.

When the intrasegment procedures are used, the value of IP is stored on the stack and when the intrasegments are used the value of CS:IP is stored.

To divert the flow of a procedure (calling it), the following directive is used:

CALL NameOfTheProcedure

The part which make a procedure are:

Declaration of the procedure
Code of the procedure
Return directive
Termination of the procedure

For example, if we want a routine which adds two bytes stored in AH and AL each one, and keep the addition in the BX register:

Adding Proc Near ; Declaration of the procedure
Mov Bx, 0 ; Content of the procedure
Mov B1, Ah
Mov Ah, 00
Add Bx, Ax
Ret ; Return directive
Add Endp ; End of procedure declaration

On the declaration the first word, Adding, corresponds to the name of out procedure, Proc declares it as such and the word Near indicates to the MASM that the procedure is intrasegment.

The Ret directive loads the IP address stored on the stack to return to the original program, lastly, the Add Endp directive indicates the end of the procedure.

To declare an inter segment procedure we substitute the word Near for the word FAR.

The calling of this procedure is done the following way:

Call Adding

Macros offer a greater flexibility in programming compared to the procedures, nonetheless, these last ones will still be used.


Definition of the macro

A macro is a group of repetitive instructions in a program which are codified only once and can be used as many times as necessary.

The main difference between a macro and a procedure is that in the macro the passage of parameters is possible and in the procedure it is not, this is only applicable for the TASM - there are other programming languages which do allow it. At the moment the macro is executed each parameter is substituted by the name or value specified at the time of the call.

We can say then that a procedure is an extension of a determined program, while the macro is a module with specific functions which can be used by different programs.

Another difference between a macro and a procedure is the way of calling each one, to call a procedure the use of a directive is required, on the other hand the call of macros is done as if it were an assembler instruction.

shaadi add

Assembler 10

Syntax of a Macro

The parts which make a macro are:

Declaration of the macro
Code of the macro
Macro termination directive

The declaration of the macro is done the following way:

NameMacro MACRO [parameter1, parameter2...]

Even though we have the functionality of the parameters it is possible to create a macro which does not need them.

The directive for the termination of the macro is: ENDM

An example of a macro, to place the cursor on a determined position on the screen is:

Position MACRO Row, Column
PUSH AX
PUSH BX
PUSH DX
MOV AH, 02H
MOV DH, Row
MOV DL, Column
MOV BH, 0
INT 10H
POP DX
POP BX
POP AX
ENDM

To use a macro it is only necessary to call it by its name, as if it were another assembler instruction, since directives are no longer necessary as in the case of the procedures.

Assembler Macro Libraries 11

One of the facilities that the use of macros offers is the creation of libraries, which are groups of macros which can be included in a program from a different file.

The creation of these libraries is very simple, we only have to write a file with all the macros which will be needed and save it as a text file.

To call these macros it is only necessary to use the following instruction Include NameOfTheFile, on the part of our program where we would normally write the macros, this is, at the beginning of our program, before the declaration of the memory model.

The macros file was saved with the name of MACROS.TXT, the instruction Include would be used the following way:

;Beginning of the program
Include MACROS.TXT
.MODEL SMALL
.DATA
;The data goes here
.CODE
Beginning:
;The code of the program is inserted here
.STACK
;The stack is defined
End beginning
;Our program ends


More debug program examples

In this section we provide you several assembler programs to run in the debug program. You can execute each assembler program using the "t" (trace) command, to see what each instruction does.

First example

-a0100
297D:0100 MOV AX,0006 ; Puts value 0006 at register AX
297D:0103 MOV BX,0004 ;Puts value 0004 at register BX
297D:0106 ADD AX,BX ;Adds BX to AX contents
297D:0108 INT 20 ;Causes end of the Program

The only thing that this program does is to save two values in two registers and add the value of one to the other.

shaadi add

Assembler 12

Second example

- a100
0C1B:0100 jmp 125 ; Jumps to direction 125H
0C1B:0102 [Enter]
- e 102 'Hello, How are you ?' 0d 0a '$'
- a125
0C1B:0125 MOV DX,0102 ; Copies string to DX register
0C1B:0128 MOV CX,000F ; Times the string will be displayed
0C1B:012B MOV AH,09 ; Copies 09 value to AH register
0C1B:012D INT 21 ; Displays string
0C1B:012F DEC CX ; Reduces in 1 CX
0C1B:0130 JCXZ 0134 ; If CX is equal to 0 jumps to 0134
0C1B:0132 JMP 012D ; Jumps to direction 012D
0C1B:0134 INT 20 ; Ends the program

This program displays on the screen 15 times a character string.

Third example

-a100
297D:0100 MOV AH,01 ;Function to change the cursor
297D:0102 MOV CX,0007 ;Forms the cursor
297D:0105 INT 10 ;Calls for BIOS
297D:0107 INT 20 ;Ends the program

This program is good for changing the form of the cursor.

Fourth example

-a100
297D:0100 MOV AH,01 ; Funtion 1 (reads keyboard)
297D:0102 INT 21 ; Calls for DOS
297D:0104 CMP AL,0D ; Compares if what is read is a carriage return
297D:0106 JNZ 0100 ; If it is not, reads another character
297D:0108 MOV AH,02 ; Funtion 2 (writes on the screen)
297D:010A MOV DL,AL ; Character to write on AL
297D:010C INT 21 ; Calls for DOS
297D:010E INT 20 ; Ends the program

This program uses DOS 21H interruption. It uses two functions of the same: the first one reads the keyboard (function 1) and the second one writes on the screen. It reads the keyboard characters until it finds a carriage
return.

cj adds plus

cj add

Assembler 13

Fifth example

-a100
297D:0100 MOV AH,02 ; Function 2 (writes on the screen)
297D:0102 MOV CX,0008; Puts value 0008 on register CX
297D:0105 MOV DL,00 ; Puts value 00 on register DL
297D:0107 RCL BL,1 ; Rotates the byte in BL to the left by one bit
; through the carry flag
297D:0109 ADC DL,30 ; Converts flag register to1
297D:010C INT 21 ; Calls for DOS
297D:010E LOOP 0105 ; Jumps if CX > 0 to direction 0105
297D:0110 INT 20 ; Ends the program

This program displays on the screen a binary number through a conditional cycle (LOOP) using byte rotation.

Sixth example

-a100
297D:0100 MOV AH,02 ; Function 2 (writes on the screen)
297D:0102 MOV DL,BL ; Puts BL's value on DL
297D:0104 ADD DL,30 ; Adds value 30 to DL
297D:0107 CMP DL,3A ; Compares 3A value with DL's contents without
; affecting its value only modifying the state of
; the car
297D:010A JL 010F ; jumps if < direction 010f
297D:010C ADD DL,07 ; Adds 07 value on DL
297D:010F INT 21 ; Calls for Dos
297D:0111 INT 20 ; Ends the Program

This program prints a zero value on hex digits

Seventh example

-a100
297D:0100 MOV AH,02 ; Function 2 (writes on the screen)
297D:0102 MOV DL,BL ; Puts BL value on DL
297D:0104 AND DL,0F ; Carries ANDing numbers bit by bit
297D:0107 ADD DL,30 ; Adds 30 to Dl
297D:010A CMP DL,3A ; Compares Dl with 3A
297D:010D JL 0112 ; Jumps if < 0112 direction
297D:010F ADD DL, 07 ; Adds 07 to DL
297D:0112 INT 21 ; Calls for Dos
297D:0114 INT 20 ; Ends the program

This program is used to print two digit hex numbers.

Eight example

-a100
297D:0100 MOV AH,02 ; Function 2 (writes on the screen)
297D:0102 MOV DL,BL ; Puts BL value on DL
297D:0104 MOV CL,04 ; Puts 04 value on CL
297D:0106 SHR DL,CL ; Moves per four bits of your number to the
; rightmost nibble
297D:0108 ADD DL,30 ; Adds 30 to DL
297D:010B CMP L,3A ; Compares Dl with 3A
297D:010E JL 0113 ; Jumps if < 0113 direction
297D:0110 ADD DL,07 ; Adds 07 to DL
297D:0113 INT 21 ; Calls for Dos
297D:0115 INT 20 ; Ends the program

This program works for printing the first of two digit hex numbers

cj adds

Assembler 14

Ninth example

-a100
297D:0100 MOV AH,02 ; Function 2 (writes on the screen)
297D:0102 MOV DL,BL ; Puts BL value on DL
297D:0104 MOV CL,04 ; Puts 04 value on CL
297D:0106 SHR DL,CL ; Moves per four bits of your number to the
;rightmost nibble
297D:0108 ADD DL,30 ; Adds 30 to DL
297D:010B CMP DL,3A ; Compares Dl with 3A
297D:010E JL 0113 ; Jumps if < 0113 direction
297D:0110 ADD DL,07 ; Adds 07 to DL
297D:0113 INT 21 ; Calls for Dos
297D:0115 MOV DL,BL ; Puts Bl value on DL
297D:0117 AND DL,0F ; Carries ANDing numbers bit by bit
297D:011A ADD DL,30 ; Adds 30 to DL
297D:011D CMP DL,3A ; Compares Dl with 3A
297D:0120 JL 0125 ; Jumps if < 125 direction
297D:0122 ADD DL,07 ; Adds 07 to DL
297D:0125 INT 21 ; Calls for Dos
297D:0127 INT 20 ; Ends the Program

This program works for printing the second of two digit hex numbers

Tenth example

-a100
297D:0100 MOV AH,01 ; Function 1 (reads keyboard)
297D:0102 INT 21 ; Calls for Dos
297D:0104 MOV DL,AL ; Puts Al value on DL
297D:0106 SUB DL,30 ; Subtracts 30 from DL
297D:0109 CMP DL,09 ; Compares DL with 09
297D:010C JLE 0111 ; Jumps if <= 0111 direction
297D:010E SUB DL,07 ; Subtracts 07 from DL
297D:0111 MOV CL,04 ; Puts 04 value on CL register
297D:0113 SHL DL,CL ; It inserts zeros to the right
297D:0115 INT 21 ; Calls for Dos
297D:0117 SUB AL,30 ; Subtracts 30 from AL
297D:0119 CMP AL,09 ; Compares AL with 09
297D:011B JLE 011F ; Jumps if <= 011f direction
297D:011D SUB AL,07 ; Subtracts 07 from AL
297D:011F ADD DL,AL ; Adds Al to DL
297D:0121 INT 20 ; Ends the Program

This program can read two digit hex numbers

Eleventh example

-a100
297D:0100 CALL 0200 ; Calls for a procedure
297D:0103 INT 20 ;Ends the program

-a200
297D:0200 PUSH DX ; Puts DX value on the stack
297D:0201 MOV AH,08 ; Function 8
297D:0203 INT 21 ; Calls for Dos
297D:0205 CMP AL,30 ; Compares AL with 30
297D:0207 JB 0203 ; Jumps if CF is activated towards 0203 direction
297D:0209 CMP AL,46 ; Compares AL with 46
297D:020B JA 0203 ; jumps if <> 0203 direction
297D:020D CMP AL,39 ; Compares AL with 39
297D:020F JA 021B ; Jumps if <> 021B direction
297D:0211 MOV AH,02 ; Function 2 (writes on the screen)
297D:0213 MOV DL,AL ; Puts Al value on DL
297D:0215 INT 21 ; Calls for Dos
297D:0217 SUB AL,30 ; Subtracts 30 from AL
297D:0219 POP DX ; Takes DX value out of the stack
297D:021A RET ; Returns control to the main program
297D:021B CMP AL,41 ; Compares AL with 41
297D:021D JB 0203 ; Jumps if CF is activated towards 0203 direction
297D:021F MOV AH,02 ; Function 2 (writes on the screen)
297D:022 MOV DL,AL ; Puts AL value on DL
297D:0223 INT 21 ; Calls for Dos
297D:0225 SUB AL,37 ; Subtracts 37 from AL
297D:0227 POP DX ; Takes DX value out of the stack
297D:0228 RET ; Returns control to the main program

This program keeps reading characters until it receives one that can be
converted to a hex number

cj add

Assembler 15

More Assembler programs examples( using TASM program)


;name of the program:one.asm
;
.model small
.stack
.code
mov AH,1h ;Selects the 1 D.O.S. function
Int 21h ;reads character and return ASCII code to register AL
mov DL,AL ;moves the ASCII code to register DL
sub DL,30h ;makes the operation minus 30h to convert 0-9 digit number
cmp DL,9h ;compares if digit number it was between 0-9
jle digit1 ;If it true gets the first number digit (4 bits long)
sub DL,7h ;If it false, makes operation minus 7h to convert letter A-F
digit1:
mov CL,4h ;prepares to multiply by 16
shl DL,CL ; multiplies to convert into four bits upper
int 21h ;gets the next character
sub AL,30h ;repeats the conversion operation
cmp AL,9h ;compares the value 9h with the content of register AL
jle digit2 ;If true, gets the second digit number
sub AL,7h ;If no, makes the minus operation 7h
digit2:
add DL,AL ;adds the second number digit
mov AH,4CH
Int 21h ;21h interruption
End ; finishs the program code

This program reads two characters from the keyboard and prints them on the screen.

;name the program:two.asm
.model small
.stack
.code
PRINT_A_J PROC
MOV DL,'A' ;moves the A character to register DL
MOV CX,10 ;moves the decimal value 10 to register cx
;This number value its the time to print out after the A ;character
PRINT_LOOP:
CALL WRITE_CHAR ;Prints A character out
INC DL ;Increases the value of register DL
LOOP PRINT_LOOP ;Loop to print out ten characters
MOV AH,4Ch ;4Ch function of the 21h interruption
INT 21h ;21h interruption
PRINT_A_J ENDP ;Finishes the procedure

WRITE_CHAR PROC
MOV AH,2h ;2h function of the 21 interruption
INT 21h ;Prints character out from the register DL
RET ;Returns the control to procedure called
WRITE_CHAR ENDP ;Finishes the procedure
END PRINT_A_J ;Finishes the program code

This program prints the a character through j character on the screen

cj add

assembly 16

;name of the program :three.asm
.model small
.STACK
.code

TEST_WRITE_HEX PROC
MOV DL,3Fh ;moves the value 3Fh to the register DL
CALL WRITE_HEX ;Calls the procedure
MOV AH,4CH ;4Ch function
INT 21h ;Returns the control to operating system
TEST_WRITE_HEX ENDP ;Finishes the procedure

PUBLIC WRITE_HEX
;........................................................;
; This procedure converts into hexadecimal number the byte is in the register DL and show the digit number;
;Use:WRITE_HEX_DIGIT ;
;........................................................;

WRITE_HEX PROC
PUSH CX ;pushes the value of the register CX to the stack memory
PUSH DX ;pushes the value of the register DX to the stack memory
MOV DH,DL ;moves the value of the register DL to register DH
MOV CX,4 ;moves the value numeric 4 to register CX
SHR DL,CL
CALL WRITE_HEX_DIGIT ;shows on the computer screen, the first hexadecimal number
MOV DL,DH ;moves the value of the register DH to the register DL
AND DL,0Fh ;ANDing the upper bit
CALL WRITE_HEX_DIGIT ; shows on the computer screen, the second hexadecimal number
POP DX ;pops the value of the register DX to register DX
POP CX ; pops the value of the register DX to register DX
RET ;Returns the control of the procedure called
WRITE_HEX ENDP

PUBLIC WRITE_HEX_DIGIT
;......................................................................;
; ;
; This procedure converts the lower 4 bits of the register DL into hexadecimal ;number and show them in the computer screen ;
;Use: WRITE_CHAR ;
;......................................................................;

WRITE_HEX_DIGIT PROC
PUSH DX ;Pushes the value of the register DX in the stack memory
CMP DL,10 ;compares if the bit number is minus than number ten
JAE HEX_LETTER ;No , jumps HEX_LETER
ADD DL,"0" ;yes, it converts into digit number
JMP Short WRITE_DIGIT ;writes the character
HEX_LETTER:
ADD DL,"A"-10 ;converts a character into hexadecimal number
WRITE_DIGIT:
CALL WRITE_CHAR ;shows the character in the computer screen
POP DX ;Returns the initial value of the register DX to register DL
RET ;Returns the control of the procedure called
WRITE_HEX_DIGIT ENDP

PUBLIC WRITE_CHAR
;......................................................................;
;This procedure shows the character in the computer screen using the D.O.S. ;
;......................................................................;

WRITE_CHAR PROC
PUSH AX ;pushes the value of the register AX in the stack memory
MOV AH,2 ;2h Function
INT 21h ;21h Interruption
POP AX ;Pops the initial value of the register AX to the register AX
RET ;Returns the control of the procedure called
WRITE_CHAR ENDP

END TEST_WRITE_HEX ;finishes the program code

This program prints a predefined value on the screen


;name of the program:five.asm
.model small
.stack
.code

PRINT_ASCII PROC
MOV DL,00h ;moves the value 00h to register DL
MOV CX,255 ;moves the value decimal number 255. this decimal number
;will be 255 times to print out after the character A
PRINT_LOOP:
CALL WRITE_CHAR ;Prints the characters out
INC DL ;Increases the value of the register DL content
LOOP PRINT_LOOP ;Loop to print out ten characters
MOV AH,4Ch ;4Ch function
INT 21h ;21h Interruption
PRINT_ASCII ENDP ;Finishes the procedure

WRITE_CHAR PROC
MOV AH,2h ;2h function to print character out
INT 21h ;Prints out the character in the register DL
RET ;Returns the control to the procedure called
WRITE_CHAR ENDP ;Finishes the procedure

END PRINT_ASCII ;Finishes the program code

This program prints the 256 ASCII code on the screen

dosseg
.model small
.stack
.code
write proc
mov ah,2h;

Timebomb of Assambly data of the computer.

mov dl,2ah;
int 21h
mov ah,4ch
int 21h
write endp

end write

This program prints a defined character using an ASCII code on the screen.


.model small; the name of the program is seven.asm
.stack;
.code;

EEL: MOV AH,01 ; 1 function (reads one character from the keyboard)
INT 21h ; 21h interruption
CMP AL,0Dh ; compares the value with 0dh
JNZ EEL ;jumps if no equal of the label eel
MOV AH,2h ; 2 function (prints the character out on the screen)
MOV DL,AL ;moves the value of the register AL to the register DL
INT 21h ;21 interruption
MOV AH,4CH ;4C function (returns the control to the D.O.S. operating system)
INT 21h ;21 interruption

END ;finishes the program

This program reads characters form the keyboard and prints them on the screen until find the return character.