You are on page 1of 9

Topic 2 :The Art of Assembly Language Programming ( 8 Marks)

2.1 Program development steps


● Defining problem,
● Writing Algorithms
● Flowchart
● Initialization checklist
● Choosing instructions
● Converting algorithms to assembly language programs.
2.2 Assembly Language Programming Tools
● Editors
● Assembler
● Linker
● Debugger.
2.3 Assembler directives
Program Development steps
1. Defining the problem
The first step in writing program is to think very carefully about the problem that you want
the program to solve.
2. Algorithm
The formula or sequence of operations or task need to perform by your program can be specified
as a step in general English is called algorithm.
3. Flowchart
The flowchart is a graphically representation of the program operation or task.
Flowchart Symbols

Subroutine

4. Initialization checklist
Initialization task is to make the checklist of entire variables, constants, all the registers, flags
and programmable ports
5. Choosing instructions
We should choose those instructions that make program smaller in size and more importantly
efficient in execution.
6. Converting algorithms to assembly language program
Every step in the algorithm is converted into program statement using correct and efficient
instructions or group of instructions.
What are the major steps in developing ALP?
1) The first step in the process is to develop assembly program text. Assemblers typically provide
assembly text editors to help program development. At assemble time, the input text is passed to
the assembler, which parses the text, emitting a program listing and possibly error reports.
2) If there are no errors, the assembler produces a binary machine language module. The module
must contain information about where the program or module is to be loaded in memory, and if it
contains the starting address of the program, this start symbol must be made known.
3) If the module has to be linked with other modules, then it must include this additional linkage
information. This means that all labels in the module that have to be visible to other modules
must be specified as public symbols. Similarly, all labels that are defined in other modules must
be specified as external symbols.
4) At link time, the linker combines separately assembled modules into a single load module. The
linker will also add any initialization or finalization code to allow the operating system to start
the program or return control to the OS, once the program has completed.
5) At load time, the program loader copies the program into computer’s main memory.

Describe the function of following assembly language programming tools.


1) Editor
Editor is a program which allows you to create a file containing the assembly language
statements for your program
Program (source code) is typed using editor. It is needed to store the assembly language program source
code file with .ASM extemsion
e.g Myprog.ASM
Examples of editor are : EDIT from Dos, Notepad and Wordpad from Windows

2) Assembler
An assembler is a program used to translate or convert the assembly language mnemonics
of source code program for instructions to the corresponding binary codes(machine language)
which is called as object code or machine code of the program.
It uses source file with .ASM extension as input.
Ex TASM (Borland’s Turbo Assembler), MASM (Microsoft Macro Assembler) etc.

3) LINKER:
A linker is a program used to join together several objects files into one large object file. 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. When all the module work, they can be linked
together to form a large functioning program.
The linker produces a link file which contains the binary codes for all the combined modules. The
linker also produces a link map which contains the address information to the program, it only
assigns relative addresses starting from zero. This form of the program is said to be relocatable,
because it can be put anywhere in memory to be run.
The command on command prompt for converting .obj file to ,EXE file is as given below:
C : \ MASM \ BIN \ > LINK myprog.obj;
C : \ TASM \ > TLINK myprog.obj;

DEBUGGER:
A debugger is a program which allows us to load object code program into system memory execute
the program, and debug it.
How does a debugger help in debugging a program?
1. The debugger allows us to look at the contents of registers and memory locations after our
program runs.
2. It allows us to change the contents of register and memory location and return the program.
3. Some debugger allows us to stop execution after each instruction so we can check or alter
memory and register
4. A debugger also allows us to set a breakdown at any point in our program. When we run a
program, the system will execute instructions up to this breakpoint and stop. We can then examine
register and memory contents to see if the result are correct at that point. If the result are correct, we
can move the break point to a later point in our program. If result are not correct we can check the
program up to that point to find out why are not correct.
Ex: DOS Debug command,Borlands turbo debugger TD.

