                                
                                UNIVERSITY  OF GUADALAJARA

			Information Systems General Coordination.
				Culture and Entertainment Web
				    University of Guadalajara

28 November  1995


This  is an introduction for people who want to program in assembler language.

Copyright (C) 1995, Hugo Perez Perez.   Anyone  may  reproduce  this document,  in  whole  or  in  part,  provided  that:   (1) any copy or republication of the entire document must show University of Guadalajara  as
the  source,  and  must  include this notice; and (2) any other use of this material must reference this manual and University of Guadalajara,  and the fact that the material is copyrighted by Hugo Perez and is used by permission.

























ASSEMBLY LANGUAGE TUTORIAL

Introduction.

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

The tutorial is completely focused towards computers that function with
processors of the x86 family of Intel.  Considering 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 flowing 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, and then continues with the tutorial itself. 


CONTAINED:

   Basic description of a computer system..........................................................3

   Why learn assembly language?.............................................................................6 
 
AN APPROACH TO ASSEMBLY LANGUAGE.

This first part is focused on the knowledge of some of the characteristics of
computers and of the assembly language.

   UNIT 1: Basic concepts ............................................................................................7
   UNIT 2: Assembler programming ..........................................................................23
        
                                           Copyright. 1995                                                 1
                                                                                       

                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


 
ASSEMBLY LANGUAGE INSTRUCTIONS

In this second part, it is deepened more into the description of the assembly
language instructions. 

   UNIT 3: Data operation instructions .................................................................36
   UNIT 4: Logic and arithmetic instructions .....................................................45
   UNIT 5: Process control instructions ................................................................54



INTERRUPTIONS AND FILE MANAGING.

   UNIT 6: Interruptions .............................................................................................72
   UNIT 7: File Managing ............................................................................................88

MACROS AND PROCEDURES

   UNIT 8: Macros and procedures .........................................................................94


Examples ..........................................................................................................................1OO

 Displaying a message on screen
 Displaying hexadecimal numbers from 15 to 0
 Basic Operations


DIRECTORY AND BIBLIOGRAPHY ...........................................................................111

COMMENTARIES AND SUGGESTIONS ....................................................................112



                                            Copyright. 1995                                                 2


                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


BASIC DESCRIPTION OF A COMPUTER SYSTEM.



This section has the purpose of giving a brief outline of the main components a of computer system at a basic level, which will allow the user a greater understanding of the concepts which will be dealt with throughout the tutorial.


   Central Processor 
   Central Memory 
   Input and Output Units 
   Auxiliary Memory Units 



Computer System.

The term computer system refers 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.



  Central Processor.

This part is also known as central processing unit or CPU, which in turn is made up 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 decodify 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.


                                           Copyright. 1995                                                  3


                              

                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


  These cells are known with the name of registers.  A processor may have one or two dozens 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 the 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 contents 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. 



   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 directional, that is  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.




                                           Copyright. 1995                                                     4                         



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



  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 more common input units are keyboards and mice.  The more common output units are screens and printers.

    Auxiliary Memory Units.

The central memory of a computer is very costly.  Considering today's
memory hungry applications, 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 inconveniences 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 magnetic discs and tapes.

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



                                                             
                        
                                           Copyright.  1995                                                 5




                                 UNIVERSITY OF GUADALAJARA 

                       Information Systems General Coordination.


Advantages of the Assembler


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

The second reason is it provides for the total control of the PC.

Third, 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.




















                                                          Copyright. 1995                                                6




                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


Basic concepts.



   Information in the computers. 

      Information units 
      Numeric systems 
      Converting binary numbers to decimal 
      Converting decimal numbers to binary 
      Hexadecimal system 

   Data representation Methods in a computer. 

      ASCII code 
      BCD method 
      Floating point representation 

   Working with the assembly language. 

      Program creation process 
      CPU registers 
      Assembler structure 
      Our first program 
      Storing and loading the programs 



   Information Units 


In order for the PC to process information, it is necessary that this information
be in special cells called registers.

                                           Copyright.1995                                                     7    



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


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, regularly 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.



   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. 

   Converting binary numbers to decimals 


                                           Copyright.1995                                                   8



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


When working with assembly language we come upon 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 process 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
occupy:

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.


   Converting decimal numbers to binary 


There are several methods to convert decimal numbers to binary however 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.

                                           Copyright.1995                                                   9




                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


Let us take for example the decimal number of 43.


43/2=21 and its remainder is 1

21/2=10 and its remainder is 1

10/2=5 and its remainder is 0

5/2=2 and its remainder is 1

2/2=1 and its remainder is 0

1/2=0 and its remainder is 1

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



   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.

                                           Copyright.1995                                                   10



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


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



   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.
                                           Copyright.1995                                                   11



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.



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.



   BCD Method 


BCD is an acronym for 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 though 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 merchantile
applications since it facilitates operations avoiding mistakes.





                                           Copyright.1995                                                  12              



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


   Floating point representation 


This representation is based on scientific notation, that 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  indicate to us the number of
spaces that the decimal point must be moved to the left to obtain the original
result.



   Program creation process 


For the creation of a program it is necessary to follow five steps: design of the
algorithm, coding of the algorithm, translation to machine language, test and
depuration of the program.

      On the design stage the problem to be solved is established and the best
   solution is proposed, creating schematic diagrams used for the better
   solution proposal. 
      The coding of the program consists in writing the program in some
   programming language; assembly language in this specific case, taking as a
   base the proposed solution on the prior step. 
      The translation to machine language is the creation of the object program,
   in other words, the written program as a sequence of zeros and ones that can
   be interpreted by the processor. 


                                           Copyright.1995                                                  13



                                UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


      The last stage is the elimination of detected faults on the program on the
   test stage. The correction of a fault normally requires the repetition of all
   the steps from the first or second. 

To create a program in assembler two options exist, the first one is to a program such as
MASM or Macro Assembler by Microsoft, and the second one is to use the debugger - on this first section we will use this latter since it is found in all PC's with MS-DOS.

Debug can only create files with a .COM extension, and because of the
characteristics of these kinds of programs they cannot be larger that 64 kb. They also must start with displacement, offset, or 0100H memory direction inside the specific segment.



   CPU Registers 


The CPU has 4 internal registers, each one of 16 bits. The first four, AX, BX,
CX, and DX are general use registers and can also be used as 8 bit registers, if
used in such a way it is necessary to refer to them for example as: AH and AL,
which are the high and low bytes of the AX register. This nomenclature is also
applicable to the BX, CX, and DX registers.

Registers known by their specific names:

   AX Accumulator 
   BX Base register 
   CX Counting register 
   DX Data register 
   DS Data segment register

                                           Copyright.1995                                                  14



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.

 
   ES Extra segment register 
   SS Battery segment register 
   CS Code segment register 
   BP Base pointers register 
   SI Source index register 
   DI Destiny index register 
   SP Battery pointer register 
   IP Next instruction pointer register 
   F Flag register 

It is possible to visualize the values of the internal registers of the CPU using
the Debug program. To begin working with Debug, at the prompt type the following:

C:/> Debug [Enter]

On the next line a dash will appear, this is the indicator of Debug, at this
moment the instructions of Debug can be introduced using the following command:

-r[Enter]

All the contents of the internal registers of the CPU are displayed; an
alternative of viewing them is to use the "r" command using as a parameter the name of the register whose value you want to see. For example:

-rbx

This instruction will only display the content of the BX register and the Debug
indicator changes from "-" to ":"




                                           Copyright.1995                                                  15



                                UNIVESITY OF GUADALAJARA

                        Information Systems General Coordination.


When the prompt is like this, it is possible to change the value of the register
which was seen by typing the new value and [Enter], or the old value can be left by pressing [Enter] without typing any other value.

It is possible to change the value of the flag register, and use it as a control
structure in our programs as we will later see. Each bit of the register has a
special name and meaning.  The following list describes the value of each bit, on
or off and its relation with the operations of the processor:

Overflow 

      NV = there is no overflow 
      OV = there is an overflow 

Direction 

      UP = forward 
      DN = backward 

Interrupts 

      DI = deactivated 
      EI = activated 

Sign 

      PL = positive 
      NG = negative 

Zero 

      NZ = it is not zero 
      ZR = it is zero 

                                           Copyright.1995                                                   16



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


Auxiliary Carry 

      NA = there is no auxiliary carry 
      AC = there is an auxiliary carry 

Parity 

      PO = uneven parity 
      PE = even parity 

Carry 

      NC = there is no carry 
      CY = there is a carry 



   Assembler structure 


In assembly language code lines have two parts, the first one is the name of the instruction which is to be executed, and the second one are the parameters of the command. For example:

add ah,bh

Here "add" is the command to be executed, in this case an addition, and "ah" as
well as "bh" are the parameters.

The name of the instructions in this language is made up of two, three, or four
letters.  These instructions are also called mnemonic names or operation codes,
since they represent a function the processor will perform.


                                           Copyright.1995                                                  17



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


