Updated decode_register to use flat array instead of branches

This commit is contained in:
Abdelrahman Said 2023-03-05 13:58:12 +00:00
parent d683951f67
commit d4733b7be7

View File

@ -96,64 +96,23 @@ uint16_t decode_mode(uint16_t instruction) {
}
void decode_register(uint16_t instruction, bool word, char *dest) {
uint16_t reg_mask = 0x07;
static uint16_t reg_mask = 0x07;
switch (instruction & reg_mask) {
case 0x00:
if (word) {
strcpy(dest, "ax");
} else {
strcpy(dest, "al");
}
break;
case 0x01:
if (word) {
strcpy(dest, "cx");
} else {
strcpy(dest, "cl");
}
break;
case 0x02:
if (word) {
strcpy(dest, "dx");
} else {
strcpy(dest, "dl");
}
break;
case 0x03:
if (word) {
strcpy(dest, "bx");
} else {
strcpy(dest, "bl");
}
break;
case 0x04:
if (word) {
strcpy(dest, "sp");
} else {
strcpy(dest, "ah");
}
break;
case 0x05:
if (word) {
strcpy(dest, "bp");
} else {
strcpy(dest, "ch");
}
break;
case 0x06:
if (word) {
strcpy(dest, "si");
} else {
strcpy(dest, "dh");
}
break;
case 0x07:
if (word) {
strcpy(dest, "di");
} else {
strcpy(dest, "bh");
}
break;
}
// clang-format off
static const char *table[16] = {
"al", "ax",
"cl", "cx",
"dl", "dx",
"bl", "bx",
"ah", "sp",
"ch", "bp",
"dh", "si",
"bh", "di"
};
// clang-format on
uint16_t offset = instruction & reg_mask;
// Multiply offset by 2 since each row has 2 columns
strcpy(dest, table[offset * 2 + (uint16_t)word]);
}