What is Direct Mapping | How it works in memory managemant?

Before we start what is direct mapping, make sure that you need to know what is cache mapping so let’s get started.

Mainly there are three different cache mapping technique

  • Direct mapping
  • Fully associative mapping
  • K-way set associative mapping


What is direct mapping?

Basically when we need to transfer data from main memory to cache memory at that time the strategy which are using by us that strategy called direct mapping.

Here I tried to explain you what is direct mapping with the help of two small architecture so let’s understand.


Direct Mapping

Here first I want to explain you what is happening here there are two diagram cache memory and second is main memory so let’s start with main memory.

In this example the storage of main memory is 128 words so here question arise that what is word the word is nothing else but it is similar to Byte here you can take one word equals to one byte. So there are 128 words.

As we all know one block of memory can store maximum four words or four byte so if we want to find how many blocks are there we can simply divides words by 4 so let’s see 128/4=32 which means there are 32 blocks.

Here I name block as B0 B1 B2 up to B31 one block can carry four words so B0 can carry W0 W1 W2 W3 you can see in diagram. Let’s move further to the cache memory.

Here in cache memory the lines are represented as L0 L1 L2 L3 up to as long as you need of the data storage.

Suppose here in cache memory the capacity of cache memory is 16 words in cache memory as we know we can store blocks into the line so if we want to find how many lines are there we need to simply divide the words by 4 so let’s see 16/4=4 so there are 4 lines In a cache memory.


How direct mapping Works?

So now here direct mapping comes into scenario here when we want to transfer the data from main memory to cache memory we using direct mapping technique.   

Suppose here the data are transferring to the cache memory in the form of block storage so blocks are start storing in lines with the help of K mod n system let’s understand with example.

Direct Mapping

As I saw you in image if we want to store blocks in we are using K nod n method here k takes place for block no and n takes place for no of lines as I saw you some examples

Example :

0 mod 4 means block B0 divides by 4 then answer is 0 so we store B0 in line 0 or L0

1 mod 4 means block B1 divides by 4 then answer is 1 so we store B1 in line 1 or L1

2 mod 4 means block B2 divides by 4 then answer is 2 so we store B2 in line 2 or L2

3 mod 4 means block B3 divides by 4 then answer is 3 so we store B3 in line 3 or L3


So after reading this full article if you have any query then you can ask me in comment section and if you get the answer what you want then do comments and do not forget to share the article on direct mapping to your friends and thank you for reading the article.


Post a comment