There are some commands which do not require parameters for their operation, as well as others that  require just one parameter.

Sometimes instructions are used as follows:

add al,[170]

The brackets in the second parameter indicate to us that we are going to work
with the content of the memory cell number 170 and not with the 170 value, this is known as direct directioning.



   Our first program 


We are going to create a program that will illustrate what we have been
seeing.  The program will add two values that we will directly introduce
into the code:

The first step is to initiate Debug, this step only consists of typing
debug[Enter] at the command prompt.

To assemble a program in Debug, the "a" (assemble) command is used; when this command is used, the address where you want the assembling to begin can be given as a parameter, if the parameter is omitted the assembling will be initiated at the locality specified by CS:IP, usually 0100h, which is the
locality where programs with .COM extension must be initiated.  This will be
the place we will use since only Debug can create this specific type of
program.




                                           Copyright.1995                                                   18



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


Even though at this moment it is not necessary to give the "a" command a
parameter, it is recommendable to do so to avoid problems once the CS:IP
registers are used, therefore we type:

-a0100[Enter]

When this is done something like this will appear on the screen: 0C1B:0100 and the cursor will be positioned to the right of these numbers.  Note that the first four digits, in hexadecimal, can be different, but the last four must be 0100, since it is the address we indicated as the  beginning.  Now we can introduce the instructions: 

0C1B:0100 mov ax,0002; puts the 0002 value on the ax register
0C1B:0103 mov bx,0004; puts the 0004 value on the bx register
0C1B:0106 add ax,bx; the content of bx is added to the content of ax
0C1B:0108 INT 20; provokes the termination of the program.
0C1B:010A

It is not necessary to write the comments which go after the ";", but it is very helpful. Once the last
command has been typed, INT 20, [Enter] is pressed without writing anything
more, to see the Debug prompt again.

The last written line is not a proper assembler instruction, instead it is a
call for an operative system interruption (these interruptions will be dealt
with more in depth on a later chapter).  For the moment it only necessary to know they save us a great deal of lines and are very useful to access operative
system functions.

To execute the program we wrote the "g" command is used.  When used we will see a message that says: "Program terminated normally".  Naturally, with a message like this one, we cannot be sure the program has done the addition, but there is a simple way to verify it.  By using the "r" command of Debug we can see the contents of all the registers of the processor, simply type:

                                           Copyright.1995                                                  19



                                UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


-r[Enter]

Each register with its respective actual value will appear on the screen:

AX=0006BX=0004CX=0000DX=0000SP=FFEEBP=0000SI=0000DI-0000
DS=0C1BES=0C1BSS=0C1BCS=0C1BIP=010A NV UP EI
PL NZ NA PO NC
0C1B:010A OF DB oF

The possibility that the registers contain different values exists, but AX and
BX must be the same, since they are the ones we just modified.

Another way to see the values, while the program is executed, is to use the
address where we want the execution to end and show the values of the registers as a parameter for "g", in this case it would be: g108.  This instruction
executes the program then stops on the 108 address and shows the contents of the registers.

A follow up of what is happening in the registers can be done by using the "t"
command (trace).  The function of this command is to execute line by line what
was assembled, showing each time the contents of the registers.

To exit Debug use the "q" (quit) command.



   Storing and loading programs 


It would not seem practical to type an entire program each time it is needed.
To avoid this it is possible to store a program on disk, with the
enormous advantage that by being already assembled it will not be necessary to run Debug again to execute it.

                                           Copyright.1995                                                   20



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


The steps to save a program that it is already stored in memory are:

   1.  Obtain the length of the program by subtracting the final address from the
        initial address, naturally in hexadecimal system.  You can use Debug's H 
        command to accomplish the math.  -H 102h 01Ah would give you both the 
        sum and difference of these numbers.
   2.  Give the program a name and extension.  Use Debug's N (name) command.  Such as -N program.com.
   3.   Put the length of the program into the CX register. -R CX E8 (E8 for length).   
   4.  Order Debug to write the program to disk with the W (write) command. 
        -W

By using the program from the prior chapter, we will have a
clearer idea of how to take these steps:

When the program is finally assembled it would look like this:

0C1B:0100 mov ax,0002
0C1B:0103 mov bx,0004
0C1B:0106 add ax,bx
0C1B:0108 INT 20
0C1B:010A
-h 10a 100
020a 000a
-n test.com
-rcx
CX 0000
:000a
-w
Writing 000A bytes

To obtain the length of a program the "h" command is used, since it will show us the addition and subtraction of two numbers in hexadecimal. To obtain the
length of ours, we give it as parameters the value of our program's final
address (10A), and the program's initial address (100). The first result the
command shows us is the addition of the parameters and the second is the
subtraction.

                                           Copyright.1995                                                   21



                               UNIVERSITY OF GUADALAJARA 

                        Information Systems General Coordination.


The "n" command allows us to name the program.

The "rcx" command allows us to change the content of the CX register to the
value we obtained from the size of the file with "h", in this case 000a, since
the result of the subtraction of the final address from the initial address.

Lastly, the "w" command writes our program on the disk, indicating how many
bytes it wrote.

To save an already loaded file two steps are necessary:

      Give the name of the file to be loaded. 
      Load it using the "l" (load) command. 

To obtain the correct result of the following steps, it is necessary that the
above program be already created.

Inside Debug we write the following:

-n test.com
-l
-u 100 109
0C3D:0100 B80200 MOV AX,0002
0C3D:0103 BB0400 MOV BX,0004
0C3D:0106 01D8 ADD AX,BX
0C3D:0108 CD20 INT 20

The last "u" command is used to verify that the program was loaded into memory.  It disassembles the code and shows it disassembled.  The parameters indicate to Debug from where and to where to disassemble.

Debug always loads the programs into memory on the address 100H, unless otherwise
indicated.
                                           Copyright.1995                                                   22



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


ASSEMBLER PROGRAMING



   Requirements for programming in assembly language. 

      Needed software 
      Utilization of the MASM 
      Linker use 

  Format of a program in assembler. 

      Internal format 
      External format 
      Practical example of a program 

  Assembly process. 

      Segments 
      Table of symbols 

  Types of instructions. 

      Data movement 
      Logic and arithmetic operations 
      Jumps, loops and procedures 








                                           Copyright.1995                                                  23



                               UNIVERSITY OF GUADALAJARA 

                       Information Systems General Coordination.



   NEEDED SOFTWARE


In order to be able to create a program several tools are needed:

First an editor to create the source program.  Second a compiler, which is
nothing more than a program that "translates" the source program into an object program.  And third, a linker that generates the executable program from the object program.

The editor can be any text editor at hand, and as a compiler we will use the
MASM, macro assembler from Microsoft, since it is the most common, and as a
linker we will use the Link program.

The extension used so that MASM recognizes the source programs in assembler is. ASM; once translated the source program, the MASM creates a file with the .OBJ extension, this file contains an "intermediate format" of the program, called this because it is neither executable nor is it a program in source language.  The linker generates, from a . OBJ or a combination of several of these files, an executable program, whose extension usually is .EXE though it can also be .COM, depending on the form it was assembled.

This tutorial describes the way to work with the 5.0 or later version of the
MASM. The main difference of this version with the ones before it is the way in
which the code, data, and stack segments are declared.  The structure of the
programming is the same.





                                           Copyright.1995                                                   24




                                UNIVERSITY OF GUADALAJARA 

                        Information  Systems General Coordination. 


   UTILIZATION OF THE MASM


Once the object program has been created it must be passed to the MASM to create the intermediate code, which remains in a file with an .OBJ extension. The command to do this is:

MASM Name_File; [Enter]

Where Name_File is the name of the source program with the .ASM extension that will be translated.  The semicolon used after the name of the file indicates to the macro assembler to directly generate the intermediate code, and in case of omitting it, the MASM will ask for the name of the file it will translate, the
name of the file which will be generated as well as options of information
listing that it can give to the translator.

It is possible to execute the MASM using various parameters to obtain a determined
goal.  The entire list can be found in the manual of the program.  I will only
remind you in this tutorial to pass such parameters to the MASM.

All parameters come after the symbol "/".  It is possible to utilize several
parameters at a time.  Once all the parameters have been typed in, the name of
the file to be assembled is written.  For example, if we want the MASM to assemble a program called "test", and we also want it to display the number of source lines and processed symbols, then we do this with the /v parameter.  Then to also tell us if a mistake occured and on which line it occurred, the /z parameter is used.
The entire command would be:

MASM /v /z test;




                                           Copyright.1995                                                   25




                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


   USE OF THE LINKER


The MASM can only create programs in .OBJ format.  These are not executable by themselves.  It is necessary to have a linker which generates the
executable code.

The use of the linker is very similar to the use of the MASM, and it is only
typed on the DOS indicator:

LINK Name_File;

Where Name_File is the name of the intermediate program, .OBJ. This generates a file directly with the name of the intermediate program and the .EXE extension.


   INTERNAL FORMAT OF A PROGRAM


