Assembler Directives


For more Notes and Material Please go to "MPMC" Button at the Menu

ASSEMBLER DIRECTIVES

Assembly language programs are composed of two types of statements.
1) The Instructions which can be translated to machine code by the assembler.
2) The directives that directs the assembler during the assembly process for which no machine code is generated. 

Assembler Directives are instructions entered into the source code along with the assembly language. Pseudo instructions (Assembler Directives) do not get translated into object code, but are used as special instructions to the assembler to perform some special functions. The directives control the generation of machine code and organization of the program.

Ex:   SEGMENT, ENDS, ASSUME, DB, DW, DD, DQ, DT, END, PROC, ENDP, MACRO, ENDM,  EQU, LENGTH, SIZE, OFFSET, EVEN, ORG, LABEL, INCLUDE

The assembler directives are classified into the following categories based on the functions performed by them. They are
a)Data definition and storage allocation directives
b)Program organization directives
c)Alignment Directives
d)Program end Directive
e)Value returning attribute directives
f)Procedure definition directives
g)Macro definition directives
h)Data control directives
i)Header file inclusion directives


a)Data definition and storage allocation directives
Data definition directives are used to define the program variables and allocate a specified amount of memory to them. They are of type BYTE, WORD, Double Word, Quad Word and Ten Byte and their size in bytes are 1,2,4,8 and 10 respectively. The data definition directives are DB, DW, DD, DQ, DT.

DB – [define byte]
The DB directive is used to define a byte–type variable or to set aside one or more storage locations of type byte in memory. It can be used to define single or multiple byte variables.

Ex 1)  n DB 42H
tells the assembler to reserve 1 byte of memory for a variable named n and to put the value 42H in the that memory location, when the program is loaded into memory to be run.

 Ex 2)  num DB ?
The above statement informs the assembler to reserve one byte of memory for a variable named num. use of ? in data definition informs the assembler that the value is unknown and hence the variable num is not initialized.

Ex 3) grade DB ‘A’ or “A”
The above statement informs the assembler to reserve one byte of memory for a variable named grade and initializes with ASCII equivalent of a letter ‘A’. The ASCII character should be enclosed within single or double quotes.

Ex 4) num DB 25,50,43,76,34
The above statement reserves 5 bytes of consecutive memory locations for the variable num and initializes them with 5 values.

Ex 5) info DB ‘welcome’
The above statement defines a variable info and reserves 7 bytes of consecutive memory locations and initializes with the string ‘welcome’ during execution of program.

Ex 6) sname db 10 dup(‘-‘)
This statement defines a variable sname, reserves 10 bytes of consecutive memory locations and initializes with ASCII equivalent of character ‘-‘.

Ex 7) sum db 25 dup(?)
This statement defines a variable and reserves 25 bytes of consecutive memory locations and are not initialized.

DW – [define word]
The DW directive is used to declare a variable of type word, or to reserve storage locations of type word in memory.

Ex:-      MULTIPLIER DW 347AH
             num dw 023ah
             sum dw ?
             items dw 03abh, 0abc4h, 0bbah, 0543ch, 04a4bh
             res dw 20 dup(0)

DD – [define double word]
            The DD directive is used to declare a variable of type double word.

DQDefine Quad word :
The directive DQ is used to define a quad word ( 8 bytes) type variable.

DTDefine Ten bytes :
The directive DT is used to define a Ten bytes type variable.










b)Program organization directives
SEGMENT : The segment directive is used to indicate the start of                                                                                                                                                                                                                                                                                                                                                                        a logical segment. Preceding the segment directive the name you want to give the segment.

                                    CODE SEGMENT
                                       -------
                                       -------
                                       -------
                                    CODE ENDS
            Indicates to the assembler the start of a logical segment called code. The SEGMENT and ENDS directives are used to identify a group of data items or a group of instructions that you want to be put together in a particular segment. A group of data statements or a group of instruction statements contained between segment and endsdirectives is called a logical segment. When you setup a logical segment, you give it a name of your choosing.

ENDS :- [end segment ] : This directive is used with the name of a segment to indicate the end of the logical segment. ENDS is used with the segment directive to ‘bracket’ a logical segment containing instructions or data.

              data segment
                   n1 db 05h
                   n2 db 04h
                   sum db ?
              data ends

ASSUME :-
The assume directive is used to tell the assembler the name of the logical segment it should use for a specified segment. The 8086 program may have several logical segments. At any time the 8086 works directly with only four physical segments; a code segment, a data segment, a stack segment and an extra segment.

The statement
                        ASSUME CS : Code
tells the assembler that the instructions for a program are in a logical segment named code.
code segment
assume cs:code, ds:data
mov ax,data
mov ds,ax
-----
-----
-----
code ends
data segment
-----
-----
data ends
                                     end
c)Alignment Directives

EVEN :
Align as even memory address. The directive EVEN is used to inform the assembler to increment the location counter to the next even memory address if it is not pointing to even memory location already.

                         data segment
                                     sum db 10
                                     even
                                     items dw 100 dup(?)
                         data ends
The data array items starts at the even memory location which makes access to its elements efficient; The directive even is used for the alignment of the data array item.

ORG : Originate :
The directive 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.
ORG 100
The above statement informs the assembler to initialize the location counter to 100.

d)Program end Directive

END – END PROGRAM
The END directive is put after the last statement of a program to tell the assembler that this is the end of the program module. A carriage return is required after the END directive. The last statement of every program must be an end directive.

