我正在编写一个简单的chip8 模拟器。
我有一个名为 programCounter(PC) 的值。
问题是,一旦我从指令 1(修改 PC)返回,PC 将返回到被该方法修改之前的值。
例子
在指令 1 分配给 PC 之前,PC 的值为 203。
在指令 1 之后,PC 的值为 (0x0NNN & 0xFFFE)。
通过programCounter++,它返回203而不是增量。
#include <cstdint>
constexpr auto PROGRAMSTART = 0x200;
constexpr auto GRAPHICSTART = 0xF00;
constexpr auto GRAPHICEND = 0xFFF;
//Memory
static uint8_t memory[4096]; // 0x000-0xFFF -> 0000-4095
static uint16_t stack[16];
//General Purpose Registers
static uint8_t registers[16]; //Register V0,V1,..V9,VA,VB,..,VF
//Special Purpose Register
static uint8_t specialRegisters[2];
static uint8_t stackPointer;
static uint16_t registerI;
static uint16_t programCounter = PROGRAMSTART;
//Graphic
const int WIDTH = 64;
const int HEIGHT = 32;
const int beginnningOfGraphicMemory = 0xF00;
对于指令 1NNN("https://en.wikipedia.org/wiki/CHIP-8#Opcode_table"),它是一个简单的无条件跳转。
void Instruction1(uint16_t NNN)
{
programCounter = (NNN & 0xFFFE); //Keeps the PC counter aligned to memory
}
int main()
{
SetUpInterpreterText(0);
Test();
for (;;)
{
printf("Program Counter: %04x \n", programCounter);
uint16_t byte0 = (memory[programCounter++] << 8);
uint16_t byte1 = (memory[programCounter]);
uint16_t instruction = byte0 + byte1; //must load data in 16bit chunks
Decoder(instruction);
printf("Program Counter: %04x Data: %04x \n", programCounter, instruction);
programCounter++;
}
return 0;
}
void Decoder(uint16_t instruction)
{
uint16_t data = instruction & 0x0FFF; //removing the top 4 bits to make it easier
switch (instruction >> 12)
{
case 0:
Instruction0(data);
break;
case 1:
Instruction1(data);
break;
default:
std::cout << "Instruction Not Foud" << std::endl;
exit(EXIT_FAILURE);
break;
}
}
解码器所做的只是删除 16 位指令的指令的前 4 位。例如,0x1234 被发送到 1NNN 指令/方法,234 代表指令的 NNN 部分。
我反汇编了程序,根据汇编,PC 被存储在内存中,一旦我到达“programCounter++;” 它从记忆中恢复。但是,对于指令 1,它不会将其权限写入寄存器 EAX 的内存。
我应该怎么做才能让编译器知道我希望它在我为其赋值时更新 PC 的内存位置,而不仅仅是更新寄存器?
PS 我尝试过编写内联汇编,但我对 x86 汇编不太熟练。我似乎无法将 register_EAX 移动到值的内存位置,因此我可以强制更新值,因为 EAX 具有正确的值,直到 PC 递增。