In order to communicate in any language, including programming languages, it is
necessary to follow a set of rules, or on the contrary we would no be able to
express what we wish.

In this section we will see some of the rules we must follow to write a program in assembly language. We will focus on the way to write the instructions so that the assembler will be able to interpret them.

Basically the format of a code line in assembly language has four parts:

*Label, variable or constant: This is not always defined; if it is defined it is
necessary to use separators to differentiate it from the other parts, usually spaces
or some special symbol.

                                           Copyright.1995                                                    26
 


                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination



*Directive or instruction: This is the name with which the instruction we want to
execute is called.

*Operator(s): The majority of the instructions in assembler work with two
operators (there are instructions which work with only one). The
first one normally is the destiny operator, which is the deposit of the result
of an operation; and the second one is the source operator, which takes the data to be processed.  The operators are separated by means of a comma ",".

*Comment: As its name indicates, it is only an informative writing, mainly
used to explain what the program is doing on a determined line.  It is separated
from the others by means of a semicolon";". This part is not necessary on the
program, but it helps us to depurate the program in case of errors and
modifications.

As an example we can see a line of a program written in assembler:

Etiq1: MOV AX,001AH ; Initializes AX with the value 001A

Here we have the label "Etiq1", identifiable as a label by the final symbol":",
the instruction "MOV", and the operators "AX" as destiny and "001A" as source,
besides the comments which follows the ";".

An example of a declaration of a constant is given by:

ONE EQU 0001H

Where "ONE" is the name of the constant we define, "EQU" is the directive
utilized to use "ONE" as a constant, and "0001H" is the operator which in this
case will be the value that "ONE" keeps.



                                           Copyright.1995                                                   27



                                UNIVERSITY OF GUADALAJARA
 
                        Information Systems General Coordination.



   EXTERNAL FORMAT OF A PROGRAM


Apart from defining certain rules so that the assembler can understand an
instruction, it is necessary to give it certain information of the resources to
be used, for example the memory segments which will be used, initial data of the program and also where does our code begin and where it ends.

A simple program could be the following;

  .MODEL SMALL
  .CODE
  Program:
  MOV AX,4C00H
  INT 21H
  .STACK
  END Program

The program does not really do anything.  It only puts the 4C00H value on the AX register, so that the 21H interruption ends the program.  It does however give us an idea of the external format of an assembler program.

The .MODEL directive defines the kind of memory which will be used; the .CODE directive indicates that what is next is our program; the Program label
indicates to the assembler the beginning of the program; the .STACK directive
asks the assembler to reserve a space of memory for the stack operations; the
"END Program" instruction marks the end of the program.





                                           Copyright.1995                                                   28




                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.



   PRACTICAL EXAMPLE OF A PROGRAM


This is an example of a program which will write a chain on the screen:

  .MODELSMALL
  .CODE
  Program:
  MOV AX, @DATA
  MOV DS, AX
  MOV DX, Offset Text
  MOV AH,9
  INT 21H
  MOV AX,4C00H
  INT 21H
  .DATA
  Text DB'Message on screen.$'
  .STACK
  END Program

The first steps are the same as the ones from the previous program: the memory
model is defined, it is indicated where the program code begins and where the
instructions begin.

Next @DATA is placed on the AX register to later pass it to the DS register
since a constant cannot be copied directly to a segment register. The content of
@DATA is the number of the segment which will be used for the information. Then a value given by "Offset Text is kept on the DX register, which gives us the address where the chain of characters is found on the data segment. 




                                           Copyright.1995                                                   29



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


Then it uses the 9 option, given by the value of AH, of the 21H interruption to display the positioned chain of the address which contains DX. Lastly it uses the 4CH option of the 21H interruption to end the execution of the program, even though we loaded the 4C00H value to the AX register the 21H interruption only takes as an option the content of the AH register.

The .DATA directive indicates to the assembler that what is written next it must store it on the memory segment destined for the data. The DB directive is used to Define Bytes, this is, to assign a value to a certain identifier.  In this
case "Text", be it a constant or a chain of characters, which in this last case
it will have to be between simple quotation marks ' and end with the "$" symbol.



   SEGMENTS


The architecture of the x86 processors forces us to use memory segments to manage the information.  The size of these segments is 64kb.

The reason for using these segments is that, considering that the maximum size of a number that the processor can manage is given by a word of 16 bits or register, it would not be possible to access more than 65536 localities of
memory using only one of these registers, but now, if the PC's memory is divided into groups or segments, each one of 65536 localities, and we use an address on an exclusive register to find each segment, and then we make each address of a specific slot with two registers, it is possible for us to access a quantity of 4294967296 bytes of memory, which is, in the present day, more memory than what we will see installed in a PC.




                                           Copyright.1995                                                  30



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


In order for the assembler to be able to manage the data, it is necessary that
each piece of information or instruction be found in the area that corresponds
to its respective segments.  The assembler accesses this information, taking into
account the localization of the segment, given by the DS, ES, SS and CS
registers and inside the register the address of the specified piece of
information. It is because of this that when we create a program using the Debug on each line that we assemble, something like this appears:

1CB0:0102 MOV AX,BX

Where the first number, 1CB0, corresponds to the memory segment being used, the second one refers to the address inside this segment, and the instructions which will be stored from that address follow.

The way to indicate to the assembler with which of the segments we will work
with is with the .CODE, .DATA and .STACK directives.

The assembler adjusts the size of the segments taking as a base the number of
bytes each assembled instruction needs, since it would be a waste of memory to use the whole segments.  For example, if a program only needs 10kb to store data, the data segment will only be of 10kb and not the 64kb it can handle.



   SYMBOLS CHART


Each one of the parts on code line in assembler is known as token, for example
on the code line:

MOV AX,Var



                                           Copyright.1995                                                  31



                                UNIVERSITY OF GUADALAJARA

                        information Systems General Coordination.


we have three tokens, the MOV instruction, the AX operator, and the VAR
operator. What the assembler does to generate the OBJ code is to read each one
of the tokens and look for it on an internal "equivalence" chart known as the
reserved words chart, which is where all the mnemonic meanings we use as
instructions are found.

Following this process, the assembler reads MOV, looks for it on its chart and
identifies it as a processor instruction.  Likewise it reads AX and recognizes it
as a register of the processor, but when it looks for the Var token on the
reserved words chart, it does not find it, so then it looks for it on the
symbols chart which is a table where the names of the variables, constants and labels used in the program where their addresses on memory are included and the sort of data it contains, are found.

Sometimes the assembler comes upon a token which is not defined by the program, therefore it then passes a second time through the source program to verify all references to that symbol and places it on the symbols chart. There are symbols which the assembler will not find since they do not belong to that segment and the program does not know in what part of the memory it will find that segment, and at this time the linker comes into action, which will create the structure necessary for the loader so that the segment and the token be defined when the program is loaded and before it is executed.



   DATA MOVEMENT


In any program it is necessary to move the data in the memory and in the CPU
registers.  There are several ways to do this: it can copy data in the memory to
some register, from register to register, from a register to a stack, from a
stack to a register, to transmit data to external devices as well as vice versa.

                                           Copyright.1995                                                  32



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


This movement of data is subject to rules and restrictions. The following are
some of them:

*It is not possible to move data from a memory locality to another directly; it
is necessary to first move the data of the origin locality to a register and then
from the register to the destination locality.

*It is not possible to move a constant directly to a segment register; it first
must be moved to a register in the CPU.

It is possible to move data blocks by means of the movs instructions, which
copies a chain of bytes or words; movsb which copies n bytes from a locality to
another; and movsw copies n words from a locality to another. The last two
instructions take the values from the defined addresses by DS:SI as a group of
data to move and ES:DI as the new localization of the data.

To move data there are also structures called batteries, where the data is
introduced with the push instruction and are extracted with the pop instruction.

In a stack the first data to be introduced is the last one we can take, this is,
if in our program we use these instructions:

  PUSH AX
  PUSH BX
  PUSH CX

To return the correct values to each register at the moment of taking them from the stack it is necessary to do it in the following order: 

  POP CX
  POP BX
  POP AX

                                           Copuright.1995                                                   33



                                UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


For the communication with external devices the out command is used to send
information to a port and the in command to read the information received from a port.

The syntax of the out command is:

OUT DX,AX

Where DX contains the value of the port which will be used for the communication and AX contains the information which will be sent.

The syntax of the in command is:

IN AX,DX

Where AX is the register where the incoming information will be kept and DX
contains the address of the port by which the information will arrive.



   LOGIC AND ARITHMETIC OPERATIONS


The instructions of the logic operations are: and, not, or and xor. These work
on the bits of their operators.

To verify the result of the operations we turn to the cmp and test instructions.

The instructions used for the algebraic operations are: to add add, to subtract
sub, to multiply mul and to divide div.

Almost all the comparison instructions are based on the information contained in the flag register. 

                                           Copyright.1995                                                   34



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


