Documente Academic
Documente Profesional
Documente Cultură
Introduction
Assembly language programs are composed of two types of statements.
1. The instructions which are translated to machine code by the assembler.
2. The directives that direct the assembler during assembly process for
which no machine code is generated.
The directive is a statement which gives direction to the assembler. Directives
indicate how an operand or a program is to be processed by the assembler.
The assembler for the microprocessor can be used in two ways:
1. With models that are unique to a particular assembler, and
2. With full segment definitions that allow complete control over the assembly
process and are universal to all assemblers.
Data definition directives are used to define the program variables and
allocate specific amount of memory to them. The data definition directives are DB,
DW, DD, DQ and DT.
DB: Define Byte
DB is used to define a byte type variable. It can be used to define single or multiple
byte type variables. The range of values can be stored in a byte is 0 to 255 for
unsigned numbers and 128 to 127 for signed numbers.
Examples: total db 0
total db ?
table db 83, 100, 200, 55, 255
list db ?, ?, ?, ?, ?
msg db “hello world$”
DW: Define Word
DW is used to define a word type variable. It can be used to define single or
multiple word type variables. The range of values can be stored in a word is 0 to
65535 for unsigned numbers and 32768to 32767 for signed numbers.
Examples: total dw 0
total dw ?
table dw 83, 100, 200, 55, 255
DD: Define Double Word
DD is used to define a double word (4 bytes) type variable. It can be used to define
single or multiple double word type variables. The range of values can be stored in
a byte is 0 to 2321 for unsigned numbers and 2321 1 to 2321 1 for signed numbers.
Examples: total dd 0
total dd ?
DQ: Define Quad Word
DQ is used to define a quadword (8bytes) type variable. It can be used to define
single or multiple quadword type variables. The range of values can be stored in a
byte is 0 to 264 1 for unsigned numbers and 2641 1 to 2641 1 for signed numbers.
Examples: total dq 0
total dq ?
DT: Define Ten Bytes
DT is used to define a ten byte type variable. It can be used to define single or
multiple 10byte type variables. The range of values can be stored in a byte is 0 to
280 1 for unsigned numbers and 2801 1 to 2801 1 for signed numbers.
Examples: total dt 0
total dt ?
? : Memory is reserved for use in the future by using a ? as an operand for a DB,
DW or DT directive. When a ? is used in place of a numeric or ASCII value, the
assembler sets aside a location and does not initialize it to any specific value.
(Actually, the assembler usually store zero into locations specified with a ?).
DUP: The DUP (duplicate) directive creates an array. A 10 DUP (?) reserves 10 –
locations of memory, but stores specific value in any of the 10 locations. If a number
appears within the ( ) part of the DUP statement, the assembler initializes the
reserved section of memory with the data indicated. For example, the DATA1 DB
10 DUP (2) instruction reserves 10 bytes of memory for array DATA1 and
initializes each location with a 02H.
EQU
The equate directive(EQU) equates a numeric, ASCII, or label to another label.
Equates make a program clearer and simply debugging.
Ex: TEN EQU 10
NINE EQU 9
ORG: Originate
Using the ORG directive, one can force the assembler, so that origin of data or the
code must be assigned to an absolute offset address with the ORG statement.
Consider the following example
Data segment
ORG 100H
Data1 dw ?
Data ends
The above example illustrates how the ORG (origin) statement changes the starting
offset address of the data in the data segment to location 100H.
ASSUME:
The ASSUME statement tells the assembler what names have been chosen for the
code, data, extra and stack segments. Without the ASSUME statement, the
assembler assumes nothing and automatically uses a segment override prefix on all
instructions that address memory data.
The directives ASSUME informs the assembler the name of the logical segment
that should be used for specified segment. When a program is loaded, the processor
segment registers should point to the respective logical segments.
General Form: Assume segment register: segment name, segment register: segment
name …
Example: Assume CS: MYCODE, DS: MYDATA
SEGMENT and ENDS directive
This directive is used to define the beginning of a logical segment. The directives
SEGMENT and ENDS must enclose the segment data, code or stack in the
program. SEGMENT indicates the starting of the segment whereas ENDS is used
to indicate the end of the segments.
Example: MYDATA SEGMENT
; program data definition here
MYDATA ENDS
OFFSET: This directive informs the assembler to determine the displacement of
the specified variable with respect to the base of the segment. This is usually used
to store the offset of a variable into a specified register. With this offset value a
variable can be referenced using indexed addressing modes.
Example:
Data Segment
Msg db ‘hello world$’
A dw 50 dup (?)
Data ends
MOV DX, offset msg ; DX=offset of the variable MSG
MOV SI, offset A ; SI = offset of the array A
MOV AX, [SI] ; AX =element of an array A pointed by SI.
PROC and ENDP
The PROC and ENDP directives indicate the start and end of a procedure
(subroutine). These directives force structure because the procedure is clearly
defined. Both the PROC and ENDP directives require a label to indicate the name
of the procedure. The PROC directive, which indicates the start of a procedure,
must also be followed with a NEAR or FAR. A NEAR procedure is one that resides
in the same code segment as the program. A FAR procedure may reside at any
location in the memory system. Often the call NEAR procedure is considered to be
local, and the call FAR procedure is considered to be global. The term global
denotes a procedure that can be used by any program, while local defines a
procedure that is only used by the current program. Any labels that are defined
within the procedure block are also defined as either local (NEAR) or global (FAR).
Ex: ADDEM PROC FAR
ADD BX, CX
RET
ADDEM ENDP
If version 6.x of the Microsoft MASM assembler program is available, the PROC
directive specifies and automatically saves any registers used within the procedure.
The USES statement indicates which registers are used by the procedure, so that
the assembler can automatically save them before your procedure begins and
restore them before the procedure ends with the RET instruction. For example, the
ADDS PROC USES AX BX statement automatically pushes AX, BX on the stack
before procedure begins and pops them from the stack before the RET instruction
executes at the end of the procedure.
Ex: ADDS PROC USES BX CX DX
ADD BX,AX
ADD CX,BX
ADD DX,CX
MOV AX,DX
RET
ADDS ENDP
Macro definition directives
The directive MACRO indicates the beginning of a macro. This directive follows the
name of the macro and macro arguments if any. The directives MACRO and ENDM
must enclose the definitions or small part of the code which have to be substituted
at the invocation of a macro.
Syntax: Macroname MACRO [argument1, argument2,….,argumentn]
Example:
Printstring MACRO msg
mov ah,09
mov dx, offset msg
int 21h
ENDM
The macro function call to PrintString macro in a program will be substituted by its
body in the program. Macro gets expanded if a call is made to it. The distinction
between macros and procedures is that, a call to a macro will be replaced by its
body during the assembly time where as a call to a procedure will be an explicit
transfer of control to the called procedure during runtime.
******