You are on page 1of 11

ASSEMBLER DIRECTIVES:

 Assembler instructions are tanslated into machine language instructions and correspond to executable
statements in high level language programs.
 As high-level language programs must have non-executable statements to pre-assign values, reserve
storage, assign names to constants, form data structures, and terminate a compilation, assembler
language program must contain directives to perform similar tasks.
 The directions to assembler are commonly called assembler directives or pseudo operations. These are
not instructions for 8086.
Data Definition and Storage Allocation:
The statements that pre-assign data and reserve storage have the form:
Variable

Mnemonic

Operand,.., operand

;comments

Variable: Is optional, if it is present it is assigned the offset of the first byte that is reserved by the directive. A
variable must be terminated by a blank, not a colon.
Operand: Data to be pre-assigned or the amount of space to be reserved. For data, operand must be a constant,
an expression that evaluates constant, or string constant.
Mnemonic: Determines the length of each operand. They are:
DB: DEFINE BYTE

It is used to declare a byte-type variable, or to set aside one or more storage locations of type byte in
memory. Each occupies one byte.

Example:
CURRENT_TEMP DB 42h
;tells the assembler to reserve 1 byte for variable
CURRENT_TEMP and put the value 42h in that memory location when the program is loaded into RAM to
be run.
DW: DEFINE WORD

It is used to declare a word-type variable, or to set aside one or more storage locations of type word in
memory. Each operand data occupies one word, with its low-order part being in the first byte and its
high-order part being in the second byte.

Example: MULTIPLIER DW 4267h ;declares a variable of type word named MULTIPLIER, and also
tells the assembler that the variable MULTIPLIER should be initialized with the value 4267h when the
program is loaded into memory to be run.
DD: DEFINE DOUBLEWORD

It is used to declare a variable of type double-word, or to reserve memory locations which can be
accessed as type double-word. Each operand data is two words long with the low-order word followed
by high-order word.

Example: MULTIPLIER DD 32454267h


;will define a variable of type double-word named
MULTIPLIER, and also tells the assembler that the variable MULTIPLIER should be initialized with the
value 32454267h when the program is loaded into memory to be run.

The low word 4267h will be put in memory at a lower address than the high word 3245h.
This type of declaration is often used with LES or LDS instructions.
Example:
LES DI, MULTIPLIER
;will copy the low word 4267h into DI register and high
word 3245h into ES register.
Example:

DATA_BYTE
DATA_WORD
DATA_DW
Packed
Unpacked
MSG
MSG

DB
DW
DD
DB
DB
DB
DB
DB
DW

10, 4, 10h
100, 100h, -5
3*20, 0FFFFh
78h, 56h, 34h, 12h
8h, 7h, 6h, 5h, 4h, 3h, 2h, 1h
H, E, L, L, O
HELLO
AB
AB

 Pre-assignment by individually listing the contents of the locations is satisfactory if there are only
few locations to be filled, but for several operands this method would be burdensome, ASM-68
permits the use of the duplication operator. The format is:
Exp DUP (operand,..,operand),
Exp: Expression that evaluates to +ve integer
ARRAY1 DB 2 DUP(0,1,2,?)

;This statement reserve 8 bytes for ARRAY1, it is equivalent to


ARRAY1 DB 0,1,2,?,0,1,2,?

00 01 02

00 01 02

 Dup can also be nested


Eg: Array DB
100 DUP (0, 2 DUP (1,2), 0, 3)
00 01 02 01 02 00 03 00 01 02 01 02 00 03 -

00 01 02 01 02 00 03

 The DW and DD directives may also used to generate offsets or full addresses of the variables or labels, but
DB cannot be used.
Example:
Table

DW
DW
DW

Par1
Par2
Par3

Table1

DD
DD

DATA1
DATA2

Table

Offset Par1

Table1

Offset Of Data1

Offset Par2

Segment of Data1

Offset Par3

Offset Of Data2
Segment of Data2

DQ: DEFINE QUADWORD

It is used to declare a variable of type 4 words in length, or to reserve 4 words of storage memory.
The statement MULTIPLIER DD 3245426732454267h, will define a variable named MULTIPLIER,
and initialize 4 words set aside with the specified number when the program is loaded into memory to be
run.