Normally the flags of this register which can be directly handled by the programmer are the data direction flag DF, used to define the operations about chains. Another one which can also be handled is the IF flag by means of the sti and cli instructions, to activate and deactivate the interruptions.



   JUMPS, CYCLES AND PRODEDURES


The unconditional jumps in a written program in assembler language are given by the jmp instruction; a jump is to alter the flow of the execution of a
program by sending the control to the indicated address.

A loop, known also as iteration, is the repetition of a process a certain number
of times until a condition is fulfilled. These loops are used 

<<<< Missing a part >>>> 















                                           Copyright.1995                                                   35




                                          UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.
                   

DATA OPERATION INSTRUCTIONS



   TRANSFER INSTRUCTIONS

They are used to move the contents of the operators. Each instruction can be
used with different modes of directioning. 

      MOV 
      MOVS (MOVSB) (MOVSW) 

   LOADING INSTRUCTIONS

These are specific register instructions. They are used to load bytes or chains
of bytes onto a register. 

      LODS (LODSB) (LODSW) 
      LAHF 
      LDS 
      LEA 
      LES 

   STACK INSTRUCTIONS

These instructions allow the use of the STACK to store or retrieve data. 

      POP 
      POPF 
      PUSH 
      PUSHF 


                                           Copyright.1995                                                   36




                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


   MOV INSTRUCTION 


Purpose: Data transfer between memory cells, registers and the accumulator.

Syntax:

MOV Destination , Source

Where Destiny is the place where the data will be moved and Source is the place where the data is.

The different movements of data allowed for this instruction are:

*Destiny:  memory.  Source:  accumulator
*Destiny:  accumulator.  Source:  memory
*Destiny:  segment register.  Source:  memory/register
*Destiny:  memory/register.  Source:  segment register
*Destiny:  register.  Source:  register
*Destiny:  register.  Source:  memory
*Destiny:  memory.  Source:  register
*Destiny:  register.  Source:  immediate data
*Destiny:  memory.  Source:  immediate data


Example:

  MOV AX,0006h
  MOV BX,AX
  MOV AX,4C00h
  INT 21H



                                            Copyright.1995                                                  37



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


This small program moves the value of 0006H to the AX register, then it moves the content of AX (0006h) to the BX register, and lastly it moves the 4C00h value to the AX register to end the execution with the 4C option of the 21h interruption.



   MOVS (MOVSB) (MOVSW) Instruction 


Purpose: To move byte or word chains from the source, directed by SI, to the destiny directed by DI.

Syntax:

MOVS

This command does not need parameters since it takes as source address the
content of the SI register and as destination the content of DI. The following
sequence of instructions illustrates this:

MOV SI, OFFSET VAR1
MOV DI, OFFSET VAR2
MOVS

First we initialize the values of SI and DI with the addresses of the VAR1 and
VAR2 variables respectively, then after executing MOVS the content of VAR1 is copied onto VAR2.

The MOVSB and MOVSW are used in the same way as MOVS, the first one moves one byte and the second one moves a word.



                                           Copyright.1995                                                   38




                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


   LODS (LODSB) (LODSW) INSTRUCTION


Purpose: To load chains of a byte or a word into the accumulator.

Syntax: 

LODS

This instruction takes the chain found on the address specified by SI, loads it
to the AL (or AX) register and adds or subtracts , depending on the state of DF,
to SI if it is a bytes transfer or if it is a words transfer.

MOV SI, OFFSET VAR1
LODS

The first line loads the VAR1 address on SI and the second line takes the
content of that locality to the AL register.

The LODSB and LODSW commands are used in the same way, the first one loads a byte and the second one a word (it uses the complete AX register).



   LAHF INSTRUCTION 


Purpose: It transfers the content of the flags to the AH register.

Syntax:

LAHF

                                           Copyright.1995                                                  39



                                UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


This instruction is useful to verify the state of the flags during the execution
of our program.

The flags are left in the following order inside the register:

SF ZF ?? AF ?? PF ?? CF

The "??" means that there will be an undefined value in those bits.



   LDS INSTRUCTION 


Purpose: To load the register of the data segment

Syntax:

LDS destination , source

The source operator must be a double word in memory. The word associated with the largest address is transferred to DS, in other words it is taken as the
segment address. The word associated with the smaller address is the
displacement address and it is deposited in the register indicated as destiny.



   LEA INSTRUCTION 


Purpose: To load the address of the source operator



                                           Copyright.1995                                                  40



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.

Syntax:

LEA destination , source

The source operator must be located in memory, and its displacement is placed on the index register or specified pointer in destiny.

To illustrate one of the facilities we have with this command let us write an
equivalence:

MOV SI,OFFSET VAR1

Is equivalent to: 

LEA SI,VAR1

It is very probable that for the programmer it is much easier to create extense
programs by using this last format.



   LES INSTRUCTION 


Purpose: To load the register of the extra segment

Syntax:

LES destination , source

The source operator must be a double word operator in memory. The content of the word with the larger address is interpreted as the segment address and it is placed in ES. The word with the smaller address is the displacement address and it is placed in the specified register on the destiny parameter.

                                           Copyright.1995                                                   41


     
                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.



   POP INSTRUCTION 


Purpose: It recovers a piece of information from the stack

Syntax:

POP destiny

This instruction transfers the last value stored on the stack to the destiny
operator, it then increases by 2 the SP register.

This increase is due to the fact that the stack grows from the highest memory
segment address to the lowest, and the stack only works with words, 2 bytes, so then by increasing by two the SP register, in reality two are being subtracted from the real size of the stack.



   POPF INSTRUCTION 


Purpose: It extracts the flags stored on the stack

Syntax:

POPF

This command transfers bits of the word stored on the higher part of the stack
to the flag register.


                                           Copyright.1995                                                   42




                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


The way of transference is as follows:

BIT    FLAG

  0     CF
  2     PF
  4     AF
  6     ZF
  7     SF
  8     TF
  9     IF
 10     DF
 11     OF

These localities are the same for the PUSHF command.

Once the transference is done the SP register is increased by 2, diminishing the
size of the stack.



   PUSH INSTRUCTION 


Purpose: It places a word on the stack.

Syntax:

PUSH source

The PUSH instruction decreases by two the value of SP and then it transfers the
content of the source operator to the new resulting address on the recently
modified register.

                                           Copyright.1995                                                   43



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.



   The decrease on the address is due to the fact that when adding values to the stack, this one grows from the greater to the smaller segment address, therefore by subtracting 2 from the SP register what we do is to increase the size of the stack by two bytes, which is the only quantity of information the stack can handle on each input and output of information.



   PUSHF INSTRUCTION 


Purpose: It places the value of the flags on the stack.

Syntax:

PUSHF


This command decreases by 2 the value of the SP register and then the content of the flag register is transferred to the stack, on the address indicated by SP.

The flags are left stored in memory on the same bits indicated on the POPF
command.



  




                                           Copyright.1995                                                  44




                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



LOGIC AND ARITHMETIC INSTRUCTIONS 



   LOGIC INSTRUCTIONS.

They are used to perform logic operations on the operators. 

      AND 
      NEG 
      NOT 
      OR 
      TEST 
      XOR 



   ARITHMETIC INSTRUCTIONS. 

They are used to perform arithmetic operations on the operators. 

      ADC 
      ADD 
      DIV 
      IDIV 
      MUL 
      IMUL 
      SBB 
      SUB 



                                           Copyright.1995                                                  45




                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   AND INSTRUCTION 


Purpose: It performs the conjunction of the operators bit by bit.

Syntax:

AND destination , source

With this instruction the "y" logic operation for both operators is carried out:

Source   Destiny  |   Destiny
-----------------------------
  1         1     |     1
  1         0     |     0
  0         1     |     0
  0         0     |     0

The result of this operation is stored on the destiny operator. 


   NEG INSTRUCTION 


Purpose: It generates the complement to 2.

Syntax:

NEG destiny

This instruction generates the complement to 2 of the destiny operator and
stores it on the same operator. For example, if AX stores the value of 1234H,
then:
                                           Copyright.1995                                                   46



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



NEG AX

This would leave the EDCCH value stored on the AX register.



   NOT INSTRUCTION 


Purpose: It carries out the negation of the destiny operator bit by bit.

Syntax:

NOT destiny

The result is stored on the same destiny operator.



   OR INSTRUCTION


Purpose: Logic inclusive OR 

Syntax:

OR destination , source




                          
                                           Copyright.1995                                                  47




                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


The OR instruction carries out, bit by bit, the logic inclusive disjunction of
the two operators:


Source     Destiny    |    Destiny
-----------------------------------
   1          1       |       1
   1          0       |       1
   0          1       |       1
   0          0       |       0




   TEST INSTRUCTION


Purpose: It logically compares the operators

Syntax:

TEST destination , source

It performs a conjunction, bit by bit, of the operators, but differing from AND,
this instruction does not place the result on the destiny operator, it only has
effect on the state of the flags.



   XOR INSTRUCTION


Purpose: OR exclusive

                                           Copyright.1995                                                    48



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



Syntax: 

