From 3e9df7670078bf3b3f9ab02ed0a096bf4285d95f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Wed, 31 May 2023 16:21:22 +0900 Subject: [PATCH] Add imediate memory support --- src/dasm.c | 28 +++++++++++++++++++++++----- src/instructions.c | 33 ++++++++++++++++----------------- src/main.c | 5 +++++ 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/dasm.c b/src/dasm.c index e70c255..8c134e1 100644 --- a/src/dasm.c +++ b/src/dasm.c @@ -4,7 +4,20 @@ void print_instruction(unsigned addr, instruction_t inst, unsigned raw) { - printf("%04x: %-14x %s\n", addr, raw, inst.name); + printf("%04x: %-14x %s", addr, raw, inst.name); + for (int i = 0; inst.mode[i] != END; i++) { + switch (inst.mode[i]) { + case IMM8: + printf(", %02x", raw & 0xFF); + break; + case IMM16: + printf(", %04x", raw & 0xFFFF); + break; + default: + printf(", undefined"); + } + } + printf("\n"); } unsigned read_size(u_int8_t *binary, unsigned size) @@ -12,7 +25,8 @@ unsigned read_size(u_int8_t *binary, unsigned size) unsigned ret = binary[0]; for (unsigned i = 1; i < size; i++) { - ret |= binary[i] << (8 * i); + ret <<= 8; + ret |= binary[i]; } return ret; } @@ -30,7 +44,7 @@ instruction_t parse_inst(u_int8_t *binary) int dasm(u_int8_t *binary, unsigned long size) { - int pc = 0; + unsigned long pc = 0; int header_size = 0; if (binary[0] == 0xEB && binary[1] == 0x0E) @@ -38,11 +52,15 @@ int dasm(u_int8_t *binary, unsigned long size) else if (binary[0] == 0x01 && binary[1] == 0x03) header_size = binary[4]; - printf("Header size: %d\n", header_size); binary += header_size; + size -= header_size; - while (pc < size - header_size) { + while (pc < size) { instruction_t inst = parse_inst(binary); + if (pc + inst.size > size) { + printf("Invalid file. Missing operand for instruction: '%s'. (pc: %lx)\n", inst.name, pc); + return 1; + } unsigned long raw = read_size(binary, inst.size); print_instruction(pc, inst, raw); pc += inst.size; diff --git a/src/instructions.c b/src/instructions.c index 50642d8..144c2d4 100644 --- a/src/instructions.c +++ b/src/instructions.c @@ -3,22 +3,21 @@ const instruction_t instructions[] = { {.opcode = 0x00, .name = "add", .mode = {R_M8, REG8, END}, .size = 2}, - {.opcode = 0xBB, .name = "mov bf", .mode = {R_M8, REG8, END}, .size = 2}, - {.opcode = 0xB0, .name = "mov al", .mode = {IMM8}, .size = 2}, - {.opcode = 0xB1, .name = "mov cl", .mode = {IMM8}, .size = 2}, - {.opcode = 0xB2, .name = "mov dl", .mode = {IMM8}, .size = 2}, - {.opcode = 0xB3, .name = "mov bl", .mode = {IMM8}, .size = 2}, - {.opcode = 0xB4, .name = "mov ah", .mode = {IMM8}, .size = 2}, - {.opcode = 0xB5, .name = "mov ch", .mode = {IMM8}, .size = 2}, - {.opcode = 0xB6, .name = "mov dh", .mode = {IMM8}, .size = 2}, - {.opcode = 0xB7, .name = "mov bh", .mode = {IMM8}, .size = 2}, - {.opcode = 0xB8, .name = "mov ax", .mode = {IMM16}, .size = 3}, - {.opcode = 0xB9, .name = "mov cx", .mode = {IMM16}, .size = 3}, - {.opcode = 0xBA, .name = "mov dx", .mode = {IMM16}, .size = 3}, - {.opcode = 0xBB, .name = "mov bx", .mode = {IMM16}, .size = 3}, - {.opcode = 0xBC, .name = "mov sp", .mode = {IMM16}, .size = 3}, - {.opcode = 0xBD, .name = "mov bp", .mode = {IMM16}, .size = 3}, - {.opcode = 0xBE, .name = "mov si", .mode = {IMM16}, .size = 3}, - {.opcode = 0xBF, .name = "mov di", .mode = {IMM16}, .size = 3}, + {.opcode = 0xB0, .name = "mov al", .mode = {IMM8, END}, .size = 2}, + {.opcode = 0xB1, .name = "mov cl", .mode = {IMM8, END}, .size = 2}, + {.opcode = 0xB2, .name = "mov dl", .mode = {IMM8, END}, .size = 2}, + {.opcode = 0xB3, .name = "mov bl", .mode = {IMM8, END}, .size = 2}, + {.opcode = 0xB4, .name = "mov ah", .mode = {IMM8, END}, .size = 2}, + {.opcode = 0xB5, .name = "mov ch", .mode = {IMM8, END}, .size = 2}, + {.opcode = 0xB6, .name = "mov dh", .mode = {IMM8, END}, .size = 2}, + {.opcode = 0xB7, .name = "mov bh", .mode = {IMM8, END}, .size = 2}, + {.opcode = 0xB8, .name = "mov ax", .mode = {IMM16, END}, .size = 3}, + {.opcode = 0xB9, .name = "mov cx", .mode = {IMM16, END}, .size = 3}, + {.opcode = 0xBA, .name = "mov dx", .mode = {IMM16, END}, .size = 3}, + {.opcode = 0xBB, .name = "mov bx", .mode = {IMM16, END}, .size = 3}, + {.opcode = 0xBC, .name = "mov sp", .mode = {IMM16, END}, .size = 3}, + {.opcode = 0xBD, .name = "mov bp", .mode = {IMM16, END}, .size = 3}, + {.opcode = 0xBE, .name = "mov si", .mode = {IMM16, END}, .size = 3}, + {.opcode = 0xBF, .name = "mov di", .mode = {IMM16, END}, .size = 3}, {.name = NULL} }; diff --git a/src/main.c b/src/main.c index 981d7b9..968bb93 100644 --- a/src/main.c +++ b/src/main.c @@ -44,6 +44,11 @@ int main(int argc, char **argv) puts("Could not read binary file."); return 2; } + // Smaller than the header size. + if (size < 32) { + puts("Binary too small."); + return 2; + } int ret = dasm(binary, size); free(binary);