Examples:
STORAGE DQ 100 DUP(0)
;Reserves 100 quad-words of storage in memory and initializes them all to
0 when the program is loaded into memory to be run.
DT: DEFINE TEN BYTES

It is used to declare a variable of type 10 bytes in length, or to reserve 10 bytes of storage memory.
The statement MULTIPLIER DT 32454267324542674578h, will declare an array named MULTIPLIER
which is 10 bytes in length, and initialize 10 bytes with the values 32454267324542674578 when the
program is loaded into memory to be run.

Examples:
STORAGE DT 20h DUP(0)
;will declare an array of 20h blocks of 10 bytes each and initialize all 320
bytes 00 when the program is loaded into memory to be run.
TYPE:
 The variable with a storage definition represents the offset in the current segment of the first byte of first
data item and has a type attribute that indicates the length of each data item. Length is 1 for DB, 2 for
DW, 4 for DD.
 An assembler uses type attribute to determine whether the machine instruction is to operate on a byte or
word, ie W is to be set to 0 or 1.
 The TYPE operator can be used in an instruction such as ADD BX, TYPE WORD_ARRAY, where we
want increment BX to point to the next word in an array.
PTR: POINTER
 The assembler is able to reduce coding errors by verifying both operands in a double operand instruction
have same type attribute.
 The PTR operator overrides the implicit type given to an operand, and has the form:

Type
PTR
Variable
Type: Byte, Word, Double-word
 The PTR operator is used to assign a specific type to a variable or to a label. It is necessary for
instructions where the type of the operand is not clear.
 For eg: when the assembler reads the statement INC [BX], it will not know whether to increment the
byte pointed by BX or to increment the word pointed by BX.
 The PTR operator clears the assembler how to code this instruction, INC BYTE PTR [BX], tells the
assembler that we want increment byte pointed by BX and INC WORD PTR [BX], tells the assembler
that we want increment word pointed by BX
 We also use the PTR operator to clarify the intentions when we use indirect Jump instructions.
 For eg: JMP [BX], does not tell the assembler whether to code this instruction for a near jump or for far
jump.
 The statement JMP WORD PTR [BX], assembler codes this instruction for near jump. The statement
JMP DWORD PTR [BX], assembler codes this instruction for far jump.
 The PTR operator can be used to override the declared type of a variable.
 For eg: we have declared an array of words as WORDS DW 3456h,5778h,2546h, normally we access
elements of this array as words. If we want to access a byte in the array, we can use an instruction such
as MOV AL, BYTE PTR WORDS.
LABEL:
 It may become tedious to use the PTR operator to frequently reference variables that are accessed as two
different types, a method has been provided for associating a location with two different variables.
 As the assembler assembles a section of data declarations or instruction statements, it uses location
counter to keep track of how many bytes from its the start of segment at any time.
 The LABEL directive is used to give a name to the current value in the location counter. The LABEL
directive must be followed by a term which specifies the type we want associated with that name.
 The LABEL directive, which has the form: Variable LABEL Type causes the variable to be typed and
assigned to the current offset.
 The directives BYTE_ARRAY LABEL BYTE,
WORD_ARRAY DW 50 DUP(?),
would assign both BYTE_ARRAY and WORD_ARRAY to the same location, the first byte of a 100byte block.
 The instruction
MOV WORD_ARRAY+2, 0 ;would set the 3rd and 4th bytes of the block to 0,
MOV BYTE_ARRAY+2, 0 ;would set the 3rd byte to 0.
 If the label going to be used as the destination for a jump or call, then the label must be specified as type
near or far.
 If the label going to be used to reference a data item, then the label must be specified as type byte, word,
or doubleword.
Examples:
STACK_SEG SEGMENT STACK
DW 100 DUP (0); Set aside 100 words for stack

STACK_TOP LABEL WORD; Give name to next location after last word in stack
STACK_SEG ENDS
LENGTH: NOT IMPLEMENTED IN IBM MASM
 LENGTH 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.