XOR destination , source Its function is to perform the logic exclusive disjunction of
the two operators bit by bit.


Source     Destiny    |    Destiny
-----------------------------------
   1          1       |       0
   0          0       |       1
   0          1       |       1
   0          0       |       0




   ADC INSTRUCTION


Purpose: Cartage addition

Syntax: 

ADC destination , source

It carries out the addition of two operators and adds one to the result in case
the CF flag is activated, this is in case there is catage.

The result is stored on the destiny operator.



                                           Copyright.1995                                                   49




                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   ADD INSTRUCTION


Purpose: Addition of the operators.

Syntax:

ADD destination , source

It adds the two operators and stores the result on the destiny operator.



   DIV INSTRUCTION


Purpose: Division without sign.

Syntax:

DIV source

The divider can be a byte or a word and it is the operator which is given the
instruction.

If the divider is 8 bits, the 16 bits AX register is taken as dividend and if the
divider is 16 bits the even DX:AX register will be taken as dividend, taking the
DX high word and AX as the low.

If the divider was a byte then the quotient will be stored on the AL register
and the residue on AH, if it was a word then the quotient is stored on AX and
the residue on DX.

                                           Copyright.1995                                                   50



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   IDIV INSTRUCTION


Purpose: Division with sign.

Syntax:

IDIV source

It basically consists on the same as the DIV instruction, and the only
difference is that this one performs the operation with sign.

For its results it used the same registers as the DIV instruction.



   MUL INSTRUCTION


Purpose: Multiplication with sign.

Syntax:

MUL source

The assembler assumes that the multiplicand will be of the same size as the
multiplier, therefore it multiplies the value stored on the register given as
operator by the one found to be contained in AH if the multiplier is 8 bits or
by AX if the multiplier is 16 bits.

When a multiplication is done with 8 bit values, the result is stored on the AX
register and when the multiplication is with 16 bit values the result is stored
on the even DX:AX register.
                                           Copyright.1995                                                  51



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   IMUL INSTRUCTION


Purpose: Multiplication of two whole numbers with sign.

Syntax:

IMUL source

This command does the same as the one before, only that this one does take into account the signs of the numbers being multiplied.

The results are kept in the same registers that the MOV instruction uses.



   SBB INSTRUCTION


Purpose: Subtraction with cartage.

Syntax:

SBB destination , source

This instruction subtracts the operators and subtracts one to the result if CF
is activated. The source operator is always subtracted from the destiny.

This kind of subtraction is used when one is working with 32 bits quantities.



                                           Copyright.1995                                                   52




                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   SUB INSTRUCTION


Purpose: Subtraction.

Syntax:

SUB destination , source

It subtracts the source operator from the destiny.



    

















                                           Copyright.1995                                                   53





                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



PROCESS CONTROL INSTRUCTIONS 



   JUMP INSTRUCTIONS 


They are used to transfer the flow of the process to the indicated operator. 

      JMP 
      JA (JNBE) 
      JAE (JNBE) 
      JB (JNAE) 
      JBE (JNA) 
      JE (JZ) 
      JNE (JNZ) 
      JG (JNLE) 
      JGE (JNL) 
      JL (JNGE) 
      JLE (JNG) 
      JC 
      JNC 
      JNO 
      JNP (JPO) 
      JNS 
      JO 
      JP (JPE) 
      JS 





                                           Copyright.1995                                                   54



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


   INSTRUCTIONS FOR CYCLES: LOOP 

They transfer the process flow, conditionally or unconditionally, to a destiny,
repeating this action until the counter is zero. 

      LOOP 
      LOOPE 
      LOOPNE 

   COUNTING INSTRUCTIONS

They are used to decrease or increase the content of the counters. 

      DEC 
      INC 

   COMPARISON INSTRUCTIONS

They are used to compare operators, and they affect the content of the flags. 

      CMP 
      CMPS (CMPSB) (CMPSW) 

   FLAG INSTRUCTIONS

They directly affect the content of the flags. 

      CLS 
      CLD 
      CLI 
      CMC 
      STC 
      STD 
      STI 
                                          Copyright.1995                                                     55


  
                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



 JMP INSTRUCTION


Purpose: Unconditional jump.

Syntax:

JMP destiny

This instruction is used to deviate the flow of a program without taking into
account the actual conditions of the flags or of the data.



   JA (JNBE) INSTRUCTION


Purpose: Conditional jump.

Syntax:

JA Label


After a comparison this command jumps if it is up or jumps if it is not down or
if not it is the equal.

This means that the jump is only done if the CF flag is deactivated or if the ZF
flag is deactivated, that is that one of the two be equal to zero.




                                           Copyright.1995                                                  56



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




   JAE (JNB) INSTRUCTION


Purpose: Conditional jump.

Syntax:

JAE label


It jumps if it is up or it is the equal or if it is not down.

The jump is done if CF is deactivated.



   JB (JNAE) INSTRUCTION


Purpose: Conditional jump.

Syntax:

JB label


It jumps if it is down, if it is not up, or if it is the equal.

The jump is done if CF is activated.


                                           Copyright.1995                                                  57




                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




   JBE (JNA) INSTRUCTION


Purpose: Conditional jump.

Syntax: 

JBE label


It jumps if it is down, the equal, or if it is not up.

The jump is done if CF is activated or if ZF is activated, that any of them be
equal to 1.



   JE (JZ) INSTRUCTION


Purpose: Conditional jump.

Syntax:

JE label


It jumps if it is the equal or if it is zero.

The jump is done if ZF is activated.


                                           Copyright.1995                                                   58



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




   JNE (JNZ) INSTRUCTION


Purpose: Conditional jump.

Syntax:

JNE label


It jumps if it is not equal or zero.

The jump will be done if ZF is deactivated.



   JG (JNLE) INSTRUCTION


Purpose: Conditional jump, and the sign is taken into account.

Syntax: 

JG label


It jumps if it is larger, if it is not larger or equal.

The jump occurs if ZF = 0 or if OF = SF.



                                           Copyright.1995                                                   59


 
                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




  JGE (JNL) INSTRUCTION


Purpose: Conditional jump, and the sign is taken into account.

Syntax:

JGE label


It jumps if it is larger or less than, or equal to.

The jump is done if SF = OF



   JL (JNGE) INSTRUCTION         


Purpose: Conditional jump, and the sign is taken into account.

Syntax:

JL label


It jumps if it is less than or if it is not larger than or equal to.

The jump is done if SF is different than OF.



                                           Copyright.1995                                                   60



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




   JLE (JNG) INSTRUCTION


Purpose: Conditional jump, and the sign is taken into account.

Syntax:

JLE label


It jumps if it is less than or equal to, or if it is not larger.

The jump is done if ZF = 1 or if SF is different than OF.



   JC INSTRUCTION


Purpose: Conditional jump, and the flags are taken into account.

Syntax: 

JC label


It jumps if there is cartage.

The jump is done if CF = 1



                                           Copyright.1995                                                  61


  
                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




 JNC INSTRUCTION 


Purpose: Conditional jump, and the state of the flags is taken into account.

Syntax:

JNC label


It jumps if there is no cartage.

The jump is done if CF = 0.



   JNO INSTRUCTION


Purpose: Conditional jump, and the state of the flags is taken into account.

Syntax:

JNO label


It jumps if there is no overflow.

The jump is done if OF = 0.



                                           Copyright.1995                                                   62



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




   JNP (JPO) INSTRUCTION


Purpose: Conditional jump, and the state of the flags is taken into account.

Syntax:

JNP label


It jumps if there is no parity or if the parity is uneven.

The jump is done if PF = 0.



   JNS INSTRUCTION


Purpose: Conditional jump, and the state of the flags is taken into account.

Syntax:

JNP label


It jumps if the sign is deactivated.

The jump is done if SF = 0.



                                           Copyright.1995                                                   63         



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




   JO INSTRUCTION


Purpose: Conditional jump, and the state of the flags is taken into account.

Syntax:

JO label


It jumps if there is overflow.

The jump is done if OF = 1.



   JP (JPE) INSTRUCTION


Purpose: Conditional jump, the state of the flags is taken into account.

Syntax:

JP label


It jumps if there is parity or if the parity is even.

The jump is done if PF = 1.



                                           Copyright.1995                                                  64



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




   JS INSTRUCTION


Purpose: Conditional jump, and the state of the flags is taken into account.

Syntax:

JS label


It jumps if the sign is on.

The jump is done if SF = 1.



   LOOP INSTRUCTION


Purpose: To generate a cycle in the program.

Syntax:

LOOP label


The loop instruction decreases CX on 1, and transfers the flow of the program to the label given as operator if CX is different than 1.



                                            Copyright.1995                                                 65



 
                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




  LOOPE INSTRUCTION


Purpose: To generate a cycle in the program considering the state of ZF.

Syntax:

LOOPE label


This instruction decreases CX by 1. If CX is different to zero and ZF is equal
to 1, then the flow of the program is transferred to the label indicated as
operator.



   LOOPNE INSTRUCTION


