Professional Documents
Culture Documents
The Programming in Assembly language or for that matter any language follows the specific
It is also called as Program Development Life Cycle (PDLC) or Software Development Life
Cycle (SDLC).
Algorithm-Textual representation
Flowchart Graphical representation
3. Initialization Checklist
The further discussion gives the detailed account of above mentioned steps.
The first step in writing a program is to think very carefully about the problem that
you want the program.
It is a good idea to write down exactly what you want the program to do and the
order in which you want the program to do it. At this point you do not write down
program statements, you just write the operations you want in general terms.
It involves finalizing the functionality (What the program is supposed to do) as well
as the Input and Output Specifications (What the program receives from the user
and what the program delivers as result, to the user) Specifying functional and I/O
specs is very crucial part of this Problem definition stage.
1 |2 1
Unit-2 : The Art of Assembly Language Programming
Algorithms
Once the problem is defined, it is analyzed for the solution. The solution is designed
for the given problem.
The solution is crystallized and it is required to be represented systematically, by
some specific method or in the standard form.
Algorithm is defined as the textual mode representation of the specific program logic
designed; in the form of sequence of steps.
The language used is English like but important control structures are expressed by
specific words such as SET (To initialize the entity or variable) IF or IF.. THEN or IF..
THEN.. ELSE (For specifying decision points and decision paths) etc.
All operational details are in the form of step by step sequence and it makes easy for
the programmer to understand the flow of the program. The algorithm is
programming language independent.
In many cases, the program logic and flow is written in the Technical language having
appearance similar to generic programming languages such as C, C++ and called as
Pseudo-code which is another form of Algorithmic expression.
Flowcharts
Another method of standard representation of program logic and flow is a graphical method
called as Flowchart.
2 |2 1
Unit-2 : The Art of Assembly Language Programming
Initialization Checklist
Besides the instructions constituting main program, there are few additional
instructions that you need to determine before you actually write your program.
The purpose of these additional instructions is to initialize various parts of the
system, such as segment registers, Flags and Programmable ports.
The various segment registers are initialized by loading immediate value in it. But
this cannot be done directly because immediate data cannot be loaded directly in
segment registers, e.g. to load Ø500 in DS, the instruction sequence will be –
MOV AX, Ø500 followed by MOV DS, AX.
Thus before writing a program, the initialization check list should be prepared as
follows.
Choosing Instructions
3 |2 1
Unit-2 : The Art of Assembly Language Programming
8086 must be fully known, in order to choose correct instructions that go into the
making of the program.
We should choose instructions or group of instructions to implement the given step
correctly. If many alternatives are available, we should choose those instructions
that make program smaller in size and more importantly efficient in execution. Also,
consideration must be given to the faster executing Instructions as higher execution
speed improves the efficiency and performance of the program.
Choice of correct instructions makes the program function correctly and optimally.
Every step in the algorithm is converted into program statement using correct and
efficient instructions or group of instructions.
When all steps are converted in this way, it forms the assembly language program
for given problem. The program is now ready. At the start of program, required
initialization instructions are written.
The program should have following sequence of Operations -
Work with the Data Segment
Initialize Variables and Data structures needed for the program
Initialize Constants needed
Declare un-initialized variables, data structures using assembler operators
Done with Data Segment
Work with Code Segment
Initialize required Segment registers
Initialize Stack by loading the Stack pointer
Initialize Ports, flags, Chips as per the initialization checklist
Write Instructions/Set of Instructions for Algorithm/Flowchart steps
Write Subroutines and Macro, if needed any
Done with the Code segment
End program with specified entry point.
4 |2 1
Unit-2 : The Art of Assembly Language Programming
These tools are programs which help you run the program, to perform some function
on the program you are writing.
These tools are pre-developed program software that comes to the aid of
developer/programmer for writing, assembling/linking, running, testing and
debugging the program at the various stages in the Program Development Life Cycle.
Editor
An editor is a program which allows you to create a file containing the Assembly
language statements for your program.
Program is typed using editor. If there is any typing error, it can corrected by moving
the cursor at that position and correcting/re-typing it. It is needed to store the
Assembly language program source code file with .ASM extension. E.g. MyProg.ASM
With the Editor tool, we are able to create, edit, modify, list, print, save or update
and delete the Assembly language source code files of the program.
Examples of the Editor are - EDIT from the DOS, NOTEPAD and WORDPAD of the
Microsoft Windows, are the well known editors.
Assembler
5 |2 1
Unit-2 : The Art of Assembly Language Programming
The output file is called object file and has extension .OBJ. The object file contains
the binary codes for the instructions and information about the addresses of the
instructions. After further processing, the contents of this file would be loaded into
memory and executed.
The other file generated by the assemblers is list file having extension .LST. This file
contains assembly instructions and binary codes side by side. This can be used for
testing and debugging.
Assembler finds the syntax errors. These errors can be corrected by re-editing the
program using editor and then assembled again. This process is repeated to get
syntax error-free source code of the program.
Examples of Assembler are - MASM, TASM etc.
Linker
Linker is a program used to join several object files into one large object file and
converts the machine language object code of the program into Executable code of
the program.
When writing large programs, it is usually much more efficient to divide the large
program into smaller modules. Each module can be individually written, tested, and
debugged, Then, when all the modules work, their object modules can be linked
together to form a large, functioning program.
6 |2 1
Unit-2 : The Art of Assembly Language Programming
Also the object modules for useful programs like square root program can be kept in
library file and linked into other program as needed.
The linker produces a link file which contains the binary codes for all the combined
modules.
The linker also produces a link map file which contains the address information
about the linked files. The linker assigns only relative address to the program starting
from zero This form of program is said to be re-locatable because it can be put
anywhere in memory to be run.
TASM or MASM assemblers use Tlink.exe and Link.exe as the linker tools,
respectively. The output of the linker is executable code of the developed program,
which is stored as .EXE file.
Debugger
If the program does not require any external hardware or Console input or output,
then debugger is used to load the .EXE file in main memory and run it.
It automatically assigns physical starting address to the segments.
Debugger is the tool that helps us test and examine the program for its desired
specified functionality; as it is executing.
By executing instructions stepwise, you can check the contents of registers and
memory locations and check the problems or software bugs at the various steps or
stages in the program.
Debugging is essentially a Testing tool. It employees testing and debugging methods
such as Single stepping, Trace, Watches and Breakpoints for this purpose.
Debugger optionally generates .DBG files, which can be of help to the
developer/programmer in checking/testing program variables and logic.
Logical errors in the program are detected in the process of debugging with the help
of the debugger. Logical errors are errors in the logic of the program. i.e. Program
does not function the way it is supposed to.
Logical errors are removed by doing appropriate corrections in the operating
program logic as suitably modifying the source code of the Program, accordingly.
7 |2 1
Unit-2 : The Art of Assembly Language Programming
ASSEMBLER DIRECTIVES
These are not Assembly Language Instructions in the Instruction set of 8086/8088.
They are commands or directions provided to the Assembler tool to perform certain
function or operation, while assembling the Source code of the Assembly language
program.
These directives do not get assembled into equivalent object code or executable
code of the program as the program gets assembled and linked by the Assembler
and the Linker.
As they provide directions to the Assembler tool, they are called as Assembler
Directives.
A Large variety of Assembler directives are available for use with the 8086 Assembler
and most of them are supported by the two widely used Assemblers; namely TASM
(Turbo Assembler) and MASM (Microsoft Assembler).
1. SEGMENT Directive Description:
The SEGMENT directive is used to indicate the start of a logical segment. Preceding
the SEGMENT directive is the name you want to give the segment. For example, the
statement- CODE SEGMENT
Declares to the assembler the start of a logical segment called CODE.
The SEGMENT and ENDS directive are used to start and end a logical segment
containing code of data.
Additional terms are often added to a SEGMENT directive statement to indicate
some special way in which we want the assembler to treat the segment. The
statement CODE SEGMENT WORD tells the assembler that we want the content of
this segment located on the next available word (even address) when segments are
combined and given absolute addresses.
The statement CODE SEGMENT PUBLIC tells the assembler that the segment may be
put together with other segments named CODE from other assembly modules when
the modules are linked together.
8 |2 1
Unit-2 : The Art of Assembly Language Programming
This directive is used with the name of a segment to indicate the end of that logical
segment.
Example:
CODE SEGMENT
3. END Directive
Description:
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 will ignore any statements
after an END directive, so one must make sure to use only one END directive at the
very end of your program module.
A carriage return is required after the END directive.
Example:
END Start
It declares that the Assembler program has come to an end. Further it also specifies
execution entry point from label Start
No line after the END directive is assembled. However, comments can be added after
the end. directive.
4. ASSUME Directive
The ASSUME directive is used tell the assembler the name of the logical segment it
should use for a specified segment.
9 |2 1
Unit-2 : The Art of Assembly Language Programming
The statement ASSUME CS: CODE, for example, tells the assembler that the
instructions for a program are in a logical segment named CODE.
The statement ASSUME DS: DATA tells the assembler that for any program instruction,
which refers to the data segment, it should use the logical segment called DATA.
Example :
ASSUME CS: CODE, DS: DATA
It declares that the Code segment register CS to be associated with logical segment
called CODE and Data Segment register DS to be associated with logical segment called
DATA.
The statement declares an array of 3 bytes named ITEMS and initializes each byte with the
respective specified value.
FIRST_NAME DB "THOMAS"
It sets aside 100 bytes of storage in memory and give it the name TEMPS. But leave the 100
bytes un-initialized.
It sets aside 20H bytes of storage in memory, gives it the name FORCE and puts 0 in all 20H
locations.
10 |2 1
Unit-2 : The Art of Assembly Language Programming
Operator DUP
As seen in the example above, PRESSURE is a variable replicated 20H times and all
locations kept un-initialized due to DUP and? Operators, respectively.
The DW directive is used to tell the assembler to define a variable of type word (16
Bits) or to reserve storage locations of type word in memory.
The statement MULTIPLIER DW 437AH, for example, declares a variable of type word
named MULTIPLIER, and initialized with the value 437AH when the program is
loaded into memory to be run.
Example :
Declare an array of 2 words named variable TWOWORD and initializes with the specified
values.
Reserve an array of 100 words of memory and initialize all 100 words with 0000. Array is
named as STORE.
11 |2 1
Unit-2 : The Art of Assembly Language Programming
Reserve 100 word of storage in memory and give it the name STORE2, but leave the words
un- initialized.
The DD directive is used to declare a variable of type double word orto reserve
memory locations, which can be accessed as type double word.
The statement ARRX DD 25629851H, for example, will define a double word named
ARRX and initialize the double word with the specified value when the program is
loaded into memory to be run. The low word, 9851H, will be put in memory at a
lower address than the high word.
The DQ directive is used to tell the assembler to declare a variable 4 words in length
or to reserve 4 words of storage in memory.
The statement BIG_NUM DQ 243598740192A92BH, for example, will declare a
variable named BIG_NUM and initialize the 4 words set aside with the specified
number when the program is loaded into memory to be run.
The DT directive is used to tell the assembler to declare a variable, which is 10 bytes
in length or to reserve 10 bytes of storage in memory.
The statement PKD_BCD DT 11223344556677889900 will declare an array named
PKD_BCD, which is 10 bytes in length. It will initialize the 10 bytes with the values 11,
22, 33, 44, 55, 66, 77, 88, 99, and 00 when the program is loaded into memory to be
run.
The statement RESULT DT 20H DUP (0) will declare an array of 20H blocks of 10 bytes
each and initialize all 320 bytes to 00 when the program is loaded into memory to be
run.
12 |2 1
Unit-2 : The Art of Assembly Language Programming
EQU is used to give a name to some value or symbol. Each time the assembler finds
the given name in the program, it replaces the name with the value or symbol you
equated with that name.
Therefore, EQU directive is essentially used to define a Constant. Within the scope of
the program, its assigned value remains constant or fixed. It cannot be changed.
Suppose, for example, you write the statement FACTOR EQU 03H at the start of your
program, and later in the program you write the instruction statement ADD AL,
FACTOR. When the assembler codes this instruction statement, it loads 03H,
constant value in AL register.
Example :
ADD AL, BL
DEC_ADJ
13 |2 1
Unit-2 : The Art of Assembly Language Programming
If the string was declared as a word string, LENGTH will produce the number
of words in the string.
Example:
NAME_STR DB "Shrikant"
Loads CX register with 08, which is the length of above specified string.
NAME_STR DB "Shrikant"
Loads BX register with address or location displacement of the above specified string, in the
declared Data Segment.
14 |2 1
Unit-2 : The Art of Assembly Language Programming
The statement INC BYTE PTR [BX] tells the assembler that we want to increment the
byte pointed to by BX. The contents of BX are incremented by 1.
The statement INC WORD PTR [BX] tells the assembler that we want to increment
the word pointed to by BX. The contents of BX are incremented by 2.
The PTR operator assigns the type specified before PTR to the variable specified after
PTR.
The PTR operator is applicable to all unary functions having memory references, such
as INC (Increment), DEC (Decrement), NEG (Negate i.e. 2's Complement), NOT (Invert
i.e. 1's Complement) etc.
DATA SEGMENT
EVEN
Skips a location, starting next memory allocation at EVEN boundary, i.e. 000A INVENTORY
DW 100 DUP (0)
Array of 100 words, starting on even address reads data faster due to aligned cycles. DATA
ENDS
Note: Word Data transfers starting on Even address boundary are called Aligned Cycles and
are completed in single Machine cycle; where as Word Data transfers starting on Even
15 |2 1
Unit-2 : The Art of Assembly Language Programming
address boundary are called Misaligned Cycles and are completed in 2 back-to-back Machine
cycles, making it much slower to operate.
The PROC directive is used to identify the start of a procedure. The PROC directive
follows a name you give the procedure. After the PROC directive, the term near or
the term faris used to specify the type of the procedure.
The statement MULXPROC FAR, for example, identifies the start of a procedure
named MULX and tells the assembler that the procedure is far (in a segment with
different name from the one that contains the instructions which calls the
procedure). The PROC directive is used with the ENDP directive to indicate logical
start and end of a procedure.
The directive is used along with the name of the procedure to indicate the end of a
procedure to the assembler. The directive, together with the procedure directive,
PROC, to declare logical start and end of a procedure.
Example :
SQR_ROOT ENDP
16 |2 1
Unit-2 : The Art of Assembly Language Programming
The ORG directive allows you to set the location counter to a desired value at any
point in the program. The statement ORG 3000H tells the assembler to set the
location counter to 3000H, for example.
A "$" it often used to symbolically represent the current value of the location
counter, the $ actually represents the next available byte location where the
assembler can put a data or code byte. The $ is often used in ORG statements to tell
the assembler to make some change in the location counter relative to its current
value.
The statement ORG $+100 tells the assembler increment the value of the location
counter by 100 from its current value.
The NAME directive is used to give a specific name to each assembly module when
programs consisting of several modules are written.
Example:
NAME CALC_MODULE
17 |2 1
Unit-2 : The Art of Assembly Language Programming
The EXTRN directive is used to tell the assembler that the name or labels following
the directive are in some other assembly module.
For example, if you want to call a procedure, which in a program module assembled
at a different time from that which contains the CALL instruction, you must tell the
assembler that the procedure is external.
The assembler will then put this information in the object code file so that the linker
can link the two modules together.
For a reference to externally named variable, you must specify the type of the
variable, as in the statement EXTRN DIVS: WORD. The statement EXTRN DIVS: FAR
tells the assembler that DIVIDE is a label of type FAR in another assembler module.
Name or labels referred to as EXTRN (external) in one module must be declared
public with the PUBLIC directive in the module in which they are actually defined or
declared.
Large program 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 complete program.
In order for the module s to link together correctly, any variable name or label
referred to in other modules must be declared PUBLIC in the module in which it is
defined.
The PUBLIC directive is used to tell the assembler that a specified name or label will
be accessed from other modules. An example is the statement PUBLIC ABC, XYZ
which makes the two variables ABC and XYZ available to other assembly modules.
The SHORT operator is used to tell the assembler that only a 1 byte displacement is
needed to code a jump instruction in the program.
18 |2 1
Unit-2 : The Art of Assembly Language Programming
The destination must in the range of - 128 bytes to +127 bytes from the current IP
register i.e. address of the instruction.
The statement JMP SHORT NEXT is an example of the use of SHORT, which tells
assembler to take a short jump to the label NEXT, which should be in the 256 Bytes
vicinity (-128 bytes to +127 bytes) of the Current execution point of the program.
The TYPE operator tells the assembler to determine the type of a specified variable.
The assembler actually determines the number of bytes in the type of the variable.
For a byte type variable, the assembler will give a value of 1,
For a word type variable, the assembler will give a value of 2,
And for a Double word type variable, it will give a value of 4.
It can be used in instruction such as ADD BX, TYPE WORD ARRX, where we want to
increment BX to point to the next word in an array ARRX of words.
This directive is used to tell the assembler to insert a block of source code from the
named file into the current source module.
These are the directions to the assembler to insert another module in the current
place in the module.
19 |2 1
Unit-2 : The Art of Assembly Language Programming
Example:
INCLUDE Program2.ASM
The contents of another program module stored in file Program2.ASM are included in the
current program module, by this statement.
Dot. Directives are the set of directives used by memory models such as Tiny and
Small models making assembly language structures simpler. They also define the
current memory model in use, type of CPU in use etc.
The different. Directives are -
Tiny - Flat model, Single segment, creates .COM executable code. ❖ Small - Single
Code Segment and Single Data Segment.
Medium - Multiple Code Segment and Single Data Segments.
Compact - Single Code Segment and Multiple Data Segments.
Large - Multiple Code as well as Multiple Data segments, segments within 64KB size
limit.
Huge Multiple Code as well as Multiple Data segments, segments having NO 64KB
size limit.
The Tiny model creates .COM executable code; it is pure machine code and usually platform
independent.
20 |2 1
Unit-2 : The Art of Assembly Language Programming
Syntax:.<CPU_type>
Example: .386 (Assembles the code compatible to 80386 instruction set)
Valid CPU Types are .286 (for 80286 Processors); .386 (for 80386 Processors); .486
(for - 80486 Processors); .586 (for Pentium class Processors).
Syntax:.CODE
This directive declares the start or beginning of the Code segment, it also
automatically associates CS register with it and initializes it.
The subsequent statements should be code instructions.
Syntax: .DATA
This directive declares the start or beginning of the Data segment, it also
automatically associates DS register with it and initializes it.
The Subsequent statements should be Data declarations.
Syntax:.STARTUP
It indicates or declares the initial execution point; also called as entry point of the
Program as the executable is run on the system.
The program execution begins from the statement subsequent to this directive.
21 |2 1