NEMU PA1

solutions

  • the structure of registers
 // TODO: Re-organize the `CPU_state' structure to match the register
 // encoding scheme in i386 instruction format. For example, if we
 // access cpu.gpr[3]._16, we will get the `bx' register; if we access
 // cpu.gpr[1]._8[1], we will get the 'ch' register. Hint: Use `union'.
 // For more details about the register encoding scheme, see i386 manual.
 //
typedef struct {
    union {
        union {
            uint32_t _32;
            uint16_t _16;
            uint8_t _8[2];
        } gpr[8];

        /* Do NOT change the order of the GPRs' definitions. */
        struct {
            uint32_t eax, ecx, edx, ebx, esp, ebp, esi, edi;
        };
    };
    swaddr_t eip;

} CPU_state;

  • debug utilities
static int cmd_step(char *args) {
    cpu_exec(1);
    return 0;
}

static void dump_regs() {
    int i;
    for(i = R_EAX; i <= R_EDI; i ++) {
        printf("%s: 0x%08x\n", regsl[i], cpu.gpr[i]._32);
    }
    printf("eip: 0x%08x\n", cpu.eip);

}

static int cmd_info(char *args) {
    switch (*args) {
    case 'r': dump_regs(); return 0;
    default: return 1;
    }
}

static int cmd_dump_mem(char *args) {
    unsigned int addr, len, i;

    sscanf(args, "%d 0x%x", &len, &addr);
    printf("dump memory start addr: 0x%08x len: %d\n", addr, len);
    for (i = 0; i < len; ++i) {
        if (!(i & 0xf)) printf("\n0x%08x: ", addr + i * 16);
        printf("0x%02x ", *(unsigned char *)hwa_to_va(addr + i));
    }
    printf("\n");

    return 0;
}

static struct {
    char *name;
    char *description;
    int (*handler) (char *);
} cmd_table [] = {
    { "help", "Display informations about all supported commands", cmd_help },
    { "c", "Continue the execution of the program", cmd_c },
    { "q", "Exit NEMU", cmd_q },

    /* TODO: Add more commands */
    { "s", "Single step", cmd_step},
    { "info", "dump informations with option: r(registers)", cmd_info},
    { "x", "dump memory: x length addr", cmd_dump_mem},

};

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 很早就一直想玩opencv 但是无奈苹果电脑玩不起。 cv2.imshow() 永远失败,说配置有问题 今天终于在...
    98Future阅读 1,872评论 0 0
  • 01 认识魏徕是在一次朋友聚会上,他坐在我对面。和朋友谈笑风生中,他闯入了我的视线。他的表情是笑着的,但我从他眼神...
    七秒记忆的鱼55阅读 3,707评论 2 4
  • 三点收获。 一、 改变坏习惯的方法 大脑具有神经可塑性,我们做的每件事都会改变大脑的结构。而可塑性具有竞争本质——...
    四四四毛阅读 9,944评论 0 11
  • 世上没有完美的事,但是我们应该在自己能力范围内过得最好。成年人走入婚姻家庭,成为一个新的社会单位的主宰,要亲自处理...
    何初见阅读 3,244评论 0 5
  • 有人会问:真实是什么?走入社会还会有真实吗? 我觉得这个问题问的好,那得问你自己的心啊,你的心里在想什么! 你在外...
    口述笔录阅读 2,854评论 0 3