146k views
4 votes
Programs for embedded devices are often written in assembly language. Some embedded processors have limited instructions, like MARIE. Create a MARIE program which determines whether a given address is a cache hit or not. For this assignment, the cache is 2-way set associative, addresses are 8 bits, blocks are 8 bytes, and the cache has 8 blocks.

Requirements
1. The program must be written in MARIE.
2. The input is a hex address (2 hex digits), the output is 1 for a cache hit and 0 for a cache miss.
3. The cache table is
Set 0 1 5
Set 1 2 4
Set 2 3 2
Set 3 6 0
4. Unlike a real memory system, for this assignment, the cache table won’t change on a cache miss.
Hints: 1.
You have already written a multiply subroutine for MARIE. You could use a similar integer divide subroutine for this. It could return both a quotient and a remainder.
2. I recommend writing this in Java or C++ first to test your algorithm. Write the divide method using repeated subtraction as you will in MARIE.
3. The textbook describes the LoadI instruction, but the MARIE simulator does not recognize that. You can accomplish the same thing using Clear and AddI.
Upload: Your MARIE source file (.mas)
Sample Output
Input: B5, Output: 0
Input: A5, Output: 1
Input: 6C, Output: 0
Input: 7D, Output: 0
Input: 8B, Output: 1

User Mlamp
by
4.9k points

1 Answer

2 votes

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;

}

Step-by-step 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.

User OpenSource
by
4.6k points