You are on page 1of 11

Microprocessor and Programming The Art of Assembly Language Programming

4. The art of Assembly Language Programming


08 Marks
Syllabus:
4.1 Program development steps defining problem, writing algorithms, flowchart, initialization
checklist, choosing instructions, converting algorithms to assembly language programs.
4.2 Assembly Language Programming Tools Editors, Assembler, Linker, Debugger.
4.3 Assembler directives and operators

Program development steps:


In order to develop programs in any language, certain steps are followed. Following these steps
is helpful to efficiently develop the programs.
1. Defining the problem:
The first step in writing a program is to think carefully the problem that is to be solved with the help of
program. If a program works fine, but it does not solve the problem according to the requirement, it is
of no use. In this step program is not written but it is known what the program must do.
2. Representing program operations:
Algorithm:
The formula or sequence of operations used to solve a programming problem is called the algorithm.
Flowchart:
The flowchart is graphical representation of sequence of operations to solve a problem.
In flow chart different operations are represented by certain symbols.
Operation Symbol
Start and stop

Processing

Input/Output/
Read/Write/
Load/Store
Subroutine/
Procedure

Decision making

Computer Department, Jamia Polytechnic, Akkalkuwa 1


Microprocessor and Programming The Art of Assembly Language Programming

Connector

Direction of control flow

3. Initialization checklist:
The program may be using variables and constants. It may be using segments, flags, I/O ports. Before
using these entities it may be required to initialize them. It is better to prepare a list of items to be
initialized. Once the list is available, all parameters in the list can be checked whether these are
initialized properly or not.
4. Choosing instructions:
Next step is to determine the instructions required for different parts of program. For this, you must
know all the instructions available in the instruction set of microprocessor and their working. For each
instruction its general form, number and type of operands which will be used with instruction should
be known. It must be cleared that which flags are affected by different instructions.
5. Converting algorithms to assembly language programs:
Once instructions have been selected for different operations, these are arranged in sequence as per
algorithm.
If different segment registers are used, such as data segment, stack segment, extra segment, they must
be initialized properly. If variables are used, they must be declared first and initialized. Jump and loop
labels must be used carefully at proper locations. Procedures/subroutines, if used, must be defined
carefully.
In the last, program must be terminated properly.

Assembly language programming tools:


Editor:
An editor is a program which allows you to create a file containing the assembly language statements
or instructions of your program. The main function of an editor is to help you to construct assembly
language program in right format. The file created by the editor is in ASCII codes and called as source
file. The source file is saved with .ASM extension. This source file is used by assembler to create an
object file which has machine language instructions of your program. Any editor can be used in order
to create source file e.g. DOS based text editor EDIT or Windows based text editor Note Pad etc.

Computer Department, Jamia Polytechnic, Akkalkuwa 2


Microprocessor and Programming The Art of Assembly Language Programming

Assembler:
An assembler is a program which is used to translate the assembly language program to machine
language program. The assembler reads the source file from disk where it is stored. The translation
may be done in one pass or two passes. In first pass, the assembler creates a symbol table in which
displacement of named data items and offset of the labels used in the programs is stored. After
successful assembly, it generates two file. The first file is called object file and has an extension
.OBJ. This file contains the machine code for the assembly language instructions. The second file
generated by assembler is a list file and has an extension .LST. This file has some extra information
as compared to object file such as assembly language statements, binary or machine code for each
instruction and offset of each instruction. TASM (Turbo Assembler) is assembler from Borland
Software Corporation and MASM is Microsoft Macro Assembler from Microsoft Corporation.

Source file Assembler Object file

fileName.ASM TASM / MASM FileName.OBJ

Linker:
A linker is a program used to link (or combine/join) several object files into one executable program.
When the programs are large, they are usually divided into smaller modules. Each module is
individually written, tested and debugged. After this, all modules can be linked together to form a
large program.

Object file Linker Executable


file

FileName.OBJ FileName.EXE

The linker produces relocatable executable files. It also produces a link map file which contains the
address information about the linked files. TLINK is a linker from Borland Software Corporation and
LINK is a linker from Microsoft Corporation.

Debugger:
Debugger is a program which allows you to load machine language program in memory and execute
it. Using debugger, contents of registers and memory locations can be examined. These contents can

Computer Department, Jamia Polytechnic, Akkalkuwa 3


Microprocessor and Programming The Art of Assembly Language Programming

be changed and you can re-execute your program. Using debugger programs can be executed in single
step mode. A debugger allows setting break points in program. The program will be executed up to the
break point and stop. Registers and memory locations can be examined to see if the results are correct
at that point. If the results are correct, the break point can be removed and can be set to a new location.
If the results are not correct, the program can be checked to fix the problem. TD is a debugger from
Borland Software Corporation and Code View CV is a debugger from Microsoft Corporation.

