diff --git a/src/instructions.c b/src/instructions.c index 91fb36c..398f99c 100644 --- a/src/instructions.c +++ b/src/instructions.c @@ -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}, diff --git a/src/instructions.h b/src/instructions.h index 2f6b1f3..a37fd91 100644 --- a/src/instructions.h +++ b/src/instructions.h @@ -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); diff --git a/src/instructions/arithmetics.c b/src/instructions/arithmetics.c index f7bc4a3..503d6de 100644 --- a/src/instructions/arithmetics.c +++ b/src/instructions/arithmetics.c @@ -2,6 +2,23 @@ #include "../interpretor.h" #include +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);