1. Simulating a "brookshear machine"

Hi everyone, can someone please help me in understanding how to increment the pc counter and where to put my printf's in my code. This assignment is giving me some trouble. heres the question.

Your program steps though the machine code, one instruction at a time. Before each instruction, your program prints out a line of text that describes the current contents of the Program Counter, the instruction to be executed and the registers.
Each time an instruction is executed your program alters the Program Counter, Registers and Memory accordingly.

The format of the output line is:

PC INST – [R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 RA RB RC RD RE RF]

Where PC is the program counter, INST is the current instruction and R0 through to RF are the value of the sixteen registers. Each field is output as a 2 or 4 digit hexadecimal value (use the X format specifier to get capital letters) with a leading 0 if necessary.

Output continues (with no user intervention) until the Halt instruction is executed, at which time your program also stops.
A Halt instruction will occur in all test programs.

Example: if a3.txt contains:

B404
239A
2412
5345
350C
C000

Then the output is:

00 B404 – [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00]
04 2412 - [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00]
06 5345 - [00 00 00 00 12 00 00 00 00 00 00 00 00 00 00 00]
08 350C - [00 00 00 00 12 12 00 00 00 00 00 00 00 00 00 00]
0A C000 - [00 00 00 00 12 12 00 00 00 00 00 00 00 00 00 00]

Use unsigned char's for your PC, registers and memory.

When reading the a3.txt file, be careful with alignment problems in the scanf-like functions. First read each line into an unsigned int variable, then extract out the two bytes using bit-wise operators and store them in your memory array.

this is what i have so far
Code:
```#include <stdio.h>
#include <string.h>

int main(){

int unsigned r[16]={0};
int unsigned mem[255];
int i=0;
unsigned int temp=0;

FILE *f;
f=fopen("a3.txt", "r");

while(fscanf(f, "%x", &temp)!= EOF){
i=i+2;
}

int a=0,b=0,c=0,d=0,cd=0;
unsigned char pc=0;

//i tried doing printf's below but the program counter(pc) doesnt increment so i end up with the same output easch time
/*printf("%02X %X%02X - [", pc,mem[pc],mem[pc+1]);
for(int j=0; j<15; j++){
printf("%02X ", r[j]);
}
printf("%02X]\n", r[15]);
*/

while (mem[pc]!=0xC0){
a = (mem[pc] & 0xF0)>>4;
b = (mem[pc] & 0xF);
c = (mem[pc+1] & 0xF0)>>4;
d = (mem[pc+1] & 0xF);
cd = (mem[pc+1]);

pc=pc+2;

if(a==0x01){
r[b]=mem[cd];
}

if(a==0x02){
r[b]=cd;
}

if(a==0x03){
r[cd]=mem[b];
}

if(a==0x04){
r[d]=r[c];
}

if(a==0x05){
r[d]=r[b]+r[c];
}

if(a==0x07){
r[d]=r[b] | r[c];
}

if(a==0x08){
r[d]=r[b] & r[c];
}

if(a==0x09){
r[d]=r[b] ^ r[c];
}

//i am unsure of this rotate beneath. can someone please tell me what im doing wrong.
if(a==0x0A){
for(int j=0; j<d; j++){
unsigned int least = r[b] & 0x01;
unsigned int most = least << 7;
r[b] = r[b] >> r[d];
r[b] = r[b] | most;
}
}

if(a==0x0B){
if(r[b]==r[0]){
pc=cd;
}
}
}
}```

2. Rotating is simple, you just need:
Code:
```number = 123;
rotBits = 3;

/* Left Rotation */
number = (number << rotBits) | (number >> (sizeof(number)*CHAR_BITS - rotBits));
/* Right Rotation */
number = (number >> rotBits) | (number << (sizeof(number)*CHAR_BITS - rotBits));```
EDIT: The above code only works for "rotBits" >= 0

3. im having a hard time understanding whats going on in your rotates. could you explain it a bit further for me please?
All the instructions are the same size, they are below.

Brookshear machine instructions

2RXY Load register R with the value XY

3RXY Store the value in register R at memory address XY

40RS Copy/move the value in register R to register S

5RST Add the values in registers R and S and put the answer in register T

7RST Bit-wise OR the values in registers R and S and put the answer in register T

8RST Bit-wise AND the values in registers R and S and put the answer in register T

9RST Bit-wise XOR the values in registers R and S and put the answer in register T

AR0X Rotate the contents of register R X times to the right

BRXY Jump to the instruction located at memory address XY if the value in register R is equal to the value in register 0 (i.e. change the PC to XY)

C000 Halt

4. When you're rotating to the left, you want to save the left-most bits, by moving them to the right-most place. Vice-versa for right rotation.