code segment
------
------
------
ends
data segment
------
------
data ends
end









e)Value returning attribute directives
LENGTH:
The directive length informs the assembler about the number of elements in a data item such as an array. If an array is defined with DB then it returns the number of bytes allocated to the variable. If an array is defined with DW then it returns the number of words allocated to the array variable.

                  Ex1)  MOV AX, LENGTH ITEMS

                            data segment
                                  items db 08h,78h,56h,78h,98h
                             data ends

               In the above example the number of elements assigned as array are 5. So five is the length of items which will be stored in AX.

                 Ex2)  MOV AX, LENGTH ITEMS

                            data segment
                                  items dw 0048h,0a78h,0b56h,0c78h,0d98h
                             data ends

               In the above example the number of elements assigned as array are 5. So five is the length of items which will be stored in AX.


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.

         Ex1)          MOV AX,SIZE ITEMS

                           data segment
                                 items DB 08h,78h,56h,78h,98h
                           data ends

In the above example the number of bytes occupied by the array items are 5. So 5 is the size of items which will be stored in AX.

            Ex2)       MOV AX, SIZE  num

                           data segment
                                   num DW 0008h,0078h,0a56h,0b78h,0c98h
                           data ends

In the above example the number of bytes occupied by the array elements 10. So 10 is the size of num which will be stored in AX.



OFFSET :
The directive OFFSET informs the assembler to determine the displacement of the specified variable with respect to the base of data segment.

                                       Offset variable_ name

Ex:
MOV DX, OFFSET MSG

Data segment
     -----
     -----
    MSG DB ‘nalanda’
     ----
Data ends

f)Procedure definition directives

PROC:
The PROC directive is used to identify the start of a procedure. The PROCdirective follows a name you give the procedure. After the PROC directive the term NEAR or FAR is used to specify the type of the procedure.
                         
                  Factorial  PROC  Near
                                 ---------
                                 ---------
                                   RET
                  Factorial  ENDP



ENDP :-         [end procedure ]
This directive is used along with the name of the procedure to indicate the end of a procedure to the assembler.

            SQUARE_ROOT    PROC    NEAR
                       ----------
                       ----------
                        SQUARE_ROOT    ENDP











g)Macro definition directives

MACRO:
A macro is a group of instructions we bracket and give a name to at the start of our program. Each time we call the macro in our program, the assembler insert the defined group of instructions in place of the call.

The MACRO directive is used to identify the start of a macro. The Macro directive follows a name you give the macro.

            MACRONAME         MACRO                                 ; START OF MACRO
                                                --------
                                                --------
                                                --------
                                             ENDM                          ; END OF MACRO

ENDM :-[end macro ]
This directive is used along with the name of the macro to indicate the end of a macro to the assembler.

PUSHALL MACRO
                                                 PUSHF
                                                 PUSH AX
                                                 PUSH BX
                                                 PUSH CX
                                                 PUSH DX
                                           ENDM

EQU :-[equate]
EQU 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 equated with that name.

Ex1) SUM EQU 10
The above statement declares the symbol SUM with value 10. The assembler will replace every occurrence of the symbol in the program by its value.

Ex2)MOVEMENT EQU MOV
Every occurrence of MOVEMENT will be replaced by MOV.

            Ex3)DECIMAL_ADJUST EQU DAA
Every occurrence of DECIMAL_ADJUST will be replaced by DAA.






h)Data control directives

LABEL:
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 you want associated with that name.

STK SEGMENT
                                    S DW 100 DUP(0)
                                    S_TOP LABEL WORD
STK ENDS





i)Header file inclusion directives

INCLUDE:
The header file inclusion directive is used to define an include file header. The header file inclusion directive is INCLUDE.

The directive INCLUDE informs the assembler to include the statements defined in the include file. The name of the include file follows the statement INCLUDE. It is useful to place all the data and frequently used macros into a file known as include file or header file.

INCLUDE <file path specification>


Assembler Directives Assembler Directives Reviewed by Suresh Bojja on 10/28/2015 09:45:00 AM Rating: 5

Subjects & Labs

AC (6) ADS (2) ATMEL (6) AWP (5) C- Language (3) CAO (5) CDAC (1) Cloud Computing (1) CMC (6) CN (10) CONSERVATION & MANAGEMENT Complete Notes (1) Control Sys (2) CVSM (2) Data Analytics (1) DC (5) Digital System Design (3) DIP (17) DLD (10) DLD Lab Manual (1) DS (4) DSD&DICA (9) DSP (6) EDC (45) Electonic Components (2) Embedded (7) EMWTL (6) ENERGY AUDIT (1) Engg. Mechanics (2) Engg.Chemistry (11) Faculty Files (6) Faculty Labs (1) Fuzzy (3) II MID (1) Job Exam Papers (4) LICA (1) Linux OS (1) Materials (1) MATLAB (27) MCU IDE 8051 (1) MEMS (4) MPMC (57) MPMC LAB Manual (3) MTECH (6) MWE (2) OC (1) PDC (9) Projects (8) RADAR (1) Research (9) RVSP (3) SC (1) Softwares (1) SS (2) STLD (19) TCSS (1) TSSN (1) TV Engg (1) VHDL (4) VLSI (8) VLSI Lab manual (1) WCN (1) Wireless Commu. (1) WSN (2)
Theme images by sebastian-julian. Powered by Blogger.