C:我如何模拟8086寄存器?

bra*_*ump 4 c simulator cpu-registers x86-16

Ohai,我目前正在尝试实现8086 ASM调试器以用于学习目的.到目前为止,我试图用char数组模拟8位和16位寄存器,但这种方法让我疯狂,在使用AX,AL和AH时.

#define setAL() { int i; for (i = 0; i < 8; i++) AL[i] = AX[i]; }
char AX[16]   = {0, 1, 1, 1, 1 ,1 ,1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char AL[8]    = {0, 0, 0, 0, 0, 0, 0, 0};
Run Code Online (Sandbox Code Playgroud)

有没有人有任何好主意(或类似'最佳实践')如何模拟这些寄存器?

rus*_*_uk 7

我不认为有这样做的"最佳实践"方法,但你可以采取的一种方法可能会让你更少疯狂,就是使用一个联合覆盖8位和16位部分:

struct RegByte { 
   unsigned char low;
   unsigned char high;
};

struct RegWord {
   unsigned short value;
};

union Reg {
   struct RegWord word;
   struct RegByte bytes;
};
Run Code Online (Sandbox Code Playgroud)

或者,如果你明确地只针对8086,你可以有一个包含所有16位寄存器的结构,一个包含所有字节部分的结构.例如

struct RegByte {
   unsigned char al, ah, bl, bh, cl, ch, dl, dh;
};

struct RegWord {
   unsigned short ax, bx, cx, dx;
   /* nothing stopping you from continuing with si, di, etc even though
    * they don't have addressable high and low bytes */
};

union Reg {
   struct RegWord word;
   struct RegByte byte;
};
Run Code Online (Sandbox Code Playgroud)