You are on page 1of 13

UNIT-II

THE ART OF ASSEMBLY


Marks-8
LANGUAGE PROGRAMMING

2.1 Program development steps:


Defining problem and constrains
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

2.1 PROGRAM DEVELOPMENT STEPS:


Q. State the steps involved in program development. (4M)
Q. List the program development steps for assembly language programming. (2M)
Q. Demonstrate in detail the program development steps in assembly language programming. (6M)
Q. What is flowchart? Sketch any four symbols use in flowchart. State their use.( 3 M)
Q. Define flowchart.( 1 M)
Q. What is algorithm.(1M)
Q. Draw the symbols used in a flowchart while developing ALP. Mention the use of each symbol. (4 M)

Program development steps :

Program Development Steps

Step 1  Defining the problem

Step 2  Algorithm

Step 3  Flowchart

Step 4  Initialization checklist

Step 5  Choosing instructions

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.

Q. List the major steps in developing an Assembly language program.


1. Defining the problem
2. Algorithm
3. Flowchart
4. Initialization checklist
5. Choosing instructions
6. Converting algorithms to assembly language program

2.2 ASSEMBLY LANGUAGE PROGRAMMING TOOLS:

Q. State the function of Editor (2M)


Q. State the function of Assembler (2M)
Q. State the function of Linker (2M)
Q. State the function of Debugger (2M)
Q. Describe Assembler, Linker and Debugger.(4M)

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.

Q. State the role Debugger in assembly language programming. (2M)


Debugger: Debugger is the program that allows the extension of program in single step mode under the control
of the user. The process of locating & correcting errors using a debugger is known as Debugger. Some examples
of debugger are DOS debug command Borland turbo debugger TD, Microsoft debugger known as code view cv,
etc…
Q. List assembly language programming tools.(2M)
Ans: 1. Editors
2. Assembler
3. Linker
4. Debugger.

Q. Describe model of assembly language programming


Note : Any one model can be considered.
Model 1 :
1) Using SEGMENT, ASSUME and ENDS directives
2) In this Data_Seg is the name of the data segment where data are declared
3) Code_Seg is the name of the code segment where code is written
4) Start is the label name used to initialize the CS register.
5) ENDS to indicate the ends of code and data segment
6) END marks the end of the program.
Model 2 :
a. Using .Data and .code directive
b. In this, .model small is used to indicate small memory model is used in the program
c. Stack 100 to indicate 100 word memory locations reserved for stack
d. Data indicates start of the data segment where data declaration of the program is made.
e. Code indicates the beginning of the code segment h. END to indicate the termination of the program.
PROGRAM DEVELOPMENT PROCESS
1 Source file creation
2 Object code generation
3 Executable file creation
4 Program Running
5 Program Testing
6 Program Debugging

1. Source file creation:-


The source file is created and edited using text editor and must have extension.ASM.
2. Object code generation:-
The assembler is used to translate assembly language source code to re- locatable object code.
3. Executable file creation:-
Linker is used to create an executable file.
4. Program Running:-
The executable file can be run by entering the name of executable file on the prompt and by passing
ENTER key on the keyboard.
5 Program Testing:-
The result or output generated by the program has to be tested for their validity.
If any error occurs in the result, then program should be debug.
6 Program Debugging:-
The errors in the program can be located using debugger.
The executable file of the program to be debugged must be created with the program debug option.

2.3 ASSEMBLER DIRECTIVES:


Q. List any four assembler directives and explain any two of them.(4 M)
Q. List any four assembler directives. State the functions of any two assembler directives. (4M)
Q. What are assembler directives? Explain any two assembler directives.(4 M)
Q. Explain any two assembler directives of 8086.
Q. Describe the Assembler Directives used in 8086 programming.(2M)
i) DB ii)EQU
Q. Describe the functions of the following Assembler Directives (3M)
1) DD 2) DB 3)DUP
Q. Explain the following Assembler Directives.(3M)
1) ORG 2) EQU 3)DD
Q. Explain the following Assembler Directives.(4M)
i) DB
ii) DW
iii) DD
iv) DQ
Q. Explain the following Assembler Directives.(3M)
i) DD ii) DB iii) DUP iv) EQU
Assembler directives:
Assembly language program supports a number of reserve word i.e. key word that enables you to control
the way in which a program assembles and lists. These words are called as assembler directives, act only
during the assembly of the program and generate no machine executable code.
So, directives are the statement that gives direction to the assembler and also called as pseudo-
instructions that are no translated in to the machine code. Directives are divided into various categories.

Directives are classified into following categories.


A. Data definition and storage allocation directives
B. Program organization directives
C. Value returning attribute directives
D. Data control directives
E. Procedure directives
F. Macro directives
G. Branch Displacement Directives
H. File Inclusion Directives
I. Target machine code generation control directives

A) Data definition and storage allocation directives:


Data definition directives are used to define the program variable and allocate a specified amount of memory
to them. They are as follows;
1. DB: Define Bytes
2. DW: Define Words
3. DD: Define Double Word
4. DQ: Define Quad Word
5. DT : Define Ten Byte
6. STRUCT: Structure Declaration
7. RECORD
8. EQU: Equate to
9. ORG: Originate
10. ALIGN: Alignment of memory addresses.
11. EVEN: Align as even memory location
12. LABEL
13. DUP: Duplicate memory location

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)

Example PRICE1 DB 49h


PRICE2 DB ?

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)

