Assembly Language Programming
Assembly Language Programming
Each family of processors has its own set of instructions for handling various operations like getting input
from keyboard, displaying information on screen and performing various other jobs.
These set of instructions are called 'machine language instruction'.
Processor understands only machine language instructions which are strings of 1s and 0s. However
machine language is too obscure and complex for using in software development. So the low level
assembly language is designed for a specific family of processors that represents various instructions in
symbolic code and a more understandable form
Assembly language is a low level programming language.
Assembly language is a low level programming language for a computer, or other programmable device
specific to a particular computer architecture.
This unlike most high-level programming languages, which are generally portable across multiple
systems.
Advantages of Assembly Language
An understanding of assembly language provides knowledge of:
Interface of programs with OS, processor and BIOS;
Representation of data in memory and other external devices;
How processor accesses and executes instruction;
How instructions accesses and process data;
How a program access external devices.
Other advantages of using assembly
It requires less memory and execution time;
It allows hardware-specific complex jobs in an easier way;
It is suitable for time-critical jobs;
O. Juma 1|Page
It is most suitable for writing interrupt service routines and other memory resident programs.
Assembly Language Program Development Tools
There are several tools available to support the programmer of assembly language for better experience of
programming. They are discussed in brief here:
• Editor
• Assembler
• Linker
• Locators
• Debuggers
• Emulators
Editor
It is the program which allows you to create a file containing the assembly language statements for your
program.
Examples are PC Write, Word stars and the editors that come with assemblers.
Creates Source file to be processed by the assembler
Assembler
It is the program which is used to transfer the assembly language mnemonics to corresponding binary
codes. It works in passes.
In the first pass, it determines the displacement of the named data items, the offset of labels etc. and puts
this information to in a symbol table.
In the second pass, it produces the binary codes for each instruction and inserts the offsets etc. that it
calculated in the first pass.
It generates two files namely object file (.OBJ) and assembler list file (.LST).
Linker
It is a program used to join several object files into large object files.
While writing large programs it is good to divide them into modules so that each modules can be written,
tested, debugged independently and then use linker to combine the modules to form the actual program.
It produces two files link file which contains the binary codes of all the combined modules and a link map
file which contains the address information about the linked files.
Locators
A locator is the program used to assign the specific addresses of where the segments of object code are to
be loaded in to main memory.
Examples include EXE2BIN which comes with the IBM PC DOS.
O. Juma 2|Page
Converts .exe to .bin files which has physical addresses
Debuggers
A debugger is the program which allows you to load your object code program in to system memory.
It allows you to look at the contents of the registers and memory locations after your program runs.
It also allows you to set breakpoints at any points in the program.
It simply allows you to find the source of the problem into the program.
There are lots of debuggers available like Borland Turbo Debugger, Microsoft’s Code view debugger etc.
Emulators
One way to run your program
It is mixture of hardware and software.
Generally used to test the hardware and software of an external system such as microprocessor based
instruments
O. Juma 3|Page
Structured programming and pseudo code
Programs should be understandable to other programmers
Two approaches top-down and bottom up approach
Top-down: divide the program in to major modules and divide theses major
modules in to sub modules and so on.
Bottom-up: each programmer writes the low level module code and hopes
that all pieces will eventually fit together.
3. Writing a program
You need to do the following to write the program effectively:
a. INITIALIZATION INSTRUCTIONS: used to initialize various parts of the program like
segment registers, flags and programmable port devices.
b. STANDARD PROGRAM FORMAT: it’s a tabular format containing ADDRESS, DATA
OR CODE, LABELS, MNEM, OPERAND(S) and COMMENTS as the columns.
c. DOCUMENTATION: you should document the program. E.g. each page of the document
contains page number and name of the program, heading block containing the abstract
about the program, comments should be added wherever necessary.
O. Juma 4|Page
Writing a program
To begin with, writing a program involves several steps (here are a few):
1. Define the external specification including the user interface and event handlers
2. Build the user interface
3. Code event handlers and write common code
4. Debug the program
5. Document the program
O. Juma 5|Page
5. The job is not finished when the program is working correctly. The programmer must prepare
documents describing both the external specification and the internal design of the program. This
documentation will be of value to users and programmers who must maintain and modify your program.
Many people may work as a team if a project is large. There might be architects, programmers, testers and
documentation writers.
It may sound like you just work your way through these steps in order, but, in practice, you will find
yourself going back at times. For example, while writing event handlers, you might decide you need to
change the user interface, so you need to back up and change the external specification.
The best way to save time on a programming project is to spend a lot of time on the external design. A
well-designed program will be easy to code, debug and document. As they say "the best way to go fast is
to go slow."
Assembler Directives
There are some instructions in the assembly language program which are not a part of processor
instruction set. These instructions are instructions to the assembler, linker, and loader.
These are referred to as pseudo-operations or as assembler directives. The assembler directives enable we
to control the way in which a program assembles and lists.
They act during the assembly of a program and d not generate any executable machine code.
There are many specialized assembler directives. Let us see the commonly used assembler directive in
8086 assembly language programming.
ALIGN: The align directive forces the assembler to align the next segment at an address divisible by
specified divisor. The general format for these directives is as shown below.
ALIGN number
Where number can be 2, 4, 8 or 16
Example: ALIGN 8 this forces the assembler to align the next segment at Address that is divisible by 8.
The assembler fills the unused bytes with 0 for data and NOP instruction for code.
Usually ALIGN 2 directive is used to start the data segment on a word boundary and ALIGN 4 directive
is used to start the data segment on a double word boundary.
ASSUME : The 8086, at any time, can directly address four physical segments which include a code
segment, a data segment, a stack segment and an extra segment.
O. Juma 6|Page
The 8086 may contain a number of logical segments. The ASSUME directive assigns a logical segment to
a physical segment at any given time.
That is, the ASSUME directive tells the assembler what address will be in the segment registers at
execution time.
Example: ASSUME CS: code, DS: Data, SS: stack
CODE: This directive provides shortcut in definition of the code segment. General format for this
directive is as shown below.
The name is optional. It is basically specified to distinguish different code signets.
Code [name]
DUP: The DUP directive can be used to initialize several locations and to assign values to this location
END: The END directive is put after the last statement of a program to tell the assembler that this is the
end of the program module. The assembler ignores any statement after an END directive.
EQU: The EQU directive is used to redefine a data name or variable with another data name, variable, or
immediate value. The directive should be defined in a program before it is referenced.
Formats:
Numeric Equate Name EQU expression
String Equate Name EQU <string>
O. Juma 7|Page
Example: Two EQU Numeric value
NUM EQU <Enter the first number:’>
EXTRN: The EXTRFN directive is used to inform assembler that the names or labels following the
directive are in some other assembly module.
For example, if you want to call a procedure which is in a program module assembled at a different time,
you must tell the assembler that the procedure is EXTRN. The assembler will then put information in the
object code file so that the linker can connect the two modules together.
PUBLIC: Large programs are usually written as several separate modules. Each module is individually
assembled, tested and debugged.
When all the modules are working correctly, their object code files are linked together to form the
completer program. In order for the modules to link together correctly, and variable name or label referred
to in other modules must be declared public in the module where it is defined The PUBLIC directive is
used to tell the assembler that a specified name or label will be accessed from other modules.
O. Juma 8|Page