0% found this document useful (0 votes)
5 views4 pages

Lab8 Cso

Uploaded by

poori.2819.hapo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Lab8 Cso

Uploaded by

poori.2819.hapo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB-8

C Program to Implement Direct Mapping, Associative Mapping ,Set-


Associative Mapping.
CODE:
#include <stdio.h>
#include <math.h>
int main() {
int cacheSize, blockSize, memorySize, mappingChoice;
printf("Enter the size of cache in words: ");
scanf("%d", &cacheSize);
printf("Enter the size of each block in words: ");
scanf("%d", &blockSize);
printf("Enter the size of memory in words: ");
scanf("%d", &memorySize);
printf("Choose the mapping technique:\n");
printf("1. Direct Mapping\n");
printf("2. Associative Mapping\n");
printf("3. N-way Set Associative Mapping\n");
scanf("%d", &mappingChoice);
int indexBits, tagBits, offsetBits;
switch(mappingChoice) {
case 1:
if(cacheSize % blockSize != 0) {
printf("Error: Cache size should be divisible by block size.\n");
return 1;
}
indexBits = log2(cacheSize / blockSize);
offsetBits = log2(blockSize);
tagBits = log2(memorySize) - indexBits - offsetBits;
break;
case 2:
indexBits = 0;
offsetBits = log2(blockSize);
tagBits = log2(memorySize) - offsetBits;
break;
case 3:
printf("Enter the value of N for N-way Set Associative Mapping: ");
int n;
scanf("%d", &n);
if(cacheSize % (blockSize * n) != 0) {
printf("Error: Cache size should be divisible by block size times N.\n");
return 1;
}
indexBits = log2(cacheSize / (blockSize * n));
offsetBits = log2(blockSize);
tagBits = log2(memorySize) - indexBits - offsetBits;
break;
default:
printf("Invalid choice.\n");
return 1;
}
printf("Index bits: %d\n", indexBits);
printf("Offset bits: %d\n", offsetBits);
printf("Tag bits: %d\n", tagBits);
return 0;
}

OUTPUT:
Enter the size of cache in words: 16
Enter the size of each block in words: 4
Enter the size of memory in words: 64
Choose the mapping technique:
1. Direct Mapping
2. Associative Mapping
3. N-way Set Associative Mapping
1
Index bits: 2
Offset bits: 2
Tag bits: 2
Enter the size of cache in words: 16
Enter the size of each block in words: 4
Enter the size of memory in words: 64
Choose the mapping technique:
1. Direct Mapping
2. Associative Mapping
3. N-way Set Associative Mapping
2
Index bits: 0
Offset bits: 2
Tag bits: 4
Enter the size of cache in words: 16
Enter the size of each block in words: 4
Enter the size of memory in words: 64
Choose the mapping technique:
1. Direct Mapping
2. Associative Mapping
3. N-way Set Associative Mapping
3
Enter the value of N for N-way Set Associative Mapping: 2
Index bits: 1
Offset bits: 2
Tag bits: 3

You might also like