Pages

Tuesday, May 3, 2011

assembly language

List and Explain Assembler directives
The following directives are commonly used in the assembly language programming practice using Micro Soft Micro Assembler or Turbo Assembler.
DB: Define Byte The DB directive is used to reserve byte or bytes of memory location in the available memory. While preparing the EXE file, this directive directs the assembler to allocate the specified number of memory bytes to the said data type that many be a constant, variable, string etc.,
The following examples show how the DB directive is used for different purposes.
Example:
RANKS DB 01H, 02H, 03H, 04H.
This statement directs the assembler to reserve four memory locations for a list named RANKS and initialize them with the above specified values.
MESSAGE DB ‘good morning’
This makes the assembler reserves number of bytes of memory equal to the number of characters in the string named MESSAGE and initialize those locations by the ASCII equivalent of these characters
VALUE DB 50H
This statement directs the assembler to reserve 50H memory bytes and leave them uninitialized for the variable named VALUE.
DW: Define word
The DW directive directs the same purpose as the DB directive, but it now make the assembler reserve the number of memory words(16 bit) instead of bytes.
Example :
WORDS DW 1234H, 4567H, 78ABH, 645CH
The DW makes the assembler reserve four words in memory(8 bytes), and initialize the words with the specified values in the statements. The lower bytes are stored in the lower memory addresses, while the upper bytes and stored at the higher addresses, during initialization. Another option of the DW directive is explained with the DUP operator.
WDATA DW 5 DUP (6666H)
This statement reserves five words, that is 10-bytes of memory for a word label WDATA and initializes all word locations with 6666H.
DQ: Define Quadword : DQ directive is used to direct the assembler to reserve 4 words (8 bytes) of memory for the specified values.
DT: Define ten bytes : The DT directive directs the assembler to define the specified variable requiring 10-bytes for its storage and initialize the 10-bytes with the specified values.
ASSUME: Assume logical segment Name : Assume directive is used to inform the assembler, the names of the logical segment to be assumed for different segments which is used in the program. In the assembly language program, each segment is given a name. For example, the code segment may be given the name DATA etc. The statement ASSUME CS: CODE directs the assembler that the machine code are available in a segment named CODE, and thus the CS register is to be loaded with the address(segment) allotted by the operating system for the label CODE, while loading. The ASSUME statement is must to start each assembly language program, without a message. ‘CODE/DATA EMITTED WITHOUT SEGMENT’ may be issued by an assembler.
END: End of program : The END directive marks the end of an assembly language program. When the assembler comes across END directive, it ignores the source lines available later on.
ENDP: END of procedure: The subroutines are called procedures in assembly language programming. They may be independent program modules which return the particular result or values to the calling programs. The ENDP directive is used to end of a procedure. A procedure is usually assigned as a name that is label.
To mark the end of a particular procedure, the name of the procedure, that is label may appear as a prefix with the directive ENDP. The statements appearing in the same module but after the ENDP directive are neglected from that procedure. The following illustration explains the use of ENDP.
PROCEDURE STAR
.
.
.
STAR ENDP
ENDS: End of Segment
The ENDS directive marks the end of a logical segment. The logical segments are assigned with the names using the ASSUME directive. The names appear with the ENDS directive as prefixes to mark the end of those particular segments. Whatever are the contents of the segment they should appear in the program before ENDS. Any statement appearing after ENDS will be neglected from the segment. The following structure explains the above mention detail more clearly.
DATA SEGMENT
..
..
DATA ENDS
ASSUME CS: CODE, DS: DATA
..
..
CODE ENDS
END
EVEN: Align on Even Memory Address
The assembler, while starting the assembling procedure of any program, initializes a location counter and goes on updating it, as the assembly proceeds. It goes on assigning the available addresses, that is the contents of the location counter, sequentially to the program variables, constants and modules as per their requirements, in the sequence in which they appear in the program. The EVEN directive updates the next even address of the current location, counter contents are not even, and assign the following routine or variable or constant to that address. The following structure explains the directive.
EVEN
PROCEDURE ROOT
..
..
ROOT ENDP
EQU : Equate
The EQU directive is used to assign a label with a value or a symbol. It is used to reduce the recurrence of the numerical values or constants in a program code. Using the EQU directive, even an instruction mnemonics can be assigned with a label, which can be used in the program in place of that mnemonic.
The following example shows the syntax.
Example :
LABEL EQU 0500H
ADDITION EQU ADD
The first statement assign the constant 500H with the label LABEL, while in the second statement assigns another label ADDITION with mnemonic ADD.
EXTRN : External and PUBLIC : Public
The EXTRN directive informs the assembler that the names, procedures and labels declared after this directive have already been defined in some other assembly language modules. While in the other module, where the names, procedures and lables appear they must be declared as public, using the PUBLIC directive.
MODULE1 SEGMENT
PUBLIC FACTORIAL FAR
MODULE1 ENDS
MODULE2 SEGMENT
EXTRN FACTORIAL FAR
MODULE2 ENDS.

