Unit 2 The Art of Assembly Language Programming
Unit 2 The Art of Assembly Language Programming
Step 2 Algorithm
Step 3 Flowchart
Converting algorithm to
Step 6 Assembly language program
1) Defining the problem: The first step in the program development is the careful analysis of problem. The
objective of the program should be clear.
2) Algorithm: It is the step wise procedure designed to perform operations according to the program which
will lead to correct results.
3) Flowchart: The flowchart is a graphically representation of the program operation or task. The specific
operation or task is represented by graphical symbol such as circle, rectangle, diagonal, square and
parallelogram etc. given below:
4) Initialization checklist: In this step, variables, constants and various parts of the system such as
segment registers, stack, flags etc. are initialized properly.
5) Choosing instructions: It includes selecting the proper instructions that will perform the
desired operations.
6) Converting algorithm to assembly language program : In this step , the instructions are arranged in
the proper sequence according to algorithm and then converted into machine language so that the
desired output can be obtained.
Software tools are used for editing, assembling, linking, and debugging assembly language
programming. These tools are briefly explained below.
Editors
Assembler
Linker
Debugger.
Editor:
1. It is software which allows you to type and edit assembly language program statements.
2. It enables you to create, edit, save ,copy and make modification in the source file.
3. The file created in editor is an ASCII format and is known as source file.
4. You can use NotePad , Norton editor,Dos editor (EDIT) any other editor that produces plain ASCII text
files. You can also use the ConTEXT editor.
Assembler:
1. Assembler is a program that translates assembly language program to the correct binary code.
2. It accepts source file .asm
3. It also generates the file called as object file with extension .obj.
4. It also displays syntax errors in the program, if any.
5. It also creates list file (.LST) AND cross refernce file (.CRF).
6. These include MASM (Macro Assembler from Microsoft), TASM (Turbo Assembler from Borland),
NASM (Netwide Assembler for both Windows and Linux), and GNU assembler distributed by the
free software foundation
Linker:
1. It is a programming tool used to convert object code into executable code.
2. It requires input as object code.
3. It combines more than one separated assembled modules into one executable module such as two more
assembly program or an assembly language with a c program.
4. It generates .EXE file.
Debugger:
1. It is programming tool.
2. It is used for tracing/locating and correcting errors.
3. It allows the execution of .EXE program in single step mode under the control of user.
1) DB(Define data Byte) - DB directive is used to declare a byte type. Variable size of variable is 1 byte.
General form:
Name_Of_Varible DB Initialiation_value(,s)
2) DW (Define data Word) - The DW directive is used to define a variable of type word. Variable size
of variable is 2 byte.
General form :
Name_Of_Varible DW Initialiation_value(,s)
Example:
PRICE1 DW 1234h
PRICE2 DW ?
3) DD (Define DoubleWord) - This directive is used to define a variable of type double. Variable size of
variable is 4 byte.
General form :
Name_Of_Varible DD Initialiation_value(,s)
Exmaple:PRICE1 DD 11223344H
PRICE2 DD ?
PRICE3 DD 5 DUP(0)
4) DQ (Define Quad Word) - This directive is used to define a variable of type quad word. Variable size
of variable is 8 byte.
General form :
Name_Of_Varible DQ Initialiation_value(,s)
5) DT (Define Ten Bytes) - This directive is used to define a variable which is 10 bytes in length.
General form :
Name_Of_Varible DT Initialiation_value(,s)
6) STRUCT:Structure Declaration : The directive STRUCT is used to declare the data type which is a
collection of primary data types (DB,DW,DD).
General form :
Structure_Name STRUCT
………………..
………………..
………………..
………………..
Structure_NameENDS
7) RECORD
The directive RECORD is used to define a bit pattern within a byte or a word.
It is similar to the bit-wise access in C language.
The RECORD definition helps in encoding or decoding of bit for which some meaning is assigned.
General form
Record_name RECORD
Field_Specification_1….Field_Specificationn_N
Examples
Bit 7, 6, 5 ; Baud Rate
Bit 4, 3 ; Parity
Bit 2 ; Stop Bits Bit 1, 0 : World
8) EQU (Equate to) - This EQU directive is used to give a name to some value or to a symbol. OR The
EQU directive is used to declare the symbols to which some constant value is assigned.
Each time the assembler finds the name in the program, it will replace the name with the value or symbol
you given to that name.
General form :
Symbol_Name EQU EXPRESSION
Example: FACTOR EQU 03H
PI EQU 3.14
9) ORG-Originate : The directives ORG assigns the location counter with the value specified in the
directive. It helps in placing the machine code in the specified location while translating the instructions
into machine codes by the assembler.
General Form:
ORG Numeric_value
Example:
ORG 100H
10) ALIGN :Alignment of memory addresses:
The directive ALIGN is used to force the assembler to align the next data item or instruction according
to given value.
General Form:
ALIGN Numeric_value ;The number must be a power of 2 such as 2,4,8,16.
Example :
ALIGN 4
General Form:
EVEN
Example : DATA SEGMENT
Name db ‘xyz’
EVEN
Age db 20
DATA ENDS
12) LABEL - The directive LABEL enables you to redefine the attribute of a data variable or instruction
label.
General form
Variable_Name LABEL Type_Specifier
Examples
TEMP LABEL BYTE
NUM LABEL WORD
1) ASSUME directive: It is used to give logical names to the different segments. When program is loaded,
the processor segment register should point to the respective logical segments.
General form:
ASSUME Seg _ Reg: Seg _ Name
Example: Assume cs:code, ds:data, ss:stack
2) SEGMENT and ENDS directives: They are used to mark the beginning and end of the particular
segment.
General form:
segment_name
;code goes here
segment_name ENDS
Example:
For example, data segment can be declared as:
DATA SEGMENT
X DB 10H
Y DB 20H
DATA ENDS
CODE SEGMENT
Program Code definition here
CODE ENDS
1) LENGTH: The directive LENGTH informs the assembler about the number of the elements in a data
items such as array.
General Form:
LENGTH Variable_name
Example:
MOV CX, LENGTH ARRAY
This statement, when assembled, will substitute the length of the array ARRAY in bytes, in the instruction.
2) SIZE : The directive SIZE is same as LENGTH except that it returns the number of bytes allocated to
the data item instead of the number of elements in it.
General Form:
SIZE Variable_name
Example:
MOV CX, SIZE ARRAY
3) OFFSET: The directive OFFSET informs the assembler to determine the displacement of the specified
variable with respect to the base of the segment.
General Form:
OFFSET Variable_name
Example:
MOV SI, OFFSET ARRAY
4) SEG: The directive SEG is used to determine the segment in which the specified data items is
defined.
General form:-
SEG Variable _ Name
Example:-
MOV DS, SEG MSG
5) TYPE - TYPE operator instructs the assembler to determine the type of a variable and determines
the number of bytes specified to that variable.
General Form:
TYPE Variable_name
Example:
MOV DX, TYPE NUM
D) Data Control Directives:
1. PUBLIC
2. EXTRN: EXTERNAL
3. PTR: POINTER
1) PUBLIC - The PUBLIC directive is used to instruct the assembler that a specified name or label will
be accessed from other modules.
General Form: PUBLIC variable1,variable2,……………….variable N
Example: PUBLIC DIVISOR, DIVIDEND ; these two variables are public so these are available to
others modules.
3) PTR -Pointer: The directive PTR is used to indicate the type of the memory access i.e.
BYTE/WORD/DWORD.
General Form:
PTR Variable_Name
Example:
INC BYTE PTR [DI]
ADD AL,BYTE PTR NUM
1. PROC : PROCEDURE
2. ENDP: END OF PROCEDURE
1. PROC : PROCEDURE
They are used to define procedures in assembly language programs. They mark the beginning of the
procedure. The term near or far is used to specify the type of the procedure.
Syntax: Proc_name PROC <NEAR/FAR>
;body of the procedure
Proc_name ENDP
NEAR
A near call refers a procedure which is in the same code segment.
FAR
A Far call refers a procedure which is in different code segment.
1. MACRO
2. ENDM: END OF MACRO
1. MACRO :
They are used to define macros in assembly language programs. They mark the beginning of the
MACRO.
General Macro_name MACRO <set of parameters>
Form:
..
.. ;body of the macro
ENDM
It consists of name of a MACRO followed by keyword MACRO and MACRO ARGUMENT.
2. ENDM: END OF MACRO : The directive ENDM informs the assembler the end of the MACRO.
General Form :
ENDM