State functions of following assembly language programming tool.


i) Assembler
1. Assembler is a program that translates assembly language program to the correct binary code.
2. It also generates the file called as object file with extension .obj.
3. It also displays syntax errors in the program, if any.
4. It can be also be used to produce list(.lst) and .crf files

ii) Linker
1. It is a programming tool used to convert Object code into executable program.
2. It combines ,if requested ,more than one separated assembled modules into one executable
module such as two or more assembly programs or an assembly language with C program.
3. It generates .EXE module
(iii) Editor
1. It is a program which helps to construct assembly language program with a file extension
.asm, in right format so that the assembler will translate it to machine language.
2. It enables one to create, edit, save, copy and make modification in source file.
Assembler Directive

Data Definition an storage allocation Directive


1)DB :Define byte(8 bits)
It is used to declare a byte type variable of 8 bit. It also can be used to declare an array of bytes.
The range of values that can be stored in a byte is 0 to 255 for unsigned numbers and -128 to +127 for
unsigned numbers.
e.g
NUM DB ? ; Allocate one memory location
NAME DB ‘VIJAY’ ; Allocate five memory locations; arrary of five bytes.
ARRAY DB 12,25,26,55,65 ; Allocate five memory locations
List DB 100 DUP(0) ; Allocate 100 memory locations.
2) DW: - Define Word (16-bits)
It is used to tell the assembler to define a variable of type word in memory or to reserve
storage locations of type word (16) in memory.
e.g. BLOCK DW 1234H, 3456H, 5678H : Declare array of 3 words.
STORAGE DW 10DUP (2222H) ; Reserve array of 10 nos. with no. 2222H.
STORAGE DW 100 DUP(?) ; Reserve 100 words of storage in memory and give it the name
STORAGE, but leave words uninitialized.

3) DD: -Define Double word (32-bits)


It is used to declare a variable of type doubleword or to reserve memory locations which can be accessed
as type doubleword(32-bits.)
e.g. NUMBER DD 1,2,3,4,9 ; allocated 20 memory locations.
NUM DD ? ;Allocate 4 memory locations
List DD 10 DUP(0) ;allocate forty memory locations.
4) DQ : Define QuadWord(4 words)
This directive is used to tell the assembler to declare a variable 4 words in length or to reserve 4 words of
storage
NUM DQ ? ;Allocate 8 memory locations
Table DQ 1,2,3,5,9 ;Allocate forty memory locations.
List DQ 10 DUP(0) ; Allocate eighty memory locations.

5) DT :Define Ten byte .This directive is used to tell the assembler to define a variable which is 10 bytes in
length or to reserve 10 bytes of storage in memory.
NUM DT ? ; Allocate Ten memory locations;
ARRAY DT 1 ,2 ,3, 4, 9 ; Allocate fifty memory locations
List DB 10 DUP(0) ; Allocate 100 memory locations.

6) STRUCT : Structure declaration


This directive is used to declare the datatype which is a collection of primary datatypes( DB,DW,DD)
Syntax: Structure _name STRUCT
….
Structure _name ENDS
e.g. EMPLOYEE STRUCT
EMP_NUM DW ?
EMP_NAME DB 25
DUP(0) EMP_AGE DB ?
EMPLOYEE ENDS

Structure declaration does not allocate memory , but storage space is allocated when structure variable is
defined.
Variable Structure_name <initialization>
E.g E1.EMPLOYEE <01,’Martin’,28>
Structure variable can be accessed by using dot operator
Varablename.fieldname
e.g. MOV AX,E1.EMP_NUM

7)EQU :Equate to

The EQU directive is used to declare the micro symbols to which some constant value is assigned. Micro
assembler will replace every occurrence of the symbol in a program by its value.
Syntax: Symbol_name EQU expression
• e.g NUM EQU 50

• CORRECTION_FACTOR EQU 100

MOV AL, CORRECTION_FACTOR ;USE IN PROGRAM


