Addressing Modes
Most assembly language instructions require operands to be processed. An operand address provides the location where the data to be processed is stored. Some instructions do not require an operand, whereas some other instructions may require one, two or three operands.
When an instruction requires two operands, the first operand is general y the destination, which contains data in a register or memory location and the second operand is the source. Source contains either the data to be delivered (immediate addressing) or the address (in register or memory) of the data. Generally the source data remains unaltered after the operation.
The three basic modes of addressing are:
Register addressing
Immediate addressing
Memory addressing
Register Addressing
In this addressing mode, a register contains the operand. Depending upon the instruction, the register may be the first operand, the second operand or both.
For example,
As processing data between registers does not involve memory, it pro vides fastest processing of data.
Immediate Addressing
An immediate operand has a constant value or an expression. When an instruction with two operands uses immediate addressing, the first operand may be a register or memory location, and the second operand is an immediate constant. The first operand defines the length of the data.
For example:
Direct Memory Addressing
When operands are specified in memory addressing mode, direct access to main memory, usually to the data segment, is required. This way of addressing results in slower processing of data. To locate the exact location of data in memory, we need the segment start address, which is typically found in the DS register and an offset value. This offset value is also called effective address.
In direct addressing mode, the offset value is specified directly as part of the instruction, usually indicated by the variable name. The assembler calculates the offset value and maintains a symbol table, which stores the offset values of all the variables used in the program.
In direct memory addressing, one of the operands refers to a memory location and the other operand references a register.
For example,
Direct-Offset Addressing
This addressing mode uses the arithmetic operators to modify an address. For example, look at the following definitions that define tables of data:
The following operations access data from the tables in the memory into registers:
Indirect Memory Addressing
This addressing mode utilizes the computer's ability of Segment:Offset addressing. Generally the base registers EBX, EBP (or BX, BP) and the index registers (DI, SI), coded within square brackets for memory references, are used for this purpose.
Indirect addressing is generally used for variables containing several elements like, arrays. Starting address of the array is stored in, say, the EBX register.
The following code snippet shows how to access different elements of the variable.
The MOV Instruction
We ha ve already used the MOV instruction that is used for moving data from one storage space to another. The MOV instruction takes two operands.
SYNTAX:
Synta x of the MOV instruction is:
The MOV instruction may have one of the following five forms:
Please note that:
Both the operands in MOV operation should be of same size
The value of source operand remains unchanged
The MOV instruction causes ambiguity at times. For example, look at the statements:
It is not clear whether you want to move a b yte equivalent or word equivalent of the number 110. In such cases, it is wise to use a type s pecifier.
Following table shows some of the common type specifiers:
EXAMPLE:
The following program illustrates some of the concepts discussed above. It stores a name 'Zara Ali' in the data section of the memory. Then changes its value to another name 'Nuha Ali' programmatically and displays both the names.
When the above code is compiled and executed, it produces fol owing result: