To do any work with assembly language or computer, it is necessary to have data using which any result is obtained. So these various data like - (video audio text any input or any instruction etc.) all these are stored in some location of memory. Then when they have to be accessed. The complete data is then retrieved by locating the starting bit location of that data. Or if a program is running, then the location of useful data for that and the next running instruction are also easily run through the addressing mode.
Assembly - Addressing Modes
Most assembly language instructions require operands to run. An operand address provides the location where the data to be processed is stored. Some instructions do not require one operand, while some other instructions may require one, two or three operands.
When an instruction requires two operands, the first operand is usually the destination, which holds data in a register or memory location, and the second operand is the source. The source consists of either the data to be delivered (the immediate address) or the address of the data (in a register or memory). Generally, the source data remains unchanged after the operation.
In general three methods of data addressing are used.
- Register addressing
- Immediate addressing
- Memory addressing
Register addressing
In register addressing the operands are in a register. Depending on the instructions, they contain the address of the data present in a register or use direct data.
MOV DX, TAX_RATE ; Register in first operand
MOV COUNT, CX ; Register in second operand
MOV EAX, EBX ; Both the operands are in registers
Since memory is not involved in data processing between registers, it provides the fastest processing of data.
Immediate addressing
The immediate addressing operand is a constant value or expression. When an instruction with two operands uses immediate addressing, the first operand may be a register or memory location, and the second operand an immediate constant. The first operand defines the length of the data.
BYTE_VALUE DB 150 ; A byte value is defined
WORD_VALUE DW 300 ; A word value is defined
ADD BYTE_VALUE, 65 ; An immediate operand 65 is added
MOV AX, 45H ; Immediate constant 45H is transferred to AX
Direct Memory Addressing
In direct addressing mode, the offset value is specified as part of the direct 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 operand refers to a memory location and the other operand refers to a register.
ADD BYTE_VALUE, DL ; Adds the register in the memory location
MOV BX, WORD_VALUE ; Operand from the memory is added to register
When operands are specified in memory addressing mode, direct access to main memory is required, usually in data segments. This method of solution results in slow processing of data. To locate the exact location of the data in memory, we need the segment start address, which is usually found in the DS register and the offset value. This offset value is also called the effective address.
Direct-Offset Addressing
This addressing mode uses arithmetic operators to modify an address. For example, look at the following definitions that define a table of data −
BYTE_TABLE DB 14, 15, 22, 45 ; Tables of bytes
WORD_TABLE DW 134, 345, 564, 123 ; Tables of words
The following operations access data in registers from tables in memory −
MOV CL, BYTE_TABLE[2] ; Gets the 3rd element of the BYTE_TABLE
MOV CL, BYTE_TABLE + 2 ; Gets the 3rd element of the BYTE_TABLE
MOV CX, WORD_TABLE[3] ; Gets the 4th element of the WORD_TABLE
MOV CX, WORD_TABLE + 3 ; Gets the 4th element of the WORD_TABLE
indirect memory addressing
This addressing mode utilizes the segmentation capability of the computer. Indirect addressing is typically used for variables that contain multiple elements, such as arrays. The starting address of the array is stored in the EBX register.
The following code snippet shows how to access the different elements of the variable.
MY_TABLE TIMES 10 DW 0 ; Allocates 10 words (2 bytes) each initialized to 0
MOV EBX, [MY_TABLE] ; Effective Address of MY_TABLE in EBX
MOV [EBX], 110 ; MY_TABLE[0] = 110
ADD EBX, 2 ; EBX = EBX +2
MOV [EBX], 123 ; MY_TABLE[1] = 123
MOV instruction
MOV instruction is used to move data from one storage location to another. MOV instruction takes two operands.
MOV destination, source
MOV register, register
MOV register, immediate
MOV memory, immediate
MOV register, memory
MOV memory, register
Please note that - in MOV operation both the operands should be of same size. the value of the source operand remains unchanged
MOV EBX, [MY_TABLE] ; Effective Address of MY_TABLE in EBX
MOV [EBX], 110 ; MY_TABLE[0] = 110
It is not clear here whether you want to shift the byte or word equivalent of the number 110. In such cases, it is wise to use the type specifier.
The following table shows some of the common type specifiers −
Type specifier | bytes addressed |
---|---|
byte | 1 |
word | 2 |
DWORD | 4 |
QWORD | 8 |
TBYTE | 10 |
The following program illustrates some of the concepts discussed above. It stores a name 'dooseep' in the data section of memory, then programmatically changes its value to another name 'prg' and displays both the names.
section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
;writing the name 'dooseep'
mov edx,9 ;message length
mov ecx, name ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov [name], dword 'prg' ; Changed the name to prg
;writing the name 'prg eep'
mov edx,8 ;message length
mov ecx,name ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
name db 'dooseep'
When the above code is compiled and executed, it produces the following result −
dooseep prg eep