Answer:
#include <iostream>
using namespace std;
int main()
{
char address[2];
int tag, firstBit, secondBit, setNumber;
int cache[4][2]={{1,5}, {2,4}, {3,2}, {6,0}};
cout << "Enter the address as hex(in small letters: "<
cin >> address;
for (int i < 0; i < 8; i++){
if (address[0] == '0'){
tag = 0;
firstBit = 0;
} else if (address[0] == '1'){
tag = 0;
firstBit = 1;
} else if (address[0] == '2'){
tag =1;
firstBit = 0;
} else if (address[0] == '3'){
tag = 1;
firstBit = 1;
} else if (address[0] == '4'){
tag = 2;
firstBit = 0;
} else if (address[0] == '5'){
tag = 2;
firstBit = 1;
} else if (address[0] == '6'){
tag = 3;
firstBit = 0;
} else if (address[0] == '7'){
tag = 3;
firstBit = 1;
} else if (address[0] == '8'){
tag = 4;
firstBit = 0;
} else if (address[0] == '9'){
tag = 4;
firstBit = 1;
} else if (address[0] == 'A'){
tag = 5;
firstBit = 0;
} else if (address[0] == 'B'){
tag = 5;
firstBit = 1;
} else if (address[0] == 'C'){
tag = 6;
firstBit = 0;
} else if (address[0] == 'D'){
tag = 6;
firstBit = 1;
} else if (address[0] == 'E'){
tag = 7;
firstBit = 0;
} else if (address[0] == 'F'){
tag = 7;
firstBit = 1;
} else{
cout<<"The Hex number is not valid"<< endl;
}
}
if(address[1]>='0' && address[1]<'8'){
secondBit = 0;
} else if(address[1]=='8'|| address[1]=='9'||(address[1]>='a' && address[1]<='f')){
secondBit = 1;
} else{
cout<<"The Hex number is not valid"<< endl;
return 0;
}
setNumber = firstBit * 2 + secondBit;
if(cache[setNumber][0]==tag || cache[setNumber][1]==tag){
cout<<"There is a hit";
} else{
cout<< "There is a miss";
}
return 0;
}
Explanation:
The C++ source code prompts the user for an input for the address variable, then the nested if statement is used to assign the value of the firstBit value given the value in the first index in the address character array. Another if statement is used to assign the value for the secondBit and then the setNumber is calculated.
If the setNumber is equal to the tag bit, Then the hit message is printed but a miss message is printed if not.