What are the different addressing modes with help of examples?



Let us think that the memory of your computer is byte addressable. Every datatype like integer, floating / double number, string takes some k bytes (k=1,2,3,4 etc). The amount of memory each datatype consumes is already defined by the system manufacturer.
What is byte addressable?
So in memory for every byte, there will be address.
Eg: your city - memory, house - byte, people in the house - data (bits), house number -address of the house.
So addresses are used to identify data

you already have some data (some songs) in the computer. Now You want to move a song from one address to another address by writing code.
Now you have to decide how to access song in order to move it. Here comes the topic of addressing modes:
Case 1: you know the address of song then chose direct addressing mode. Directly mention the address and access it
Eg: Address of song =20h (hexadecimal)
Where to move = 80h
Mov 80h,20h
Actually, there is no instruction as above one because I am doing memory to memory operation which costs me a lot of time, you can have some coffee in the meanwhile to move song .
Why it takes more time???
1.Because memory is far away from your processor
2. slow address decoding capability of your memory
3. memory is large in size
So how can I reduce the time???
  1. Use Registers which are very fast for accessing them.
  2. Registers are also typed of memories which are lesser in size (for a 16-bit processor, each Register size is 16 bits)
  3. They are nearer to the processor (in ALU)
There are 3 types of instructions in any processor :
1.memory to memory (CISC processor)
2. Register to register (RISC processor)
3.register to memory(both RISC and CISC)
Case 2: you are trying to access the song many times.you cannot remember the address of the song every time you use it, then store it in the register(AX, BX, CX, DX).You can remember the name of register easily.
But how you access the data through Register is important this is called Register indirect addressing.
Example:
Address of song =20h (hexadecimal)
Where to move = 80h
Mov ax,20h
Mov 80h,@ax
Case 3: for supposing you remember the data of song i.e 0110100011110000
Let us assume that data in hexadecimal.(68A0 h)
Actually, data of song may be in megabytes but for easy understanding, I am considering 2 bytes.
You want to Load the song directly into register. This is called IMMEDIATE ADDRESSING.
Example:
Mov Ax,#68A0 h
Case 4:
For some purpose, You want to move the data of song from one Register to another Register. This is called REGISTER MODE ADDRESSING.
Example:
Mov Ax,#68A0 h
Mov BX,AX
Case 5:
You have five songs in sequence in memory.YOU have to move them to other memory locations in sequence.
Now you can not remember all the addresses of songs, so remember the first one and you can calculate the addresses of other songs, as each song occupies the same amount of memory.
Each song occupies 2 memory location so 2 bytes for each song, 5 *2 bytes=10bytes for all songs.
First song address = 2015h
Index=2
DESTINATION ADDRESS=3001h
So here comes Index addressing.
Use index Register (SI) for incrementing memory locations.
Note:[ax ] here [] is used to tell the assembler that it refers to data present in the address present in the register
Example:
Mov SI ,0
Mov CX ,3001h
Mov BX,5
Label :MOV AX, [SI+2015h]
Mov [CX] ,AX
Add CX,2
Add SI,2
Dec Bx /*for subtracting 1 from BX*/
Add AX,1
CMP BX,0
JNZ Label. /*If BX REGISTER VALUE NOT EQUAL TO ZERO JUMP TO LABEL AND EXECUTE.*/
Don't worry after seeing the code, just
decode the above instructions according to each iteration you will find the answer.
Case 6:
If you have songs in two-dimensional arrays. You have moved from one memory location to another
Here comes BASED INDEX DISPLACEMENT ADDRESSING
Example: There are 5 movies, each movie has 6 songs.
Each song 2 bytes
Each movie takes 12 bytes for all 6 songs
Base register - BX
Index Register-SI ( FOR moving between columns)
OFFSET -12 (For moving between Rows)
Exercise: try to write code by your self and write in the comment section I will answer the code within 10 days
**NOTE:
STILL, there are a lot of addressing modes but I discussed the important and frequently used ones.
1. Instructions are based on 8086 microprocessor
2. I have used the instructions in the logical way which are correct, there are some mistakes regarding instructions if you find it, please notify me
3.if you didn't understand any concept please raise questions in the comment section
4.if there is anything wrong in my answer please notify me
5.In order to know how instructions have executed the look for my answer that I have already written
@how do microprocessors execute instructions
6. Please feel free to ask questions
Next Post Previous Post
No Comment
Add Comment
comment url