Implement add

This commit is contained in:
2023-07-09 13:08:09 +09:00
parent e99e054e24
commit bfc84ce2aa
3 changed files with 27 additions and 9 deletions
+9 -9
View File
@@ -24,12 +24,12 @@ instruction_t parse_inst(u_int8_t *binary, unsigned long size)
}
const instruction_t instructions[] = {
{.opcode = 0x00, .extended = -1, .name = "add", .mode = {R_M8, REG8, END}, .exec = NULL},
{.opcode = 0x01, .extended = -1, .name = "add", .mode = {R_M16, REG16, END}, .exec = NULL},
{.opcode = 0x02, .extended = -1, .name = "add", .mode = {REG8, R_M8, END}, .exec = NULL},
{.opcode = 0x03, .extended = -1, .name = "add", .mode = {REG16, R_M16, END}, .exec = NULL},
{.opcode = 0x04, .extended = -1, .name = "add", .mode = {OPREG8, IMM8, END}, .exec = NULL},
{.opcode = 0x05, .extended = -1, .name = "add", .mode = {OPREG16, IMM16, END}, .exec = NULL},
{.opcode = 0x00, .extended = -1, .name = "add", .mode = {R_M8, REG8, END}, .exec = &add},
{.opcode = 0x01, .extended = -1, .name = "add", .mode = {R_M16, REG16, END}, .exec = &add},
{.opcode = 0x02, .extended = -1, .name = "add", .mode = {REG8, R_M8, END}, .exec = &add},
{.opcode = 0x03, .extended = -1, .name = "add", .mode = {REG16, R_M16, END}, .exec = &add},
{.opcode = 0x04, .extended = -1, .name = "add", .mode = {OPREG8, IMM8, END}, .exec = &add},
{.opcode = 0x05, .extended = -1, .name = "add", .mode = {OPREG16, IMM16, END}, .exec = &add},
{.opcode = 0x06, .extended = -1, .name = "push es", .mode = {END}, .exec = &push},
{.opcode = 0x07, .extended = -1, .name = "pop es", .mode = {END}, .exec = NULL},
{.opcode = 0x08, .extended = -1, .name = "or", .mode = {R_M8, REG8, END}, .exec = NULL},
@@ -258,7 +258,7 @@ const instruction_t instructions[] = {
const instruction_t extended[][8] = {
// 0x80 extended
{
{.opcode = 0x00, .extended = -2, .name = "add", .mode = {R_M8, IMM8, END}, .exec = NULL},
{.opcode = 0x00, .extended = -2, .name = "add", .mode = {R_M8, IMM8, END}, .exec = &add},
{.opcode = 0x01, .extended = -2, .name = "or", .mode = {R_M8, IMM8, END}, .exec = NULL},
{.opcode = 0x02, .extended = -2, .name = "adc", .mode = {R_M8, IMM8, END}, .exec = NULL},
{.opcode = 0x03, .extended = -2, .name = "sbb", .mode = {R_M8, IMM8, END}, .exec = NULL},
@@ -269,7 +269,7 @@ const instruction_t extended[][8] = {
},
// 0x81 extended
{
{.opcode = 0x00, .extended = -2, .name = "add", .mode = {R_M16, IMM16, END}, .exec = NULL},
{.opcode = 0x00, .extended = -2, .name = "add", .mode = {R_M16, IMM16, END}, .exec = &add},
{.opcode = 0x01, .extended = -2, .name = "or", .mode = {R_M16, IMM16, END}, .exec = NULL},
{.opcode = 0x02, .extended = -2, .name = "adc", .mode = {R_M16, IMM16, END}, .exec = NULL},
{.opcode = 0x03, .extended = -2, .name = "sbb", .mode = {R_M16, IMM16, END}, .exec = NULL},
@@ -280,7 +280,7 @@ const instruction_t extended[][8] = {
},
// 0x83 extended
{
{.opcode = 0x00, .extended = -2, .name = "add", .mode = {R_M16, IMM8, END}, .exec = NULL},
{.opcode = 0x00, .extended = -2, .name = "add", .mode = {R_M16, IMM8, END}, .exec = &add},
/**/{.opcode = 0x01, .extended = -2, .name = "invalid", .mode = {END}, .exec = NULL},
{.opcode = 0x02, .extended = -2, .name = "adc", .mode = {R_M16, IMM8, END}, .exec = NULL},
{.opcode = 0x03, .extended = -2, .name = "sbb", .mode = {R_M16, IMM8, END}, .exec = NULL},
+1
View File
@@ -10,5 +10,6 @@ void jmp(const instruction_t *self, state_t *state);
void int_inst(const instruction_t *self, state_t *state);
void lea(const instruction_t *self, state_t *state);
void add(const instruction_t *self, state_t *state);
void sub(const instruction_t *self, state_t *state);
void xor_inst(const instruction_t *self, state_t *state);
+17
View File
@@ -2,6 +2,23 @@
#include "../interpretor.h"
#include <stdint.h>
void add(const instruction_t *self, state_t *state)
{
operand_t from = get_operand(self, 0, state);
operand_t to = get_operand(self, 1, state);
unsigned old = read_op(from);
unsigned minus = read_op(to);
unsigned value = old + minus;
write_op(from, value);
state->of = value != read_op(from);
state->cf = value >= (is_operand_wide(self, 0) ? 0x10000 : 0x100);
state->sf = value & (is_operand_wide(self, 0) ? 0x8000 : 0x80);
state->zf = value == 0;
// TODO: Set AF and PF
}
void sub(const instruction_t *self, state_t *state)
{
operand_t from = get_operand(self, 0, state);