Example: MOV CX, LENGTH STRING1, assembler will determine the number of elements in
STRING1 and code this number as a part of the instruction. When the instruction executes the length of
the string will be loaded into CX.
 If the string was declared as a string of bytes, LENGTH will produce the number of bytes in the string.
If the string was declared as a string of words, LENGTH will produce the number of words in the string.
OFFSET:
 OFFSET is an operator which tells the assembler to determine the offset or displacement of the named
data item (variable) or procedure from the start of the segment which contains it.
 This operator is usually used to load the offset of the variable into a register so that the variable can be
accessed with one of the indexed addressing modes.
Example: MOV BX, OFFSET STRING1, assembler will determine the offset of the variable STRING1
from the start of the segment in which STRING1 is defined and code this displacement as a part of the
instruction.
 When the instruction executes the computed displacement will be loaded into BX. Then an instruction
such as ADD AL, [BX] is used to add a value from STRING1 to AL.
SHORT:
 The SHORT operator is used to tell the assembler that only a 1-byte displacement is needed to code a
Jump instruction. For JMP Destination, instruction in the program, the assembler will automatically
reserve 2 bytes for the displacement.
 Using the SHORT operator saves 1-byte of memory by telling the assembler that it needs to reserve only
1-byte for this particular jump.
 For this the destination must be in the range of -128 bytes to +127 bytes from the address of the
instruction after the jump.
Eg: JMP SHORT NEAR_LABEL
Segment Definition:
 The assembler must perform is to assign the offsets of the labels and variables as it translates the
instruction into machine language. The assembler must also pass to the linker (via the object modules)
all the information that the linker will need in putting the various segments and modules together to
form a program.
 To be able to assign the variable and label offsets the assembler must know the exact structure of each
segment. A data, extra data, or stack segment normally has the form:
Segment name
SEGMENT
Storage definition, allocation,
and alignment directives
Segment name
ENDS

 A code segment normally has the form:


Segment name
SEGMENT
Instructions and
instruction-related directives
Segment name
ENDS
SEGMENT:
 The SEGMENT directive is used to indicate the start of a logical segment.
For eg: CODE SEGMENT, tells the assembler that the start of a logical segment called CODE.
 The SEGMENT and ENDS directives are used to bracket a logical segment called code or data.
 Additional terms are added to a SEGMENT 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 contents of this
segment located on the next available word (even) address when segments are combined and given the
absolute addresses.
 Without this WORD addition, the segment will be located on the next available paragraph (16-byte)
address, which might waste as much as 15 bytes of memory.
 The statement CODE SEGMENT PUBLIC tells the assembler that this segment may be put together
with other segments named CODE from other assembly modules when the modules are linked together.
ENDS: END SEGMENT
 This directive is used with the name of a segment to indicate the end of that logical segment.
 ENDS is used with the SEGMENT directive to bracket a logical segment containing instruction or
data.
Example:
CODE SEGMENT

;Start of logical segment containing code instruction statements

CODE ENDS

; End of segment named CODE.

Example:
DATA_SEG SEGMENT
OPER1
OPER2
RESULT
DATA_SEG ENDS

DW
DW
DW

12
302
?

CODE_SEG SEGMENT
ASSUME
CS:CODE_SEG, DS:DATA_SEG
START:
MOV AX, DATA_SEG
MOV DS, AX
MOV AX, OPER1
ADD AX, OPER2
MOV RESULT, AX

CODE_SEG ENDS
END START

ASSUME:
 As the assembler translates the instructions it must know the exact correspondences between the
segments and segment registers. This allows the assembler to check for certain types of errors and
inconsistencies.
 It is used to tell the assembler the name of the logical segment it should use for a specified segment.
 The statement ASSUME CS: CODE tells the assembler that the instructions for a program are in a
logical segment called CODE.
 The statement ASSUME DS: DATA tells the assembler that for any program instructions which refers
data segment, it should use a logical segment called DATA.
 If we want use stack in program, we must tell the assembler the name of the logical segment we set up
as a stack with a statement ASSUME SS: STACK_HERE.
 For a program with string instructions which use DI, the assembler must be told what to assume for