GROUP: Group the Related Segments
The GROUP directive is used to form logical groups of segments with similar purpose or type. It is used to inform the assembler to form a logical group of the following segment names. Thus all such segments and labels can be addressed using the same segment base.
Program group code, data, stack
The above statement directs the loader/linker to prepare an EXE file such that CODE, DATA and STACK segment must be within a 64k byte memory segment that is named as PROGRAM. Now, for ASSUME statement, one can use the label PROGRAM rather than CODE, DATA and STACK as shown
ASSUME CS: PROGRAM, DS; PROGRAM, SS: PROGRAM.
LABEL: Label
The LABEL directive is used to assign a name to the current content of the location counter. When the assembly process starts the assembler initializes a location counter to keep track of memory locations assigned to the program. As the program assembly proceeds, the contents of the location counter are updated. During the assembly process, whenever the assembler comes across the LABEL directive, it assigns the declared label with the current contents of the location counter.
LENGTH: Byte length of a label
This directive is not available in MASM, and used to refer the length of a data array or a string.
MOV CX, LENGTH ARRAY
This statement, when assembled, will substitute the length of the array ARRAY in bytes, in the instruction.
LOCAL
The labels, variables, constants or procedures declared LOCAL in a module are to be used only by that particular module.
Example:
LOCAL a,b DATA, ARRAY, ROUTINE.
NAME: Local NAME of a Module
The NAME directive is used to assign a name to an assembly language program module. The module, may now be referred by its declared name.
OFFSET: Offset of a Label
When the assembler comes across the OFFSET operator along with a Label, it first computes the 16-b it displacement(also called as offset interchangeably) of the particular label, and replaces the string ‘OFFSET LABEL’ by the computed displacement. This operator is used with arrays, strings, labels and procedures to decide their offsets in their default segments.
The examples of OFFSET operator are as follows:
Example :
CODE SEGMENT
MOVE SI, OFFSET LIST
CODE ENDS
DATA SEGMENT
LIST DB 10 H
DATA ENDS
ORG: Origin
The ORG directive directs the assembler to start the memory allotment for the particular segment, block or code from the declared address in the ORG statement. The location counter is initialized to 0000 if the ORG statement is not written in the program. If an ORG 200H statement is present at the starting of the code segment of the module, then the code will start from 200H address in code segment.
PROC: Procedure
The PROC directive marks the start of a named procedure in the statement. And also the types NEAR or FAR specify the type of the procedure. That is, whether it is to be called by the main program located within 64k of physical memory or not.
Example :
RESULT PROC NEAR
ROUTINE PROC FAR
PTR: Pointer
The POINTER Operator is used to declare the types of label, variable or memory operand. The operator PTR is prefixed by either BYTE or WORD.
PUBLIC:
The PUBLIC directive is used along with the EXTRN directive. And informs the assembler that the labels, variables, constants or procedures declared PUBLIC may be accessed by other modules. But while using the EXTRN declared labels, variables, constants or procedures, the user must declare them external using the EXTRN directive.
SEG: Segment of a Label
The SEG operator is used to decide the segment address of the label, variable, or procedure and substitutes the segment base address in place of “SEG” label.
Example:
MOV AX, SEG ARRAY ; This statement moves the segment address of ARRAY in
MOV DS,AX ; Which it is appearing, to register AX and then to DS.
SEGMENT: Logical Segment
The SEGMENT directive marks the starting of a logical segment. The started segment is also assigned a name that is label, by the statement. In some cases, the segment may be assigned a type like PUBLIC or GLOBAL (can be accessed by any other modules)
Examples:
EXE CODE SEGMENT GLOBAL
; start of segment named EXE.CODE
; that can be accessed by any other module
EXE. CODE ENDS; END of EXE.CODE logical segment
SHORT
The SHORT operator indicates to the assembler that only one byte is required to code the displacement for a jump(that is displacement is within -128 to +127 bytes from the address of the byte next to the jump.
Example:
JMP SHORT LABEL
TYPE:
The TYPE operator directs the assembler to decide data type of the specified label and replaces the ‘TYPE’ label by the decided data type. For the word type variable, the data type is 2, for the double word type, it is 4, and for byte type 1.
GLOBAL
The labels, variables, constants or procedures declared GLOBAL may be used by other modules of the program.
Example:
ROUTINE PROC GLOBAL
‘+’ & ‘-‘ Operators:
These operators represent arithmetic addition and subtraction respectively and they are typically used to add or substract displacements(8 or 16 bit) to base, index etc.
Example:
MOV AL, [SI + 2]
MOV DX, [BX -5]
MOV BX, [OFFSET LABEL F10H]
MOV AX, [B + 91]
FARPTR:
The FAR PTR directive indicates the assembler that label following FAR PTR is not available within the segment and the address of the label is 32 bits, that is 2 bytes offset followed by 2 bytes segment address
Example:
JMP FAR PTR LABEL
CALL FAR PTR ROUTINE
NEAR PTR
The NEAR PTR directive indicates the label following NEAR PTR is in the same segment and requires only 16-bit that is 2 byte offset to address it.
Example:
JMP NEAR PTR LABEL
CALL NEAR PTR ROUTINE