Exmaple :PRICE1 DQ 1122334455667788h


PRICE2 DQ ?
PRICE3 DQ 5 DUP(0)

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)

Exmaple :PRICE1 DT 00112233445566778899h


PRICE2 DT ?
PRICE3 DT 5 DUP(0)

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

Exmaple :STUDENT STRUCT


STUD_NAME DB 15 DUP(0)
STUD_ROLLNO DB 01H
STUD_PER DB ?
STUDENT ENDS

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

Q. Describe Assembler Directive: EVEN (1 M)


11) EVEN –Align as even memory location: This EVEN directive is used to inform the assembler to
increment the location counter to the next even memory address if it is not already in the even
address.
If it is location counter is already pointing to even memory address, it should not be incremented.

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

13) DUP: Duplicate memory location :


The DUP directive can be used to generate multiple bytes or words with known as well as un-
initialized values.
General form :
DUP ?
Example:
Table dw 100 DUP(0)
Stars db 50 dup(’*’)
ARRAY3 DB 30 DUP(?)

B) Program Organization Directives:

Q. Describe the functions of the following Assembler Directives (2M)


1. ASSUME
2. SEGMENT

These directives are used to organize program.


3. ASSUME
4. SEGMENT
5. ENDS: END OF THE SEGMENT
6. END:END OF THE PROGRAM
7. CODE: Simplified CODE Segment directive
8. DATA: Simplified DATA Segment directive
9. STACK: Simplified Stack Segment directive
10. MODEL: Memory model declaration for segments.

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

Q. Describe the meaning of the directives : ENDS (1M)


3) ENDS: END OF THE SEGMENT
The directive ENDS informs the assembler the end of the segment.
General form: ENDS
Example :
DATA SEGMENT
Program Data definition here
DATA ENDS

Q. Describe the assembler directive : END (1M)


4) END:
The directive END is used to inform assembler about the end of the program.
General form: END
Example:
END [Start_ Address]
(The optional start _ address specifies the location in the code segment where execution is to be start)

4) .CODE: Simplified CODE segment directive:


This simplified segment directive defines the code segment. All executable code must be defined in
this segment.
General form:
.CODE

5) .DATA: Simplified DATA segment directive:


This simplified segment directive defines the data segment. All data definition and declaration must
be placed in this segment.
General Form:
.DATA

6) .STACK: Simplified STACK segment directive:


This simplified segment directive defines the stack segment and default size of the stack segment is
1024 bytes.
General Form:
.STACK

7) .MODEL :Memory model declaration for segments :


This simplified segment directive creates default segments.
General Form:
.MODEL memory_models
Memory models are
o .SMALL: All data in one segment and all code in one segment.
o .MEDIUM: All data in one segment but code in more than one segment.
o .COMPACT: Data in more than one segment but code in one segment.
o .LARGE: both data and code in more than one segment, but no array may exceed 64 kbytes.
o .HUGE: both data and code in more than one segment, but array may exceed 64 kbytes.

C) Value Returning Attribute Directive:


1. LENGTH
2. SIZE
3. OFFSET
4. SEG: SEGMENT
5. TYPE

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.

2) EXTRN: External and PUBLIC:


The directive EXTRN informs the assembler that the data items or label following the directive will be
used in a program module, which is defined in the other program modules.
General Form:Extrn variable1,variable2,……variable N
Example :EXTRN msg:byte, name:word

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

E) Procedure Definition Directive:

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.

2. ENDP : END OF PROCEDURE


ENDP The directive is used along with the name of the procedure to indicate the end of a procedure to the
assembler.
General Form :
Procedure _ name

ENDP
EXAMPLE:
FACTORIAL

ENDP

F) Macro Definition Directives:

A Macro is a set of instructions grouped under a single unit.

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

G) Branch Displacement Directive


1. SHORT
2. LABEL

1. SHORT : One byte displacement is required to code to jump instruction.


Example :
JMP SHORT NEXT

2. LABEL: Assign a name to the current value in the location counter.


General Form :
LABLE LABEL_NAME LABEL_TYPE

H) File Inclusion Directives:


Q. Explain the assembler directive: INCLUDE (1M)
INCLUDE - This INCLUDE directive informs the assembler to include the statement defined in the
include file. The name of the include file follows the statement INCLUDE.
General form: INCLUDE <file path specified with file name>
Example: INCLUDE C:\TASM\MACRO.LIB
INCLUDE C:\TASM\MYPROC.LIB

I) Target machine code Generation Control Directive:


• The assembler will recognized only 8086 instructions by default.
• It is because the program that confirm themselves to the 8086 instruction set that can run on any IBM PC,
irrespective of the microprocessor i.e. 8086, 80186, 80286 etc.
• So, special directive can be used at the beginning of a program to inform to the assembler to generate a
code for the specific processor and these are listed below.
• General form.
.186- generate machine code for 80186 processor only
.286 - generate machine code for 80286 processor only
.386 generate machine code for 80386 processor only
.486 - generate machine code for 80486 processor only
.586 generate machine code for 80586 processor only i.e. pentium

Difference between instructions and assembler directives:

Assembler Directives Instructions


It directs the assembler to perform certain
actions during assembly phase. It is the operation performed by processor.
Assembler directive does not create any
machine code. Assembler instruction generates machine code.
It does not contribute to program size. It contributes to program size.
e.g. assume,proc,endp,ends,segment etc. E.g. add,sub,mul,div etc

You might also like