extra segment with the statement ASSUME ES: STRING_DESTINATION.
Alignment Directive:
EVEN: ALLIGN ON EVEN MEMORY ADDRESS
 As the assembler assembles a section of data declarations or instruction statements, it uses a location
counter to keep track of how many bytes it is from the start of a segment at any time.
 The EVEN directive tells assembler to increment location counter to the next even address if it is not
already at an even address.
 The 8086 can read a word from memory in one bus cycle if the word is at an even address, otherwise it
requires two bus cycle if the word is at an odd address. A series of words can be read much more
quickly if they are at even addresses.
 When EVEN is used in a data segment, the location counter will simply incremented to the next even
address if necessary. When EVEN is used in a code segment, the location counter will be incremented to
the next even address if necessary and a NOP instruction will be inserted in location incremented over.
Examples:
DATA_HERE SEGMENT
SALES_AVERAGE DB 9 DUP (?)
EVEN
RECORDS DW 100 DUP(0)
DATA_HERE ENDS

;Declare array of 9 bytes and location counter points to 0009 after


assembler reads next statement
;Increment location counter to 000Ah
;Array of 100 words starting on even address for quicker read.

ORG: ORIGINATE
 The ORG directive allows to set the location counter to a desired value at any point in the program. For
eg: ORG 2000h tells the assembler to set the location counter to 2000h.

 A $ is 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.
 A $ 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 to increment the value of the location counter by 100
times from its current value. This type of statements might be used in a data segment to leave 100 bytes
of space for future use.
Program Termination:
END: END Program
 This 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 statement after an END directive. A carriage return is
required after the END directive.
 The format is: END Label, to indicate the end of a set of assembler language code. The label appears
only in the termination of the main program in a program structure, all seperately assembled program
modules must conclude with an END directive.
 The label must also appear in the instruction to be executed first, the point at which the program begins.
 The address associated with this label is the address the loader branches to after the program has been
loaded and is ready for execution.
Procedure Declaration:
PROC: PROCEDURE
 This directive is used to identify the start of a procedure. The PROC directive follows a name of the
procedure. After the PROC directive, the term near or far is used to specify the type of procedure.
For eg: SMART_DIVIDE PROC FAR, identifies the start of a procedure named SMART_DIVIDE and
tell the assembler that the procedure is far (in a segment with a different name from the one that
contains the instruction which calls the program).
 The PROC directive is used with the ENDP directive to bracket a procedure.
ENDP: END PROCEDURE
 This directive is used along with the name of the procedure to indicate the end of a procedure to
assembler.
 This directive, together with procedure directive PROC is used to bracket a procedure.
Example:
SQUARE_ROOT PROC

;Start of procedure instruction statements

SQUARE_ROOT ENDP

;End of procedure

EQU: EQUATE
 It is used to give a name to some value or symbol. Each time the assembler finds the given name in the
program, it will replace the name with the value or symbol which is equated with that name.

For eg: the statement TEMP EQU 03h at the start of program, and later in the program we can write the
instruction statement ADD AL, TEMP.
 When it codes this instruction statement, the assembler will code this as if the instruction statement
ADD AL, 03h.
 The advantage of EQU in this manner is that if TEMP is used 28 times in a program, and if we want to
change the value, all we have to do is change the EQU statement and reassemble the program.
 The assembler will automatically put in the new value each time it finds the name TEMP.
Examples:
CONTROL_WORD EQU 1100 1001
MOV AL, CONTROL_WORD

;Replacement
;assignment

DECIMAL_ADJUST EQU DAA


ADD AL, BL
DECIMAL_ADJUST

;Create the name for mnemonic DAA


; Add BCD numbers
; Keep result in BCD format

STRING_START EQU [BX]

;Give name to [BX]

EXTRN:
 It is used to tell the assembler that the names or labels following the directive are in some other
assembly module. For eg: if we want to call a procedure which is in a program module assembled at a
different time from that which contains the CALL instruction, we must tell the assembler that the
procedure is external.
 The assembler will then put the information in the object file so that the linker can connect the two