Assembler Directives:
Assembler provides a number of reserved words. These reserved words are known as assembler
directives. Assembler directives are helpful in writing programs. These direct or tell the assembler to
perform a particular task. Since these are not the part of 8086 instruction set, machine code is not
generated for assembler directives.
ASSUME:
This directive is used to inform the assembler the name of logical segment. This name is used to refer
to a particular segment. In assembly language programming each segment is given a name e.g. code
segment may be given a name as CODE, data segment as DATA and so on.
General form:
ASSUME segment_register: segment_name, segment_register: segment_name
where segment_register may be CS, DS, ES, SS and segment_name is the name given
to the segment.
For example,
ASSUME CS: CODE, DS: DATA

DB: Define Byte


This directive is used to define a byte type variable. It can be used to define a single-byte variable or a
multi-byte variable.
General form:
varable_name DB value or values separated by comma
For example,
NUM1 DB 10H ; Hexadecimal value
NUM2 DB 15 ; Decimal value
BLOCK1 DB 10H, 20H, 30H, 40H, 50H ; Array of hexadecimal values
RESULT DB ? ; value undefined
SNAME DB BATLIWALA MALIK ; String of bytes

Computer Department, Jamia Polytechnic, Akkalkuwa 4


Microprocessor and Programming The Art of Assembly Language Programming

DW: Define Word


This directive is used to define a word type (i.e. 2 byte) variable. It can be used to define a single-word
variable or a multiple-word variable.
General form:
varable_name DW value or values separated by comma
For example,
NUM1 DW 1230H
NUM2 DW 15H
BLOCK1 DW 100H, 2000H, 3045H, 7409H, 5100H ; Array of hexadecimal values
RESULT DW ? ; value undefined

DD: Define Double Word


This directive is used to define a double word type (i.e. 4 byte) variable. It can be used to define a
single or a multiple double word variable.
General form:
varable_name DD value or values separated by comma
For example,
NUM1 DD 12345678H

DQ: Define Quad Word


This directive is used to define a quad word type (i.e. 8 byte) variable. It can be used to define a single
or a multiple quad word variable.
General form:
varable_name DQ value or values separated by comma

DT: Define Ten Bytes


This directive is used to define a ten byte type variable. It can be used to define a single or a multiple
ten-byte variable.
General form:
variable_name DT value or values separated by comma

EQU: Equate
This directive is used to give a name to some value or symbol. Each time, while assembling, the
assembler finds the given name in the program, it will replace the name with the value or symbol.
General form:

Computer Department, Jamia Polytechnic, Akkalkuwa 5


Microprocessor and Programming The Art of Assembly Language Programming

symbol_name EQU value or symbol


For example,
COUNTER EQU 10
ADDITION EQU ADD
ASCII_ADJUST EQU AAA
Once a name is given to a value or symbol, this name can be used in place of that value or symbol.
For example,
MOV CX, COUNTER MOV CX, 10
ADDITION AL, BL ADD AL, BL

END: End program


This directive is used after the last statement of a program to tell the assembler that this is the end of
the program. The assembler will ignore any statements after and END directive. Some assemblers
require a label after END directive. This label is same as the label used for first instruction in code
segment.
General form:
END Label
For example,
END START
END MAIN

ENDP: End procedure


This directive along with procedure name is used to indicate the end of a procedure.
General form:
procedure_name ENDP
For example,
FACTORIAL ENDP

ENDS: End Segment


This directive is used with the name of a segment to indicate the end of logical segment.
General form:
segment_name ENDS
For example,
CODE ENDS
DATA ENDS

Computer Department, Jamia Polytechnic, Akkalkuwa 6


Microprocessor and Programming The Art of Assembly Language Programming

In the above statements, CODE and DATA are the segment names.

SEGMENT: Start segment


This directive is used to indicate the start of a logical segment. Preceding the SEGMENT directive is
the name of segment.
General form:
segment_name SEGMENT
For example,
CODE SEGMENT
DATA SEGMENT

PROC: Procedure
This directive is used to identify the start of a procedure. This directive is used after the name of
procedure. After PROC directive, the terms NEAR or FAR is used to give the type of procedure.
General form:
procedure_name PROC NEAR/FAR
For example,
FACTORIAL PROC FAR
At the end of procedure ENDP directive is used to indicate the end of procedure.

MACRO:
This directive informs the assembler the beginning of a macro. Before this directive, name of macro is
used. After this directive, macro arguments can be given. At the end of macro ENDM directive is used
to indicate end of the macro.
General form:
macro_name MACRO argument1, argument2
For example,
DISPLAY MACRO message
In the above statement, DISPLAY is macro name and message is an argument.

ENDM: End Macro


This directive informs the assembler the end of the macro. This directive is used as last statement of a
macro.
General form:
ENDM

