簡體   English   中英

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

[英]C: How do I simulate 8086 registers?

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};

有沒有人有任何好主意(或類似'最佳實踐')如何模擬這些寄存器?

我不認為有這樣做的“最佳實踐”方法,但你可以采取的一種方法可能會讓你更少瘋狂,就是使用一個聯合覆蓋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;
};

我將結構抽象出來並使用訪問器函數。

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

啟用內聯后,這種方法的性能不會低於使用聯合。

如果它可能對某人有用,我把我在這里寫的那個。

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;
        };
    };
};

為什么不像這樣使用structureunion

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM