From d4ec188ef1d6bae7fe56075712009bbc2088e446 Mon Sep 17 00:00:00 2001 From: tuz358 Date: Mon, 5 Mar 2018 08:34:42 +0900 Subject: [PATCH] Add add_rm32_r32(0x01) instruction --- include/instructions.h | 1 + instructions.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/include/instructions.h b/include/instructions.h index 2510334..2373cc4 100644 --- a/include/instructions.h +++ b/include/instructions.h @@ -23,6 +23,7 @@ public: void calc_modrm(); void execute_opcode(uint8_t opcode); + void add_rm32_r32(); // 0x01 void xor_rm32_r32(); // 0x31 void mov_rm32_r32(); // 0x89 void nop(); // 0x90 diff --git a/instructions.cpp b/instructions.cpp index ce9ba1f..511be6d 100644 --- a/instructions.cpp +++ b/instructions.cpp @@ -20,6 +20,7 @@ void Instructions::init(uint32_t eip, uint32_t esp, Memory memory){ void Instructions::init_instructions(){ memset(this->instructions, 0, sizeof(this->instructions)); + this->instructions[0x01] = &Instructions::add_rm32_r32; this->instructions[0x31] = &Instructions::xor_rm32_r32; this->instructions[0x89] = &Instructions::mov_rm32_r32; this->instructions[0x90] = &Instructions::nop; @@ -46,6 +47,56 @@ void Instructions::execute_opcode(uint8_t opcode){ (this->*instructions[opcode])(); } +void Instructions::add_rm32_r32(){ + printf("add_rm32_r32 called.\n"); + uint32_t addr, dst, imm32; + uint8_t imm8; + + this->modrm = memory.read_uint8(this->eip); + this->calc_modrm(); + + switch (this->mod) { + case 0: + // add [M], R + // addr : M + this->eip++; + addr = this->registers[this->M]; + // dst : data of [M] + dst = memory.read_uint32(addr); + memory.write_uint32(addr, dst + this->registers[this->R]); + break; + case 1: + // add [M+imm8], R + this->eip++; + imm8 = memory.read_uint8(this->eip); + // addr : M + addr = this->registers[this->M]; + // dst : data of [M+imm8] + dst = memory.read_uint32(addr + imm8); + memory.write_uint32(addr + imm8, dst + this->registers[this->R]); + this->eip++; + break; + case 2: + // add [M+imm32], R + this->eip++; + imm32 = memory.read_uint32(this->eip); + imm32 = swap_endian32(imm32); + // addr : M + addr = this->registers[this->M]; + // dst : data of [M+imm32] + dst = memory.read_uint32(addr + imm32); // error + memory.write_uint32(addr, dst + this->registers[this->R]); + this->eip += 4; + break; + default: + // case mod == 3 + // add M, R + this->eip++; + this->registers[this->M] += this->registers[this->R]; + break; + } +} + void Instructions::xor_rm32_r32(){ printf("xor_rm32_r32 called.\n"); uint32_t addr, dst, imm32;