Purpose: To generate a cycle in the program, considering the state of ZF.

Syntax:

LOOPNE label


This instruction decreases one from CX and transfers the flow of the program
only if ZF is different to 0.



                                           Copyright.1995                                                  66



  
                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



 DEC INSTRUCTION 


Purpose: To decrease the operator.

Syntax: 

DEC destiny

This operation subtracts 1 from the destiny operator and stores the new value in the same operator.



   INC INSTRUCTION


Purpose: To increase the operator.

Syntax:

INC destiny The instruction adds 1 to the destiny operator and keeps the result in the same destiny operator.



   CMP INSTRUCTION


Purpose: To compare the operators.

Syntax:

                                            Copyright.1995                                                  67



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



CMP destination , source

This instruction subtracts the source operator from the destiny operator but
without this one storing the result of the operation, and it only affects the
state of the flags.


   CMPS (CMPSB) (CMPSW) INSTRUCTION


Purpose: To compare chains of a byte or a word.

Syntax: 

CMP destination , source

With this instruction the chain of source characters is subtracted from the
destiny chain.

DI is used as an index for the extra segment of the source chain, and SI as an
index of the destiny chain.

It only affects the content of the flags and DI as well as SI are incremented.


   CLC INSTRUCTION


Purpose: To clean the cartage flag.

Syntax:

CLC                                      
                                           Copyright.1995                                                   68



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



This instruction turns off the bit corresponding to the cartage flag, or in
other words it puts it on zero.


                                          
   CLD INSTRUCTION


Purpose: To clean the address flag.

Syntax:

CLD

This instruction turns off the corresponding bit to the address flag.



   CLI INSTRUCTION


Purpose: To clean the interruption flag.

Syntax:

CLI

This instruction turns off the interruptions flag, disabling this way those
maskable interruptions.

A maskable interruptions is that one whose functions are deactivated when
IF=0.
                                           Copyright.1995                                                   69



 
                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


  CMC INSTRUCTION


Purpose: To complement the cartage flag.

Syntax:

CMC

This instruction complements the state of the CF flag, if CF = 0 the
instructions equals it to 1, and if the instruction is 1 it equals it to 0.

We could say that it only "inverts" the value of the flag.


   STC INSTRUCTION


Purpose: To activate the cartage flag.

Syntax:

STC

This instruction puts the CF flag in 1.


   STD INSTRUCTION


Purpose: To activate the address flag.

Syntax:

                                           Copyright.1995                                                   70
                                           


                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



STD

The STD instruction puts the DF flag in 1. 



   STI INSTRUCTION


Purpose: To activate the interruption flag.

Syntax:

STI

The instruction activates the IF flag, and this enables the maskable external
interruptions ( the ones which only function when IF = 1).















                                           Copyright.1995                                                   71




                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


Interruptions


   Internal hardware interruptions 
   External hardware interruptions 
   Software interruptions 
   Most common interruptions: 

      INT 21H (DOS interruption) 

   Multiple calls to DOS functions.

      INT 10H (BIOS interruption) 

   Video input/output.

      INT 16H (BIOS interruption) 

   Keyboard input/output.

      INT 17H (BIOS interruption) 

   Printer input/output.



   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.
                                           Copyright.1995                                                   72



                                UNIVERSYTI OF GUADALAJARA

                        Information Systems General Coordination.


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


   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.



   SOFTWARE INTERRUPTIONS 


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



                                  UNIVERSYTI OF GUADALAJARA

                        Information Systems General Coordination.


                              
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.



   21H INTERRUPTION 


Purpose: To call on diverse DOS functions.

Syntax: 

INT 21H

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


                                           Copyright.1995                                                   74



                                  UNIVERSYTI OF GUADALAJARA

                        Information Systems General Coordination.



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 Aleatory reading 
      22H Aleatory writing 



                                           Copyright.1995                                                  75



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   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 



   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.

                                           Copyright.1995                                                  76



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   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.



                                           Copyright.1995                                                  77


 
                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



 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
                                           Copyright.1995                                                 78



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



  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. 

                                           Copyright.1995                                                   79




                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




 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.


                                           Copyright.1995                                                   80




                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


   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.          Copyright.1995                                                    81



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


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:

                                           Copyright.1995                                                  82



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


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


                                           Copyright.1995                                                    83



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   22H FUNCTION


  Use: 

To write in an Aleatory 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.



                                    Copyright.1995                                                         84



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



  Call registers: 

AH = 3CH
CH = File attribute
DS:DX = Pointer to an ASCIIZ 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 ASCIIZ 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.


                                           Copyright.1995                                                   85


 
                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




  3DH FUNCTION


  Use:

It opens a file and returns a handle.

  Call registers:

AH = 3DH
AL = manner of access
DS:DX = Pointer to an ASCIIZ 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

                                           Copyright.1995                                                   86
                                           


                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



   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.



     
                                                          Copyright.1995                                                  87  






                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


INTRODUCTION TO FILE MANAGING



   Ways of working with files 

      Work methods with files 

   FCB method 

      Introduction 
      Open files 
      Create a new file 
      Sequential writing 
      Sequential reading 
      Aleatory reading and writing 
      Close a file 

   Channels of communication method 

      Working with handles 
      Functions to use handles 



   WORK METHODS 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".


                                           Copyright.1995                                                 88




                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



 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 FCB's and to the communication channels as handles.



   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 FCB's do not provide support for the use of the organization by directories of DOS.


                                           Copyright.1995                                                   89



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



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



                                           Copyright.1995                                                   90


 
                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



  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.

                                           Copyright.1995                                                   91



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


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.


   ALEATORY READING AND WRITING 


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

The Aleatory 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. 
  
                                         Copyright.1995                                                    92



                                UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



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. 


   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 Aleatory 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 
unit 6: Interruptions, in the section dedicated to the 21H interruption. 

                                           Copyright.1995                                                  93



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


MACROS AND PROCEDURES 


   Procedures. 


      Definition of procedure 
      Syntax of a procedure 


   Macros. 


      Definition of a macro 
      Syntax of a macro 
      Macro libraries 


   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.


                                           Copyright.1995                                                   94



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.




   Syntax of a Procedure 


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

When the intra segment procedures are used, the value of IP is stored on the
stack and when the intra segments 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 up 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

                                           Copyright.1995                                                   95



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



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 intra segment. 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 a 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 MASM - 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.
                                           Copyright.1995                                                  96



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.



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.



   Syntax of a Macro 



The parts which make up 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
                                           Copyright.1995                                                  97



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


 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. Example: 

Position 8, 6 


   Macro Libraries 


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.



                                           Copyright.1995                                                   98



                               UNIVERSITY OF GUADALAJARA

                        Information Systems General Coordination.


Supposing 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

















                                           Copyright.1995                                                    99


                                 
                                UNIVERSITY  OF GUADALAJARA

                        Information Systems General Coordination.

Examples:

One of the simplest programs, but practical in a certain form, is one which
displays a chain of characters on screen. That is what the following program
does:

Program:

MODEL SMALL    ; First we define the memory model, in this case small.
.CODE          ; We declare the area which will contain the code.
Beginning:     ; Beginning of the program label.
MOV AX,@DATA   ; We are going to place the data segment address.
MOV DS,AX      ; On DS, using AX as an intermediary.
MOV DX,OFFSET Chain  ; We place the address in DX, inside the
                     ; segment of the chain to display.
MOV AH,09      ; We will use the 09 function of the interruption.
INT 21H        ; 21H To display the chain.
MOV AH,4CH     ; Through the 4CH function of the interruption.
INT 21H        ; 21H We will end our program.
.DATA          ; We declare the data segment.
Chain  DB 'Program message.$'  ; Chain to display.
.STACK         ; We declare the stack.
END Beginning  ; End of our program.


A program very similar to this one has already been explained thus it does
not require any additional commentaries.

When a program is created it is not necessary to write the commentaries
which go after the quotation marks, though it is a recommendable technique
so that in case of errors or improvements to the code it may be easier to
find the desired part.

To assemble this program it first is saved in ASCII format with a valid
name, for example: program1.asm
 
                                          Copyright. 1995                                                100


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


To assemble it the MASM is used, the assembling command is: MASM program1

To link it and make it executable we type: link program1

Once these steps are finished, it is possible to execute it by typing:
program1 [ENTER]

To directly use the program on your computer save this file as ASCII or
text, take it to your PC, with an editor get rid of all these commentaries
from the beginning and assemble it.



This program displays the 16 characters corresponding to the hexadecimal
code in a descending order.

Program:


; Beginning of the program, we define the memory model
; to use and the code segment.
MODEL SMALL           ; Memory model.
.CODE                 ; Code area.
Beginning:            ; Beginning of the program label.

