Add imediate memory support

This commit is contained in:
2023-05-31 16:21:22 +09:00
parent e9b2a773ac
commit 3e9df76700
3 changed files with 44 additions and 22 deletions
+23 -5
View File
@@ -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;
+16 -17
View File
@@ -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}
};
+5
View File
@@ -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);