modules together. For a reference to an external named variable, we must specify the type of the
variable as in the statement EXTRN DIVISOR: WORD.
 For a reference to label, we must specify whether the label is near (in a code segment with the same
name) or far (in a code segment with different name).
 The statement EXTRN SMART_DIVIDE: FAR tells the assembler that SMART_DIVIDE is a label of
type far in another assembly module.
 Names or labels referred to as external in one module must be declared with the PUBLIC directive in the
module in which they are defined.
 EXTRN statements should usually bracketed with SEGMENT_ENDS directive which identify the
segment in which the external name or label will be found.
Example:
PROCEDURE_HERE SEGMENT
EXTRN SMART_DIVIDE: FAR
PROCEDURE_HERE ENDS

;Found in segment PROCEDURE_HERE

PUBLIC:
 Large programs 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.
 For the modules 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. For eg: PUBLIC DIVISOR, DIVIDEND, which makes the two variables DIVISOR and
DIVIDEND available to other assembly modules.
 If an instruction in a module refers to a variable or label in another assembly module, the assembler
must be told that it is external with the EXTRN directive.
NAME:
 The NAME directive is used to give a specific name to each assembly module when programs
consisting of several modules are written.
 The statement NAME PC_BOARD, might be used to name an assembly which contains the instructions
for controlling a printed-circuit- board-making machine.
INCLUDE: INCLUDE SOURCE CODE FROM FILE
 This directive is used to tell the assembler to insert a block of source code from the named file into the
current source module. This shortens the source code.
GLOBAL: DECLARE SYMBOLS AS PUBLIC OR EXTRN
 The GLOBAL directive is used in place of a PUBLIC directive or in place of an EXTRN directive.
 For a name or symbol defined in the current assembly module, the GLOBAL directive is used to make
the symbol available to other modules.
 The statement GLOBAL DIVISOR, makes the variable DIVISOR public so that it can be accessed from
other assembly modules.
 The statement GLOBAL DIVISOR:WORD tells the assembler that DIVISOR is a variable of type word
which is in other assembly module or EXTRN.
GROUP: GROUP-RELATED SEGMENTS
 The GROUP directive is used to tell the assembler to group the logical segments named after the
directive into one logical group segment.
 This allows the contents of all the segments to be accessed from the same group segment base.
 The assembler sends a message to the linker and/or locator telling it to link the segments so that the
segments are physically in the same 64-Kbyte segment.
Eg: SMALL_SYSTEM GROUP CODE, DATA, STACK_SEG
ASSUME CS: SMALL_SYSTEM, DS: SMALL_SYSTEM, SS: SMALL_SYSTEM.

ALIGN
 The ALIGN directive is used to makes sure that the memory array are stored on word boundaries. An
ALIGN 2 places data on word boundaries, ALIGN 4 places the data on double word boundaries.
Example: data segment
num1 db 12h
;the $=0001, if num1 is assigned to 0000
align 2
;the $=0002, the next word address
num2 dd 12346723h, 45h
data ends
extra
data1
align
data2
extra

segment word
db 34h, 56h
4
dw 3456h
ends

;the $=0002, if num1 is assigned to 0000


;the $=0004, the next double-word address

Questions Appeared in Exam:


1. What are the pseudo codes? Explain the following directives with examples:
ENDP
EXTRN
GLOBAL
PROC

(June 12, 07 Marks)

2. What are assembler directives? Explain the significance of the following assembler directives with
suitable examples. i) LENGTH ii) TYPE iii) DB iv) EQU
(June 2011, 8 Marks)
3. Define the assembler directive. Explain the following directives: i) DW ii) PTR
iii) EVEN
iv) PROC
(Dec 2010, 6 Marks)
4. What do you mean by assembler directives? Explain the following assembler directives:
i) ALIGN 16 ii) PROC..ENDP iii) ASSUME
iv) EXTRN..PUBLIC
(June 2010, 5 Marks)
5. What are assembler directives? Explain the action performed by the following assembler directives:
i) Price db (?) ii) PAI Equ 40h
iii) ASSUME iv) EXTRN
(Dec 2009, 6 Marks)

You might also like