MOV AX, @DATA         ; The DS register begins with the address given
MOV DS, AX            ; by @DATA (Data segment).
MOV DX, OFFSET Title  ; It obtains the address of the chain of characters.
MOV AH,09             ; We use the 09H function of the 21H interruption
INT 21H               ; to display the chain whose address we obtained.
MOV CX,16             ; Counter of the characters to be shown.
MOV BX, OFFSET Chain  ; It allows access to the chain where the values
                      ; to display are.

                                           Copyright. 1995                                                 101 


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.



Cycle:                ; Label to generate a cycle.
MOV AL,CL             ; It places the number to translate in AL and
                      ; translates it
XLAT                  ; using the XLAT instruction
MOV DLAL              ; It places the value to be displayed in DL through the
MOV AH,02             ; function 2 of the 21H interruption
INT 21H               ; It displays the character
MOV DL,10             ; It jumps a line displaying the character 10
INT 21H               ; It displays the character
MOV DL,13             ; It produces a carriage return displaying
                      ; the character 13
INT 21H               ; It displays the carriage return
LOOP Cycle            ; It decreases CX by one and it jumps the Cycle
                      ; as long as CX is not equal to zero
MOV AH,4C             ; It uses the 4C function of the 21H interruption to
INT 21H               ; end the program

; Beginning of the data segment
.DATA  ; It defines the data segment
Title DB 13,10, 'Display the hexadecimal numbers from 15 to 1'
   DB 13,10,'$'  ; Chain to display at the beginning of the program
Chain DB '0123456789ABCDEF'  ; Chain with the hexadecimal digits
; Declaration of the stack segment
.STACK
END Beginning  ; Declaration of the end of the program


The XLAT looks for the chain or table located in BX, the AL register
contains the number of bytes, from the beginning address which the pointer
will go over it to search for data, the content of AL is replaced by the
byte where the pointer is found.

The assembling process is equal to the prior example.

                                           Copyright. 1995                                                 102 


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


On the following example most of the instructions seen in the tutorial are
used, their objective is to perform adding, subtracting, multiplying
operations as well as division of two quantities.

To access each one of the available options the menu is used and where the
available operations are presented.

Program:


.MODEL SMALL    ; It defines the memory model
.DATA           ; It defines the data segment

ErrorCAP DB 0   ; Error flag in the capture of the quantities
Quantity DB 0   ; Quantity on which it is operated.  If it is 0 the quantity
                ;  signals 1, and if it is 1 it signals 2.
QuantOneR  DW 0  ; It will save the quantity of 1 converted into binary
QuantTwoR  DW 0  ; It will save the quantity of 2 converted into binary
QuantOneN  DB 6,0,6 DUR(?)  ; Variable which stores the quantity of 1
QuantTwoN  DB 6,0,6 DUR(?)  ; Variable which stores the quantity of 2
Function   DB 0  ; Variable which stores the option to perform
Results    DB 13,10,'Result: $'
ResultsR   DB 11 DUP(?)
Message    DB 13,10,'Basic operations between two numbers'
           DB 13,10,13,10,'$'
Question   DB13,10,'Press:',13,10
           DB ' 1 Multiplication ',13,10
           DB ' 2 Division ',13,10
           DB ' 3 Addition',13,10
           DB ' 4 Subtraction ',13,10
           DB ' 5 Exit ',13,10,'$'
Error      DB 7,13,10,'Invalid selection (1-5)',13,10,'$'
Error1     DB 7,13,10,'Quantity 1 invalid.      ',13,10,'$'
Error2     DB 7,13,10,'Quantity 2 invalid.      ',13,10,'$'

                                           Copyright. 1995                                                 103


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


Error3     DB 7,13,10,'Quantity out of range (65535)    ',13,10,'$'
Error4     DB 7,13,10,'Intent of division by zero. ',13,10,'$'
QuantOneM DB 13,10,'Introduce quantity 1 (Less than 65535): $'
QuantTwoM DB 13,10,'Introduce quantity 2 (Less than 65535): $'

; Potencies table for binary/ASCII conversion

Potency DW 0001h, 000Ah, 0064h, 03E8h, 2710h
PotencyF DW $

.CODE                     ; It defines the code area
Begins:                   ; Beginning of the program label

  Mov AH, 0Fh             ; It obtains current video mode
  INT 10h
  Mov AH,00               ; It changes the video mode to the same prior
  INT 10h                 ; with the finality  that the screen is cleared
  Mov AX, @Data           ; It obtains the data segment address
  Mov Ds, Ax              ; It initializes DS with this address
  Mov Dx, Offset Message  ; It displays the program title
  Call Print              ; It calls a procedure
  Mov Is, Offset ResultsR ; The ResultsR variable initializes
  Add Si,11
  Mov Al,'$'
  Mov [Is], Al

ANOTHER:
  Mov Dx,Offset Question  ; It displays options menu
  Call Print
  Call ObtainKey          ; It waits for the desired option to be pressed
  Cmp Al, 49              ; It compares the selection with the ASCII l digit
  Jae CONTINUE            ; If the option is greater than 1 it jumps CONTINUE
  Mov Dx, Offset Error    ; It displays an error message
  Call Print
 
                                           Copyright. 1995                                                 


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


 Jmp ANOTHER             ; It jumps ANOTHER to ask again
CONTINUE:
  Cmp Al,53               ; It compares the selection with the ASCII 5 digit
  Jbe ALLWELL             ; If it is less than 5 it jumps ALLWELL, if not it
                          ; continues to
  Mov Dx,Offset Error     ; If the option was greater than 5 it displays the
                        ; error
  Call Print
  Jmp ANOTHER
ALLWELL:
  Cmp Al,53                 ; It compares the selection with the ASCII digit 5
  Jnz CHECKALL              ; If it is not the same it jumps to CHECKALL
  Jmp FUNCTION5             ; If it is equal it jumps to FUNCTION5 to end
CHECKALL:
  Mov Function,Al           ; It saves the function number to perform
CAPCANT01:
  Mov Dx, Offset QuantOneM  ; Capture message of quantity 1
  Call Print
  Mov Ah,OAh                ; It captures the quantity (up to 8 digits)
  Mov Dx, Offset QuantOneN
  INT 21h
  Mov ErrorCap, O           ; It assumes there are no errors and that it
  Mov Quantity, O           ; is operating on the quantity 1
  Call Conv NUM             ; It converts the quantity 1 into binary
  Cmp ErrorCAP, 1           ; It verifies if there is no error
  Jz CAPCANTO1              ; In case it is affirmative it returns to the
                            ;  capture.
  Mov QuantOneR, Bx         ; It saves the result of the conversion
CAPCANTO2:

  Mov ErrorCAP, O           ; It assumes there is no error
  Mov Quantity, 1           ; It indicates to ConvNUM that it will work
                            ; with the quantity of 2
  Mov Dx, Offset QuantTwoM  ; Capture of quantity of 2 message
  
                                           Copyright. 1995                                                 104


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


Call Print
  Mov Ah, OAh               ; Capture of quantity 2
  Mov Dx, Offset QuantTwoM
  INT 21H
  Call ConvNum              ; It converts the quantity 2 into binary
  Cmp ErrorCAP, 1           ; It verifies if there was any errors
  Jz, CAPCANTO2             ; In case it is affirmative it returns to
                            ; the capture
  Mov QuantTwoR, Bx         ; It stores the binary value of the quantity of 2

;The following part is the process of selection of the operation to be
;performed:

  Mov Al, Function  ; It loads the function the user selected in Al
  Cmp Al, 31h       ; It checks if it is 1
  Jne FUNCTION2     ; If it is not it jumps to FUNCTION2
  Call Multiply     ; It multiplies the quantities
  Jmp ANOTHER       ; It returns to the main menu

FUNCTION2:
  Cmp Al, 32h       ; Checks if it is 2
  Jne FUNCTION3     ; If it is not it jumps to FUNCTION3
  Call Divide       ; It divides the quantities
  Jmp ANOTHER

FUNCTION3:
  Cmp Al, 33h       ; Checks if it is 3
  Jne FUNCTION4     ; If it is not it jumps to FUNCTION4
  Call Addition     ; It adds the quantities
  Jmp ANOTHER

FUNCTION4:
  Cmp Al, 34h       ; Checks if it is 4
  Jne FUNCTION5     ; If it is not it jumps to FUNCTION5
 
                                           Copyright. 1995                                                 105


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


 Call Subtraction  ; It subtracts the quantities
  Jmp ANOTHER

FUNCTION5:
  Mov Ax, 4000h     ; This function ends the execution
  INT 21h           ; of the program


;Program procedures or routines


Multiply Proc Near         ; Indicator of procedure beginning
  Xor Dx,Dx                ; Dx = 0
  Mov Ax, QuantOneR        ; First quantity
  Mov Bx, QuantTwoR        ; Second quantity
  Mul Bx                   ; It multiplies
  Call Conv ASCII          ; It converts into ASCII
  Mov Dx, Offset Results   ; It prints the result message
  Call Print
  Mov Dx, Offset ResultsR  ; It prints the result
  Call Print
  Ret                      ; It returns to the main program
Multiply Endp              ; End of procedure indicator

