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 mask1=0xFF00, mask2=0X00FF;
int i=0;
unsigned int temp=0;
FILE *f;
f=fopen("a3.txt", "r");
while(fscanf(f, "%x", &temp)!= EOF){
mem[i]=(temp & mask1)>>8;
mem[i+1]=(temp&mask2);
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;
}
}
}
}