Question

So for my assignment I have to simulate basic machine language with C. The machine has 16 registers (reg[]), a program counter(pc) and memory (mem[]) all of which are unsigned chars. The instructions are read in from a file and are in the format:

B404 (1RXY = Load register R with the value at memory address XY). All numbers are in hex. C000 is the halt command.

Now my problem is that when I print out the instructions (when stored in a,b,c,d and cd) b and d have leading zeros. How do I get rid of them so the instruction is printed as above? (Line 48).

Also it appears some of my if statements aren't called as if I put a printf() in them and they are on the file it is never printed. (Lines 48 to 78).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main () {
FILE *file;
file = fopen("a3.txt","r");
unsigned int temp = 0;
unsigned char pc, mem[256], reg[16],a,b,c,d,cd;
unsigned int count;
count = 0;
pc = 0;
for (int i=0; i < 16;++i) {
    reg[i] = 0;
}

if (file == NULL) {
    printf("\n Error opening the file");
    exit(0);
}
for (int i=0; !feof(file); i += 2) {
    fscanf(file, "%X", &temp);
    mem[i] = ((temp & 0xFF00) >> 8);
    mem[i + 1] = (temp & 0xFF);
    if (feof(file)) {
        break; // exit from while
    }
    ++count;
}
int fclose(FILE *file);
/*while (mem[pc] != 0xC0) {*/
for (unsigned int i=0; i < count-1;++i) {
    if (mem[i] == 0xC0) {
        break; // exit from for
        exit(1);
    }
    cd = mem[pc + 1];
    a = (mem[pc] & 0xF0);
    b = (mem[pc] & 0xF);
    c = (mem[pc + 1] & 0xF0);
    d = (mem[pc + 1] & 0xF);
    printf("%02X ",pc);
    printf("%X%X%X%X - [",a,b,c,d);
    printf("%02X %02X %02X %02X ",reg[0],reg[1],reg[2],reg[3]);
    printf("%02X %02X %02X %02X ",reg[4],reg[5],reg[6],reg[7]);
    printf("%02X %02X %02X %02X ",reg[8],reg[9],reg[10],reg[11]);
    printf("%02X %02X %02X %02X]\n",reg[12],reg[13],reg[14],reg[15]);
    if (a == 0x1) {
        reg[b] = reg[cd];
    }
    if (a == 0x2) {
        reg[b] = cd;
        //printf("2 reporting in");
    }
    if (a == 0x3) {
        reg[cd] = reg[b];
    }
    if (a == 0x4) {
        reg[d] = reg[c];
    }
    if (a == 0x05) {
        reg[d] = (reg[b] + reg[c]);
        //printf("5 reporting in");
    }
    if (a == 0x7) {
        reg[d] = (reg[b] | reg[c]);
    }
    if (a == 0x8) {
        reg[d] = (reg[b] & reg[c]);
    }
    if (a == 0x9) {
        reg[d] = (reg[b] ^ reg[c]);
    }
    if (a == 0xA0) {
        reg[b] = (reg[b] >> reg[d]) | (reg[b] << (32 - reg[d]));
    }
    pc += 2;
    if (a == 0xB0) {
        //printf("B reporting in");
        if (reg[b] == reg[0]) {
            pc = cd;
        }
    }
}
return 0;
}

Thanks!

Was it helpful?

Solution

To get the high bits of an unsigned char, use the following function:

unsigned char hi(unsigned char bits) {
  return (bits >> 4) & 0x0F;
}

And when you are doing the instruction dispatch, it is better to use a switch statement instead of an if ... else if ... else cascade. Then you can have a simple default: fprintf(stderr, "unknown instruction: %u\n", a); exit(1); for error detection.

Also, 0xA0 is not the value 10, its rather 160, so you should either write 10 or 0x0A.

Update:

The code for extracting a, b, c and d should look like this:

a = (mem[pc + 0] >> 4) & 0x0F;
b = (mem[pc + 0] >> 0) & 0x0F;
c = (mem[pc + 1] >> 4) & 0x0F;
d = (mem[pc + 1] >> 0) & 0x0F;

This is simple to read and easy to check for correctness. One can clearly see the uniform handling, and that each value is restricted to the range [0x00;0x0F].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top