Computer Department, Jamia Polytechnic, Akkalkuwa 7


Microprocessor and Programming The Art of Assembly Language Programming

EVEN: Align on even memory address


When assembler starts assembly process, it initializes a location counter and goes on incrementing it
as assembly proceeds. It uses location counter value to assign addresses to variables, constants,
instructions etc. the EVEN directive tells the assembler to increment the location counter to next even
number if it is not already at even address.
General form:
EVEN
For example,
DATA SEGMENT
AVGS DB 9 DUP(?) ; Location counter has a value 0009H after this declaration
EVEN ; Increment the location counter to next even number i.e. 000AH
SALES DW 100 DUP(0)
; .
DATA ENDS

ALIGN: Align according to the number


This directive is used to force the assembler to align the next data item or instruction according to
given value.
General form:
ALIGN NUMBER
The NUMBER must be a power of 2 such as 2, 4, 8, 16, 32.
For example ,
ALIGN 4
In the above statement the assembler advances the location counter which is divisible by 4. In data
segment, assembler fills unused bytes with 0s. In code segment, assembler fills NOP instructions for
unused locations.

ORG: Originate
As assembler assembles the program, it uses a location counter to keep track of the allotted addresses.
If the ORG statement is not used, the location counter is initialized to 0000H. If an ORG value
statement is present at the start of a logical segment then first data item or first instruction will be
placed at an offset value. For example, if ORG 0200H statement is present at the start of code
segment, then the code will be placed from offset 0200H in code segment. In other words, the location
counter will be initialized with a value 0200H instead of 0000H. $ symbol can be used to refer current
value of location counter.

Computer Department, Jamia Polytechnic, Akkalkuwa 8


Microprocessor and Programming The Art of Assembly Language Programming

LENGTH:
This directive is an operator which tells the assembler to determine the number of elements in some
named data item, such as a string or an array.
When a statement like MOV CX, LENGTH string1 is used, the assembler will determine the no.
of elements in string1 and transfer it to CX.

PTR: Pointer
This is used to declare the type of a label, variable or memory operand. This is used with BYTE or
WORD. If used with BYTE, then the particular label, variable or memory operand is treated as an 8
bit quantity. If used with WORD, then the particular label, variable or memory operand is treated as a
16 bit quantity.
For Example,
MOV AL, BYTE PTR[SI]
INC BYTE PTR[BX]
MOV BX,WORD PTR[2000H]
INC WORD PTR[3000H]

OFFSET:
This directive tells the assembler to determine the offset or displacement of particular label from the
start of the segment which contains it.
Consider the fig. below:
(See next page)

The offset of NUM1 can be determined and transferred into BX as follows:


MOV BX, OFFSET NUM1 ;BX=2000H

Computer Department, Jamia Polytechnic, Akkalkuwa 9


Microprocessor and Programming The Art of Assembly Language Programming

SEG: Find segment address of a label


This directive tells the assembler to find the segment address of a label, variable or procedure e.g. let
my_array is a variable defined in data segment that has DS=7000H.
MOV AX, SEG my_array
In the above statement, SEG my_array will return my_arrays segment address which is 7000H and
MOV instruction will transfer that into AX. So after execution of this instruction AX=7000H.

EXTERN: External and PUBLIC:


This directive is used to tell the assembler that the names, procedures and labels declared after this
directive have already been defined in some other assembly module. While in the other module where
the names, procedures and labels actually appear, they must be declared PUBLIC using PUBLIC
directive. Suppose if one wants to call a procedure Factorial defined in module1 from module2, in
module1, it must be declared PUBLIC and in module2, it must be declared EXTERN. Following code
fragment explains this:

module1 SEGMENT
PUBLIC FACTORIAL FAR
module1 ENDS

module2 SEGMENT
EXTERN FACTORIAL: FAR
Module2 ENDS

GLOBAL:
The GLOBAL directive can be used in place of PUBLIC or EXTERN directive. This directive makes
a name or symbol available to other modules.
For Example,
GLOBAL DIVISOR makes DIVISOR public.

GROUP:
This directive is used to tell assembler to group the logical segment names which appear after the
directive into one logical group segment. This allows the contents of all segments to be accessed from
same group segment base.
For Example,
SMALL_SYSTEM GROUP CODE, DATA, STACK_SEG
ASSUME CS: SMALL_SYSTEM, DS: SMALL_SYSTEM, SS: SMALL_SYSTEM

Computer Department, Jamia Polytechnic, Akkalkuwa 10


Microprocessor and Programming The Art of Assembly Language Programming

INCLUDE: Include source code from file


This directive tells the assembler to insert a block of source code from the named file into current
source module.

Computer Department, Jamia Polytechnic, Akkalkuwa 11

You might also like