Addressing in Assembly
NOTE:To learn this tutorial ,you need to know about Registers,Pointers,Index Registers,etc.
Address mode are ways of locating,indicating, transferring data.The "mov" instruction is used in addressing data.The 'mov' instruction has 2 operands , destination and the source.The 'mov' instruction copies the data from the source to destination.The format goes like this, "mov dest,src".Now what addressing mode means is the way of moving data or copying.
NOTE:The ';'symbol is used as comment in assembly.
Example:
;The format is mov destniation, source
mov eax,edx
This simple program copies the 32bit data from ebx to eax.
Addressing Modes
- Register Addressing Mode
In this register addressing mode ,the instruction is use between 2 registers.This is the fastest
type of addressing mode as it is between registers and there is no interference with memory.
Examples:
mov ecx,ebx
mov ax,bx
2. Immediate Addressing Mode
In this type of addressing, the data occupies the immediate next byte in the memory to the
instruction code in thememory.In simple words, it copies the data given in the source to the
destination.Valid sources are hex,decimal,binary.
Examples:
mov al,20H ;Copies 20(8bit value) in Hex to al which is a 8bit Register
mov ax,2000H ;Copies 2000 in Hex to ax which is a 16bit register.
mov eax,20000H ;Same as the above but copies a 32bit value to a 32bit register 'eax'
3. Memory Addressing Mode
In this type, one of the operand is a memory location or a memory address.The Data Segment is
the default segment register for addressing data.
Examples:
mov [2000H],eax ; Copies the data on eax to 2000 offset of the data segment
mov ES:[2000],ebx ;Copies the data on ebx register to memory with offset 2000 and segment ES
mov eax,[2000H] ;Copies the data from the memory with the offset 2000 to the eax register.
TIP:To access a memory, the processor writes the address of the data to be accessed on the
address bus(Segment:Offset).
-------------------------------------------------------------------------------------------------------------------------------
There are subdivisions under the above categories, they are given below.
- Direct Memory Addressing
The offset address of the memory is directly provided to the mov instruction is known as direct
memory addressing.
Example:
mov ax,[2000H]
2. Indirect Memory Addressing
In Indirect Memory Addressing the instruction uses the base or index registers as offset.(BX,BP,DI,SI)
Example:
mov ax,[BX]
3. Based Addressing Mode
In this type of addressing the address is obtained by adding a displacement value and the content of
the base register(BP or BX).
Examples:
mov ax,value[BX] ;This is same as mov ax,value + [bx]
4. Indexed Addressing
In this type of addressing the address is obtained by adding a displacement value and then content
of a index register.
Example:
mov ax,value[SI]
5. Base Indexed Addressing
In this type, I needn't explain,the name shows it, here the address is obtained by adding a
displacement value and content of the base register and the index register.
Example:
mov ax,value[BX][SI]
6. Port Addressing Mode
This is somthing spcial, you are going to read or write the data from or to a I/O port.
There are 2 functions for these, in and out.The in is used to take input from the given port and the
out just the opposite.
Examples:
in al,60H ;Reads data from the port 60
out dx,al ;Write data on the al register to the port address in dx register.
NOTE:The input or output size is variable, if in ax,dx , takes in 16bit data, if in eax,dx takes in
32bit data
-------------------------------------------------------------------------------------------------------------------------------Common Errors
- You cannot move the data of a 32bit Register to a 16bit Register.The registers must be of the same size.
- Segment register cannot be copied to another segment register.
- You cannot transfer memory to memory
mov ds,es ;This is wrong, Segment register cannot be copied to another segment register
mov ax,ebx ;This is wrong ,Sizes of the register must be same,here ebx is 32bit and ax is 16bit
mov [DI],[BX] ;This is wrong, memory to memory transfer is not possible
-------------------------------------------------------------------------------------------------------------------------------
Practice Paper
- What are the instructions used in addressing memory?
- Name the different categories of addressing mode.
- Correct the errors:
- mov ebx,ax
- mov ds,es
- mov [1000H],[10010H]
- mov [DI],[BX]
- mov ax,edx
4. Write a code to copy data from a register to a memory, take input and store the input in dx.
5. Write a code implementing almost all the types of addressing modes in single script.