C:如何模拟8086寄存器?

发布于 2024-12-28 12:14:36 字数 346 浏览 3 评论 0原文

欧海, 我目前正在尝试实现一个 8086 ASM 调试器以用于学习目的。到目前为止,我尝试用字符数组模拟 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};

有谁有任何好主意(或诸如“最佳实践”之类的东西)如何模拟这些寄存器?

Ohai,
I'm currently trying to implement an 8086 ASM debugger for learning purposes. Until now, I tried to simulate the 8 and 16 bit registers with char arrays but this approach is driving me nuts, when working with AX, AL and 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};

Does anyone has any good idea (or something like 'best practice') how to simulate those registers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜声 2025-01-04 12:14:36

我不认为有“最佳实践”方法可以做到这一点,但您可以采取的一种方法可能会少让您发疯,那就是使用联合来覆盖 8 位和 16 位部分:

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

struct RegWord {
   unsigned short value;
};

union Reg {
   struct RegWord word;
   struct RegByte bytes;
};

或者考虑到您明确的目标只是对于 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;
};

I don't think there's a 'best practice' way of doing this, but one approach you could take that may drive you nuts less is to use a union to overlay the 8 and 16 bit portions:

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

struct RegWord {
   unsigned short value;
};

union Reg {
   struct RegWord word;
   struct RegByte bytes;
};

Alternatively given you're explicitly targeting just 8086 you could have one structure containing all the 16 bit registers and one containing all of the byte portions. e.g.

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;
};
东走西顾 2025-01-04 12:14:36

我将结构抽象出来并使用访问器函数。

struct registry_file_t;
uint16_t get_al(registry_file_t * r);
void set_al(registry_file_t * r, uint16_t value);

启用内联后,这种方法的性能不会比使用联合差。

I'd abstract the structure away and use accessor functions.

struct registry_file_t;
uint16_t get_al(registry_file_t * r);
void set_al(registry_file_t * r, uint16_t value);

With inlining enabled, this approach will be no less performant than using a union.

且行且努力 2025-01-04 12:14:36

如果它可能对某人有用,我把我写的放在这里。

typedef struct cpu_8086_s cpu_t;

struct cpu_8086_s
{
    struct
    {
        union
        {
            uint16_t ax;
            struct
            {
                uint8_t al;
                uint8_t ah;
            };
        };
        union
        {
            uint16_t cx;
            struct
            {
                uint8_t cl;
                uint8_t ch;
            };
        };
        union
        {
            uint16_t dx;
            struct
            {
                uint8_t dl;
                uint8_t dh;
            };
        };
        union
        {
            uint16_t bx;
            struct
            {
                uint8_t bl;
                uint8_t bh;
            };
        };
        uint16_t sp;
        uint16_t bp;
        uint16_t si;
        uint16_t di;
    };
    struct
    {
        uint16_t es;
        uint16_t cs;
        uint16_t ss;
        uint16_t ds;
    };
    uint16_t ip;
    union
    {
        uint16_t flags;
        struct
        {
            uint8_t c: 1;
            uint8_t  : 1;
            uint8_t p: 1;
            uint8_t  : 1;
            uint8_t a: 1;
            uint8_t  : 1;
            uint8_t z: 1;
            uint8_t s: 1;
            uint8_t t: 1;
            uint8_t i: 1;
            uint8_t d: 1;
            uint8_t o: 1;
        };
    };
};

In case it might be useful for someone, I put the one I wrote here.

typedef struct cpu_8086_s cpu_t;

struct cpu_8086_s
{
    struct
    {
        union
        {
            uint16_t ax;
            struct
            {
                uint8_t al;
                uint8_t ah;
            };
        };
        union
        {
            uint16_t cx;
            struct
            {
                uint8_t cl;
                uint8_t ch;
            };
        };
        union
        {
            uint16_t dx;
            struct
            {
                uint8_t dl;
                uint8_t dh;
            };
        };
        union
        {
            uint16_t bx;
            struct
            {
                uint8_t bl;
                uint8_t bh;
            };
        };
        uint16_t sp;
        uint16_t bp;
        uint16_t si;
        uint16_t di;
    };
    struct
    {
        uint16_t es;
        uint16_t cs;
        uint16_t ss;
        uint16_t ds;
    };
    uint16_t ip;
    union
    {
        uint16_t flags;
        struct
        {
            uint8_t c: 1;
            uint8_t  : 1;
            uint8_t p: 1;
            uint8_t  : 1;
            uint8_t a: 1;
            uint8_t  : 1;
            uint8_t z: 1;
            uint8_t s: 1;
            uint8_t t: 1;
            uint8_t i: 1;
            uint8_t d: 1;
            uint8_t o: 1;
        };
    };
};
秉烛思 2025-01-04 12:14:36

为什么不像这样使用 structunion

union AX_R {
    AX_R() {
        AX = 0;
    }
    unsigned __int16 AX;
    struct {
        unsigned __int8 AL;
        unsigned __int8 AH;
    } _AX_R;
};

Why not use structure and union like this:

union AX_R {
    AX_R() {
        AX = 0;
    }
    unsigned __int16 AX;
    struct {
        unsigned __int8 AL;
        unsigned __int8 AH;
    } _AX_R;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文