Divide Proc Near
  Mov Ax, QuantOneR        ; It loads the quantity 1 (dividend)
  Mov Bx, QuantTwoR        ; It loads the quantity 2 (divider)
  Cmp Bx, 0                ; It checks the divider is not zero
  Jnz DIVIDE1              ; If it is not zero it jumps to DIVIDE01
  Mov Quantity, 3          ; There was an error so it displays the message and
                           ; returns to the program
  Call ThereWasError
  Ret
 

                                          Copyright. 1995                                                 106


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


DIVIDE01:
  Div Bx                   ; It performs the division
  Xor Dx, Dx               ; Dx = 0.  The residue is not used.
  Call Conv ASCII          ; It converts into ASCII the result
    Mov Dx, Offset Results  ; It displays the message of the result
  Call Print
  Mov Dx, Offset ResultsR ; It displays the result
  Call Print
  Ret
Subtract Endp

Print Proc Near
  mov Ah,09   ; It uses the 9 function of the 21h interruption
  INT 21h     ; to display a chain
  Ret
Print Endp

ObtainKey Proc Near
  Mov ah, 0   ; It uses the 16h interruption to
  INT 16h     ; read a key
  Ret
ObtainKey Endp

ConvNum Proc Near
  Mov Dx, 0Ah     ; The multiplier is 10
  Cmp Quant, 0    ; Verifies if the quantity is 1
  Jnz CONVNUM01   ; It was not, then it is quantity 2 and jumps
  Mov Di, Offset QuantOneN + 1  ; Number  of bytes read of quantity 1
  Mov Cx, [D1]
  Mov S1 Offset QuantOneN + 2   ; Quantity 1
  Jmp CONVNUM02
CONVNUM01:
  Mov D1, OffsetQuantTwoN + 1   ; Number of bytes read of quantity 2
  Mov Cx, [D1]
 
                                           Copyright. 1995                                                 107


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


 Mov Is, Offset QuantTwoN + 2  ; Quantity 2
CONVNUM02:
  Xor Ch, Ch, CH = 0
  Mov D1, Offset Potency        ; Potency table address
  Dec Is
  Add Is, Cx
  Xor Bx, Bx
  Std
CONVNUM3:
  Lodsb           ; It loads the byte whose address is DS:SI in AL
  Cmp Al, "0"     ; It compares the byte with the digit 0
  Jb CONVNUM04    ; If it is smaller it is invalid and jumps
  Cmp Al, "9"     ; It compares the byte with the digit 9
  Ja CONVNUM04    ; If it is larger it is invalid and jumps
  Sub Al, 30h     ; It converts the digit from ASCII to binary
  Cbw             ; It converts the word
  Mov Dx, [Di]    ; It obtains the potency to be used to multiply
  Mul Dx          ; It multiplies the number
  Jc CONVNUM05    ; If there is cartage it jumps ( it was larger than 65535 )
  Add Bx, Ax      ; It adds the result to BX
  Jc CONVNUM05    ; If there is cartage it jumps
  Add Di, 2       ; It goes to the next potency of 10
  Loop CONVNUM03  ; It jumps until CX is equal to 0
  Jmp CONVNUM06
CONVNUM04:
  Call TherewasERROR  ; It means the number was larger than 65535
  Jmp CONVNUM06
CONVNUM5:
  Mov Cantidad, 2
  Call TherewasERROR
CONVNUM06:
  Cld             ; It returns the address flag to its normal
  Ret             ; state
ConvNum Endp

                                           Copyright. 1995                                                 108


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


ConvASCII Proc Near
  Push Dx
  Push Ax         ; It saves the result of the stack
  Mov Is, Offset ResultsR  ; Initializes the ResultsR variable
  Mov Cx, 10      ; surrounding it with asterisks
  Mov Al, '*'
ConvASCII01:
  Mov [Is], Al
  Inc Is
  Loop ConvASCII01
  Pop Ax
  Pop Bx
  Mov Bx, Ax      ; Low word of the quantity
  Mov Ax, Dx      ; High word of the quantity
  Mov Is, Offset ResultsR  ; Chain where the result is kept
  Add Is,  11
  Mov Cx, 10      ; Divider = 10
OBTAINDIGIT:
  Dec Is
  Xor Dx, Dx      ; Dx will contain the residue
  Div Cx          ; It divides the high word
  Mov Di, Ax      ; It saves the quotient in DI
  Mov Ax, Bx      ; It loads the high word in AX
  Div Cx          ; DX contains the register of the division
  Mov Bx, Ax      ; It saves the quotient
  Mov Ax, Di      ; It returns the high word
  Add Dl, 30h     ; It converts the residue in ASCII
  Mov [Is], Dl    ; It stores it
  Or Ax, Ax       ; If the high word is not zero
  Jnz OBTAINDIGIT ; it jumps to OBTAINDIGIT
  Or Bx, Bx       ; If the low word is not zero
  Jnz OBTAINDIGIT ; it jumps to OBTAINDIGIT
  Ret
ConvASCII Endp
    
                                       Copyright. 1995                                                 


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


TherewasERROR Proc Near
  Cmp quantity, 0         ; Is it quantity 1?
  Jnz THEREWASERROR02     ; no
  Mov Dx, Offset Error1
  Call Prit
  Mov ErrorCap, 1         ; It turns on the error flag
  Jmp THEREWASERROR05
THEREWASERROR02:
  Cmp Quantity, 1         ; Is it quantity 2?
  Jnz THEREWASERROR03     ; no
  Mov Dx, Offset Error2
  Call Print
  Mov ErrorCAP, 1
  Jmp THEREWASERROR05
THEREWASERROR03:
  Cmp Quantity, 2         ; Is it an out of range quantity?
  Jnz THEREWASERROR04     ;no
  Mov Dx, Offset Error3
  Call print
  Mov ErrorCAP, 1
  Jmp THEREWASERROR05
THEREWASERROR04:
  Mov Dx, Offset Error4   ; Division by zero error
  Call Print
  Mov ErrorCAP, 1
THEREWASERROR05:
  Ret
TherewasERROR Endp

.STACK
End Begin




                                           Copyright. 1995                                                 109


                              UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


Mov Dx, Offset Results   ; It displays the message of the result
  Call Print
  Mov Dx, Offset ResultsR  ; It displays the result
  Call Print
  Ret
Divide Endp

Addition Proc Near
  Xor Dx, Dx             ; Dx = 0 just in case there is cartage
  Mov Ax, QuantOneR      ; Quantity 1
  Mov Bx, QuantTwoR      ; Quantity 2
  Add Ax, Bx             ; It performs the addition
  Jnc ADDCONV            ; If there was no cartage it jumps to ADDCONV
  Adc Dx,0               ; There was
ADDCONV:
  Call ConvASCII        ; It converts the result into ASCII
  Mov Dx, Offset Results ; It displays the message of the result
  Call Print
  Ret
Addition Endp

Subtract Proc Near
  Xor Dx, Dx              ; Dx = 0 in case there is cartage
  Mov Ax, QuantOneR       ; Ax = quantity 1
  Mov Bx, QuantTwoR       ; Bx = quantity 2
  Sub Ax, Bx              ; It performs the subtraction
  Jnc SUBCONV             ; If there is no cartage it jumps to SUBCONV
  Sbb Dx,0                ; There is cartage
SUBCONV:
  Call ConvASCII          ; Converts the result into ASCII





                                           Copyright. 1995                                                 110


       



                               UNIVERSITY OF GUADALAJARA

                       Information Systems General Coordination.


Directory and bibliography 



Credits 

Monico Brise-o C., Engineer 
   Original Idea 
Hugo Eduardo Prez P. 
   Development and Implementation 
V'ctor Hugo Avila B. 
   English Translation 

Assembly Language For IBM Microcomputers 
   J. Terry Godfrey 
   Prentice Hall Hispanoamericana, S.A. 
   Mexico 
Basic Assembler 
   A. Rojas 
   Ed Computec Editores S.A. de C.V. 
   Mexico 
IBM Personal Computer Assembly Language Tutorial 
   Joshua Auerbach 
   Yale University 





                                           Copyright.1995                                                  111



                                UNIVERSITY OF GUADALAJARA

                        Information  Systems General Coordination.


We need your opinion! 



Our finality with this document is to take the knowledge to your
hands. To do this in a more efficient manner it is necessary to know
your opinion, suggestions and criticisms.

We would like it if you would send us your programs written in
assembler, in fountain code please, to show them in this space, as well
as to give you the recognition you deserve. Please try to include all your
info, name, address, profession, and your e-mail address if you have
one, and the version of assembler you use.

Send your programs and suggestions related with this tutorial to this
address huperez@rulfo.dca.udg.mx or if you wish send your
commentaries about this server in general to this address
monico@redudg.udg.mx 

Just so you know it I am a student of the University of Guadalajara,
from the Computer Science Department. I study the career of
Computer Engineering and I would like to learn from you. The first
address is my personal address, so feel free to send your commentaries.










                                           Copyright.1995                                                 112




































































   




































     














 




       
