Assembler Directives
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 1
The assembler directives control organization of
the program and provide necessary information
to the assembler to understand assembly
language programs to generate machine codes.
An assembler supports directives to define data,
to organize segments to control procedures, to
define macros etc.
An assembly language program consists of two
types of statements:
Instructions and Directives.
The instructions are translated to machine codes by the
assembler whereas the directives are not translated to
machine codes.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 2
SEGMENT AND ENDS DIRECTIVES
To put a group of data items or group of
instructions in a particular segment
A group of data segments or a group of instruction
statements contained between SEGMENT and
ENDS directives is called a logical segment
SEGMENT
Beginning of a memory segment.
It is used to show the beginning of a memory segment
with a specific name.
ENDS
End of segment
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 3
This directive defines the termination of a
particular memory segment as it is specified
by its name.
The statements within the segment are
nothing but the program code.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 4
ASSUME
The ASSUME directives is used to inform the assembler
the names of the logical segments, which are to be
assigned to the different segments used in an assembly
language program.
In an assembly language program, each segment is given
a name by the programmer.
For example, the code segment may be given the name
CODE or CODE_SEG or MY_CODE, etc.
The data segment may be given the name DATA,
MY_DATA, DATA_SEG, etc.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 5
ASSUME CS : CODE
The above directive tells the assembler that the name
of the code segment is CODE.
This name is a user defined segment name. The
segment named CODE contains the machine codes of
the instructions.
The code segment register (CS register) is to be
loaded with the starting address of the code segment,
given by the operating system for the label CODE in
the assembly language program.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 6
ASSUME DS: DATA
The above directive informs the assembler that the
name of the data segment is DATA.
This is a user defined segment name. It contains data
of the program which is being executed.
The DS register (data segment register) is to be
loaded with the starting address of the data segment,
given by the operating system for the label DATA in
the program.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 7
ASSUME ES: EXTRA
The above directive tells the assembler that the name
of the extra segment is EXTRA which is a user defined
segment name.
In Intel 8086 microprocessor, string instructions may
use DI register to point the destination memory
address for the data.
The EXTRA segment is used to hold the addresses
pointed by DI.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 8
ASSUME SS: STACK
The above directive tells the assembler that the name
of the stack segment used by the programmer is
STACK.
This is a user defined segment name.
It stores addresses and data of the subroutines, saves
the contents a specified register or memory locations
after PUSH instruction, etc.
The stack segment register SS holds the starting
address of the stack segment allotted by the operating
system.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 9
Naming Data and Addresses – EQU, DB,
DW and DD Directives
EQU(Equate):
The directive EQU is used to give a name to certain
value or symbol.
If any value or symbol is used many times in an
assembly language program, a name is given to the
value or symbol to make programming easier and
simpler.
Each time the assembler finds the given name in the
program, it replaces the name with the value or
symbol which has already been equated with that
name.
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 10
The general format for the EQU directive is:
Value Name EQU Value
Example:
ROOM_TEMP EQU 02H
The above directive tells assembler to replace
ROOM_TEMP by 02H.
If EQU directive is written in the beginning of the
program and later on MOV AL, ROOM_TEMP is
written in the program, the assembler will treat
this instruction as if it were MOV AL, 02H while
giving its machine codes.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 11
Advantage of using EQU directive at start of
programs is that if we want to modify the value
of ROOM_TEMP it is enough if we change only
at one place of declaration; it will reflect at all
occurrences of NUM in the pgm
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 12
DB, DW and DD Directives
DB: Define byte
DW: Define word
DD: Define doubleword
DT: Define TenBytes
DQ: Define Quadword
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 13
DB (Define Byte) :
The directive DB a byte type variable.
DB takes 1 byte of memory location.
In a given directive statement, there may be
single initial value or multiple initial values of the
defined variable.
If there is one initial value, one byte of memory
space is reserved for each value.
The general format is:
Name of Variable DB Initial value or Values.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 14
Examples:
A DB 12H
Array DB 27H, 48H, 32H, 69H
C DB ?
? Signifies we don’t care about the initial value
This directive informs assembler to reserve one byte of
memory space for the variable named A and initialize
with value 12H.
This directive informs assembler to reserve Four bytes of
consecutive memory space for the variable named
Array.
The memory locations are to be initialized with the
values 27H, 48H, 32H and 69H.
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 15
MESSAGE DB ‘THANK YOU’
This directive informs the assembler to reserve
the number of bytes of memory space equal to
the number of characters in the string named
MESSAGE, and initialize the memory locations
with ASCII codes of the these characters.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 16
DW (Define Word)
The directive DW defines a word -type variable. The
defined variable may have one or more initial values in
the directive statement.
If there is one value, two-bytes of memory space are
reserved.
If there are multiple values, two bytes of memory space
are reserved for each value.
The general formula is:
Name of variable DW Initial Value or Values.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 17
Examples:
SUM DW 3596.
This directive informs the assembler to
reserve two bytes (one word) of consecutive
memory locations for the variable named SUM
and initialize it with the value 3596.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 18
DD (Define Double Word)
This directive DD defines a double word-type variable.
The defined variable may have one or more values in the
statement. If there is only one value, four bytes of
consecutive memory locations are reserved. If there are
multiple values, four bytes of memory locations are
reserved for each value.
The general format is:
Name of Variable DD Initial value or values
Example:
NUMBER DD 23958634
The above directive informs assembler to reserve four bytes of
memory locations for the variable named NUMBER and initialize
with the number 23958634.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 19
DUP-duplicate
The DUP directive can be used to initialize
several locations and to assign values to these
locations
Example:
TABLE DB 10 DUP(0)
Reserve an array of 10 bytes of memory and intialize all 10
Bytes with 0.
Array is named TABLE
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 20
Multiply.asm
Data segment
a dw 204Ah
b dw 3B2Ah
c dw 2 dup (0)
Data ends
Code segment
assume cs:code, ds:data
Start: mov ax, data
mov ds, ax
mov ax, a
mul b
mov c, ax
mov c+2, dx
mov ah, 4ch
int 21h
Code ends
End start
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 21
Types of Numbers in Data
Statements
BINARY:
Example:
MAX DB 01111001B
If you want to put in a negative binary no, write
the no in its 2’s complement sign-and-magnitude
form
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 22
DECIMAL:
Example:
MAX DB 49
If you specify a –ve no in a data declaration
statement, the assembler will convert it to 2’s
complement form
Example:
MIN DB -20
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 23
HEXADECIMAL:
A hexadecimal number is indicated by an H after the
hexadecimal digits
Example:
NUM1 DB 35H
Note:
A zero must be placed in front of a hex number that
starts with a letter
Example:
NUM2 DB 0F4H
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 24
BCD: (Binary Coded Decimal)
BCD uses a 4-bit binary code to individually
represent each decimal digit in a number
Uses the first 10 numbers of a standard binary
code for the BCD numbers 0 through 9
The hex codes A through F are invalid BCD
codes
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 25
To convert a decimal number to its BCD
equivalent, just represent each decimal digit by
its 4-bit binary equivalent
Decimal 5 2 9
BCD 0101 0010 1001
To convert a BCD number to its decimal
equivalent, reverse the process
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 26
BCD Addition:
1)
BCD
35 0011 0101
+23 + 0010 0011
58 0101 1000
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 27
2)
BCD
7 0111
+5 + 0101
12 1100 Incorrect BCD
+ 0110 Add 6
0001 0010 correct BCD 12
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 28
3)
BCD
9 1001
+8 + 1000
17 0001 0001 Incorrect BCD
0000 0110 Add 6
0001 0111 correct BCD 17
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 29
Note:
The reason for the correction factor 6 is that in
BCD we want a carry into the next digit after
1001 or 9, but in binary a carryout of the lower 4
bits doesn’t occur until after 1111 or 15.
The difference between the two carry points is to
produce the desired carry if the result of an
addition in any BCD is more than 1001
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 30
If you want the assembler to initialize a variable
with the value 37 BCD, we have to put an H after
the number
Example:
SECONDS DB 59H
This will initialize SECONDS with 0101 1001,
which is BCD representation of 59
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 31
ASCII:
We can declare a data structure (array)
containing a sequence of ASCII codes by
enclosing the letters or numbers after a DB in
single quotation marks
Example:
BOY1 DB ‘RAM’
Tells the assembler to declare a data item
named BOY1 that has 3 memory locations
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 32
It also tells the assembler to put the ASCII code
for R in the first memory location, ASCII code for
A in second memory location and ASCII code for
M in 3rd memory location
The assembler will automatically determine the
ASCII codes for the letters or numbers within the
quotes
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 33
Accessing Named Data with program
instructions
The assembler will automatically calculate the
displacement of the named data item from the
start of the segment and insert this value as part
of the binary code for the instruction
Example:
a dw 1234h
b db 23h
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 34
Naming Addresses - Labels
One type of name used to represent addresses
is called a label
Labels are written in the label field of an
instruction statement or a directive statement
One major use of labels is to represent the
destination for jump and call instructions
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 35
Example:
NEXT : IN AL, 05H
.
.
.
JMP NEXT
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 36
The END(End of Program) Directive
The directive END informs assembler the end of
a program module.
This is used after the last statement of the
program module.
This assembler ignores statement(s) after an
END directive.
Its general format is:
END label
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 37
ENDP – End Procedure
The directive ENDP informs assembler the end of a procedure. In
assembly language programming, subroutines are called
procedures.
A procedure may be an independent program module to give certain
result or the required value to the calling program. A procedure is
given a name i.e. a label.
The label is used as prefix with directive ENDP.
This directive is used together with PROC directive to enclose the
procedure.
To specify the type of the procedure the term FAR or NEAR is used
after the PROC directive.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 38
The type FAR indicates that the procedure to be
called is in some other segment of memory.
Type NEAR indicates that the procedure is in the
same segment of memory.
If type is not specified, the assembler assumes it
NEAR.
The general format for ENDP directive is:
Procedure Name ENDP
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 39
Example:
Delay PROC FAR ; Start of Procedure
: ; Procedure instructions
Delay ENDP ; End of Procedure
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 40
ENDM (End Macro)
The directive ENDM is used to inform assembler that this
is the end of a macro. The directive ENDM is used with
the directive MACRO to enclose macro instructions.
Example:
COMPLIMENT MACRO ; Start of macro
: ; Macro instructions
ENDM ; End of Macro
COMPLIMENT is the name of a macro. The name is
given before the directive MCRO which tells the
assembler the beginning of a macro.
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 41
ENDS ( End of Segment)
The ENDS directive informs assembler that this is the
end of the segment. The name of the segment is given
using ASSUME directive.
The name of the segment is used as the prefix of the
ENDS directive.
Its general format is:
Segment Name ENDS
Example:
CODE_SEG SEGMENT ; Start of code segment
– ; instructions
CODE_SEG ENDS ; End of segment
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 42
Initializing Segment Registers
When the instructions are executed, the
displacements in the instructions will be added
to the segment base addresses represented by
the 16-bit numbers in the segment registers to
produce the actual physical addresses
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 43
The segment registers other than the code
segment register must be initialized by program
instructions before they can be used to access
data
If data segment represented by DATA then the
instructions for this are:
MOV AX, DATA
MOV DS, AX
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 44
If we use the stack segment and the extra
segment in a program, the stack segment
register and the extra segment register must be
initialized by program instructions in the same
way
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 45
Writing and Using Assembler Macros
Macro is a set of instruction and the programmer can
use it anywhere in the program by using its name.
It is mainly used to achieve modular programming.
So same set of instructions can be used multiple times
when ever required by the help of macro.
Wherever macro’s identifier is used, it is replaced by the
actual defined instructions during compilation thereby no
calling and return occurs.
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 46
Defining and Calling a Macro Without
Parameters
Macro Definition:
MACRO-NAME MACRO
; MACRO DEFINITION
;
ENDM
Invoking a Macro:
MACRO-NAME
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 47
Defining Macro :
MACRO-name MACRO [ARGUMENT 1,……….ARGUMENT N]
-----
MACRO CODING GOES HERE
ENDM
Ex:
DISPLAY MACRO 12,13
---------------------
MACRO STATEMENTS
-----------------------
ENDM
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 48
Example
Macro Definition:
CLRSCR MACRO
MOV AH, 00H
MOV AL, 02H
INT 10H
ENDM
Macro Invocation:
CLRSCR
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 49
Passing Parameters to Macros
Example:
Macro definition:
SETCURSOR MACRO row, col
MOV DL, col ;Column
MOV DH, row ;Row
MOV BH, 00H
MOV AH, 02H
INT 10H
ENDM
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 50
If the macro invocation statement is
SETCURSOR 12, 40 then the macro will be
replaced by:
MOV DL, 40
MOV DH, 12
MOV BH, 00H
MOV AH, 02H
INT 10H
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 51
INCLUDE – Include source code from file
Is used to tell the assembler to insert a block of
source code from the named file into the current
source module
INCLUDE f1.mac
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 52
OFFSET
Tells the assembler to determine the offset or
displacement of a named data item or procedure
from the start of the segment which contains it
Example:
MOV SI, OFFSET PRICES
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 53
PROC – Procedure
Is used to identify the start of a procedure
The PROC directive follows a name you give
the procedure
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 54
FAR
This directive is a type specifier that is used by
the assembler to declare intersegment call (i.e.,
call from different segment).
NEAR
This is used for intrasegment call i.e., a call
within the same segment.
SEGMENT
Is used to indicate the start of a logical
segment
Preceding the SEGMENT directive is the
name you want to give the segment
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 56
The statement, CODE SEGMENT for example,
indicates to the assembler the start of a logical
segment called CODE
The SEGMENT and ENDS directives are used
to “bracket” a logical segment containing code or
data
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 57
Assembly Language Program
Development Tools
We will probably want to use some type of
program development tools to make our work
easier
Most of these tools are programs which we run
to perform some function on the program we
are writing
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 58
Editor
Is a program which allows us to create a file
containing the assembly language statements
for our program
As we type in our program, the editor stores the
ASCII codes for the letters and numbers in
successive RAM locations
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 59
When we have typed in all our program, we then
save the file on hard disk
If we are going to use MASM assembler, we
should give the source file name the
extension .asm
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 60
Assembler
Translates the assembly language mnemonics
for instructions to the corresponding binary
codes
On the first pass through the source program,
the assembler determines the displacement of
named data items, the offset of labels etc and
puts this information in a symbol table
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 61
On the second pass through the source
program, the assembler produces the binary
code for each instruction and inserts the offsets,
etc that is calculated during first pass
The assembler generates two files on hard disk
The .obj file and .lst file
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 62
The .obj file i.e. object file contains the binary
codes for the instructions and information about
the addresses of the instructions
The .lst i.e. assembler list file consists of
assembly language statements, the binary
codes for each instruction and the offset for
each instruction
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 63
The assembler listing will also indicate any
typing or syntax errors we made in our source
program
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 64
Linker
A linker is a program used to join several object
files into one large object file
When writing large programs, it is usually much
efficient to divide the large program into smaller
modules
Each module can be individually written, tested
and debugged
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 65
When all the modules work, their object modules
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 file which
contains the address information about the
linked files
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 66
The MASM assemblers produce link files with
the .EXE extension
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 67
Locator
A locator is a program used to assign the
specific addresses of where the segments of
object code are to be loaded into memory
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 68
Debugger
A debugger is a program which allows you to
load our object code program into system
memory, execute the program and troubleshoot
or debug it
Allows us to look at the contents of registers and
memory locations after our program runs
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 69
A debugger also allows us to set a breakpoint at
any point in our program
If we insert a breakpoint, the debugger will run
the program up to the instruction where we put
the breakpoint and then stop execution
The debugger commands help us to quickly find
the source of a problem in our program
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 70
Emulator
Is a mixture of hardware and software
Is usually used to test and debug the hardware
and software of an external system, such as the
prototype of a microprocessor-based instrument
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 71
Like a debugger, an emulator allows us to load
and run programs, examine and change the
contents of registers, examine and change the
contents of memory locations and insert
breakpoints in the program
The emulator also takes a snapshot of the
contents of the registers, activity on the address
and data bus, and the state of the flags as each
instruction executes
06/29/24 ROSHAN FERNANDES, DEPT OF CSE 72
Thank You…
Roshan Fernandes, Dept of CSE,
06/29/24 NMAMIT, Nitte 73