8) ORG : Originate
The directive ORG assigns the location counter with value specified in the directive.
It helps in placing the machine code in the specified location while translating instructions into machine
codes by the assembler. $ is used to indicate current value of location counter
Syntax: ORG [$+] Numeric_value
e.g ORG 2000H ; set location counter to 2000H
ORG $+ 100 ; increment value of location counter by 100 from its current.

9) ALIGN :Alignment of memory addresses


This directive is used to force the assembler to align the next data item or instruction according to given
value.
Syntax: ALIGN Numeric_value
e.g ALIGN 4 ;advances location counter to the next address that is evenly divisible by 4

10) EVEN: - The directive even is used to tell the assembler to increment the location counter to
the next even memory address.
If the location counter is already pointing to an even address it should not be incremented. The 8086
reads/writes data in one machine cycle, if data is to be accessed from even location. It requires two
bus cycles to access a word from an odd memory location. This directive can be used in both Code
and Data segments.
Example: - DATA SEGMENT
Array DB 9 DUP (?)
Even
Block DW 100H DUP (0)
DATA ENDS

11) LABEL : The LABEL directive is used to give a name to the current value in the
location counter .IT enables you to redefine the attributes of a data variable or instruction
label.
Syntax: variable_name LABEL type_specifier
e.g STACK_TOP LABEL WORD
TEMP LABEL BYTE
12) DUP : Duplicate memory location
This directive can be used to generate multiple bytes or words with known as well as un-
initialized values.
e.g TABLE DW 100 DUP(0) ; Create array of 100 words all contains data 0
Array DB 9 DUP (?) ; Create array of 9 bytes with unknown value.

PROGRAM ORGANIZATION DIRECTIVE


8086 programs are organized as collection of logical segment. The directive used to organize the program
segments are SEGMENT,END ,ENDS,ASSUME
1) ASSUME: - Assume directive is used to tell Assembler the name of the logical segment it
should use for the specified segment.
When program is loaded the processor segment register should point to the respective logical segments.
Example: - Assume CS: MSBTE_CODE, DS: MSBTE_DATA

2) SEGMENT :Used to indicate the beginning of logical segment . Preceding the SEGMENT directive is the
name you want to give the segment
Syntax:
Segment_Name SEGMENT [Word/Public]
WORD indicates that the segment has to be located at the next available address; otherwise the segment will be
located at the next available paragraph which might waste upto 15 bytes of the memory.
Type specifier PUBLIC indicates that this segment may be put together with other segments named CODE from
other assembly modules when modules are linked together.
e.g My_data SEGMENT

My_data ENDS

3) ENDS : End of segment .The directive ENDS is used with the name of a segment to indicate the end
of that logical segment (which contains instructions or data).
CODE SEGMENT ;Start of logical segment
------ ;Instruction statements
CODE ENDS ;End of segment named CODE.

4) END :End of Program


This directive is used to inform assembler the end of the program. The assembler will ignore any statements
after an END directive.

5) Dot . directives
.Directives are set of directives used by memory models such as Tiny and Small models making assembly
language structures simpler. They define current memory model in use, type of CPU in use.
Different .Directives are
a).MODEL Directive :
syntax: .MODEL <Memory_model_type>
ex: .Model Small
● Tiny- Single segment, create .COM executable code which is machine independent.
● Small- Single Code Segment and Single Data Segment.
● Medium- Multiple Code segment and Single Data Segment.
● Compact –Single Code segment and Multiple Data Segment.
● Large - Multiple code as well as Multiple Data Segment (segments within 64K size)
● Huge – Multiple copies as well as Multiple data segments.(no 64K size
limit) All model except tiny creates .EXE code which is OS dependent.

b).CODE: (Simplified code segment directive)


