From 8a0268317fbce2baa2e5119c796750ea6c80a813 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 18 Oct 2017 02:27:56 -0700 Subject: 4084 subx: extract helpers for 'push' and 'pop'. We will be using them in 'call' and 'ret' as well. --- subx/011direct_addressing.cc | 30 ++++++++++++++++++------------ subx/012indirect_addressing.cc | 19 ++++++------------- 2 files changed, 24 insertions(+), 25 deletions(-) (limited to 'subx') diff --git a/subx/011direct_addressing.cc b/subx/011direct_addressing.cc index b3f234b3..9e5f6e80 100644 --- a/subx/011direct_addressing.cc +++ b/subx/011direct_addressing.cc @@ -223,9 +223,8 @@ case 0x89: { // copy r32 to r/m32 # op ModRM SIB displacement immediate 53 # push EBX (reg 3) to stack +run: push reg 3 ++run: decrementing ESP to 0x00000060 +run: pushing value 0x0000000a -+run: ESP is now 0x00000060 -+run: contents at ESP: 0x0000000a :(before "End Single-Byte Opcodes") case 0x50: @@ -238,13 +237,15 @@ case 0x56: case 0x57: { // push r32 to stack uint8_t reg = op & 0x7; trace(2, "run") << "push reg " << NUM(reg) << end(); - const int32_t val = Reg[reg].u; - trace(2, "run") << "pushing value 0x" << HEXWORD << val << end(); + push(Reg[reg].u); + break; +} +:(code) +void push(uint32_t val) { Reg[ESP].u -= 4; + trace(2, "run") << "decrementing ESP to 0x" << HEXWORD << Reg[ESP].u << end(); + trace(2, "run") << "pushing value 0x" << HEXWORD << val << end(); *reinterpret_cast(&Mem.at(Reg[ESP].u)) = val; - trace(2, "run") << "ESP is now 0x" << HEXWORD << Reg[ESP].u << end(); - trace(2, "run") << "contents at ESP: 0x" << HEXWORD << *reinterpret_cast(&Mem.at(Reg[ESP].u)) << end(); - break; } //:: pop @@ -256,7 +257,7 @@ case 0x57: { // push r32 to stack 5b # pop stack to EBX (reg 3) +run: pop into reg 3 +run: popping value 0x0000000a -+run: ESP is now 0x00000064 ++run: incrementing ESP to 0x00000064 :(before "End Single-Byte Opcodes") case 0x58: @@ -269,9 +270,14 @@ case 0x5e: case 0x5f: { // pop stack into r32 uint8_t reg = op & 0x7; trace(2, "run") << "pop into reg " << NUM(reg) << end(); - Reg[reg].u = *reinterpret_cast(&Mem.at(Reg[ESP].u)); - trace(2, "run") << "popping value 0x" << HEXWORD << Reg[reg].u << end(); - Reg[ESP].u += 4; - trace(2, "run") << "ESP is now 0x" << HEXWORD << Reg[ESP].u << end(); + Reg[reg].u = pop(); break; } +:(code) +uint32_t pop() { + uint32_t result = *reinterpret_cast(&Mem.at(Reg[ESP].u)); + trace(2, "run") << "popping value 0x" << HEXWORD << result << end(); + Reg[ESP].u += 4; + trace(2, "run") << "incrementing ESP to 0x" << HEXWORD << Reg[ESP].u << end(); + return result; +} diff --git a/subx/012indirect_addressing.cc b/subx/012indirect_addressing.cc index 37d82a3f..f4e8c665 100644 --- a/subx/012indirect_addressing.cc +++ b/subx/012indirect_addressing.cc @@ -349,18 +349,14 @@ case 0xff: { // jump to r/m32 ff 30 # push *EAX (reg 0) to stack +run: push effective address +run: effective address is mem at address 0x60 (reg 0) -+run: ESP is now 0x00000010 -+run: contents at ESP: 0x000000af ++run: decrementing ESP to 0x00000010 ++run: pushing value 0x000000af :(before "End Op ff Subops") case 6: { trace(2, "run") << "push effective address" << end(); const int32_t* val = effective_address(modrm); - trace(2, "run") << "pushing value 0x" << HEXWORD << *val << end(); - Reg[ESP].u -= 4; - *reinterpret_cast(&Mem.at(Reg[ESP].u)) = *val; - trace(2, "run") << "ESP is now 0x" << HEXWORD << Reg[ESP].u << end(); - trace(2, "run") << "contents at ESP: 0x" << HEXWORD << *reinterpret_cast(&Mem.at(Reg[ESP].u)) << end(); + push(*val); break; } @@ -374,8 +370,8 @@ case 6: { 8f 00 # pop stack into *EAX (reg 0) +run: pop into effective address +run: effective address is mem at address 0x60 (reg 0) -+run: storing 0x00000030 -+run: ESP is now 0x00000014 ++run: popping value 0x00000030 ++run: incrementing ESP to 0x00000014 :(before "End Single-Byte Opcodes") case 0x8f: { // pop stack into r/m32 @@ -385,10 +381,7 @@ case 0x8f: { // pop stack into r/m32 case 0: { trace(2, "run") << "pop into effective address" << end(); int32_t* dest = effective_address(modrm); - *dest = *reinterpret_cast(&Mem.at(Reg[ESP].u)); - trace(2, "run") << "storing 0x" << HEXWORD << *dest << end(); - Reg[ESP].u += 4; - trace(2, "run") << "ESP is now 0x" << HEXWORD << Reg[ESP].u << end(); + *dest = pop(); break; } } -- cgit 1.4.1-2-gfad0