Syntax: .CODE
This directive declares the start or beginning of Code segment .All executable code must be placed in
this segment
c).DATA: (Simplified data segment directive)
Syntax: .DATA
This directive declares the start or beginning of the Data Segment. All data definition and declaration
must be placed in this segment.
d).STACK: (Simplified stack segment directive)
Syntax: .STACK <size>
Example: .STACK 1024 (Allocates 1024 bytes to the stack)
This directive defines the stack segment; default size of stack is 1024bytes. It automatically associates SS
register with it and initializes it.

Value Returning Attribute Directive


a) Length : The directive length determines the number of elements in some named data item, such as a
string or an array
Syntax: LENGTH Variable_Name
Mov CX,Length Array ; Find no of elements in Array and load length to CX.
b) Size :This directive returns the number of bytes allocated to the data item instead of number of
elements in it
Syntax: SIZE Variable_name

Example : MOV AX, SIZE Total ; Find no of bytes in Total and load length to CX.

c) OFFSET: This directive tells the assembler to determine the offset or displacement of a named data item
or procedure from the start of segment which contains it. Used to load the offset of variable into a register
sothat variable can be accessed with one of the indexed addressing mode.
Syntax : OFFSET Variable_name
Ex OFFSET BX,PRICES ;determine the offset of variable and PRICES from seg start and load
displacement in BX
d) SEG :Segment
Used to determine the segment in which the specified data item is defined.
Syntax: SEG Variable_name

Example : MOV DS,SEG MSG


MOV ES,SEG LIST
e) TYPE : determine type of data item , no of bytes allocated to
data item Allocates one byte for DB , Two byte for DW, four
byte for DD Syntax TYPE Variable_name
Ex : ADD BC,Type Num ;
Procedure directive
1) PROC :Procedure
Used to identify start of procedure and follow name of procedure.
NEAR and Far indicate type of procedure. If term not specified, assembler assumes NEAR
procedure. The use of procedure type specifier helps the assembler to decide whether to code RET as
near or far. Syntax:Procedure_name PROC [NEAR/Far]
………
Endp
Ex. ADD_IT PROC NEAR ;Near procedure
……….
ADD_IT ENDP
Procedure is called by CALL ADD_IT
2) ENDP :Ends of procedure
ENDP directive is used along with the name of the procedure to indicate the end of a procedure to
the assembler
Syntax : procedure_name ENDP
Example:
SQUARE_NUM PROC; It start the procedure
Some procedural steps to find the square root of a number
SQUARE_NUM ENDP ;Hear it is the End for the procedure

Data Control directive

1)Public : This directive is used to tell the assembler that a specified name or label will be accessed
from other modules. It helps in managing the multiple program modules by sharing the global
variable or procedures.
Ex . PUBLIC ABC,XYZ makes two variables available to other assembly modules.

2)EXTERN :External

This directive is used to tell the assembler that the names or labels following the directive are
in some other assembly module.
Syntax for variable reference
EXTERN Varaiable_name1:reference_type,…… variable_nameN:reference _type
For procedure reference
EXTERN procedure_name:[NEAR/FAR]
Ex: EXTERN msg:byte,name:word
EXTERN Display:FAR
3)PTR
Uses to indicate type of memory access i.e BYTE/WORD/DWORD
If instruction INC[SI] is given , we will not be able to decide whether to code for byte
increment or word increment.so PTR is used as
INC BYTE PTR[SI]
ADD AL, WORD PTR[SI]

File inclusion Directive


INCLUDE-Include source code code from file
This directive used to tell the assembler to insert a block of source code from named file into current
source module. The include file must exist in the directory in the path specified otherwise, the
assembler gives an error.
Syntax :INCLUDE <file path specification with file name>
Example INCLUDE C:\Tasm\Macro.lib
INCLUDE C:\Tasm\BIN\MYProc.lib

Differenciate between Assembler directive and Instructions


Srno Assembler directive Instructions

1 Assembler directive gives direction to assembler Instructions perform operation specified


by it using processor

2 Assembler directive does not execute. Instructions are executed by processor

You might also like