From bfba6cd70990066c41747ea4ee03aa009f8bbce8 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 27 Apr 2015 12:33:20 -0500 Subject: [PATCH] Make ASMInstruction's bytes an array. --- src/assembler.c | 10 ++-------- src/assembler/state.c | 3 ++- src/assembler/state.h | 4 ++-- src/assembler/tokenizer.c | 5 +++-- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/assembler.c b/src/assembler.c index 98e93d0..ed1024a 100644 --- a/src/assembler.c +++ b/src/assembler.c @@ -90,14 +90,8 @@ static ErrorInfo* resolve_symbols(AssemblerState *state) return ei; } - if (inst->loc.length == 3) { - inst->b2 = symbol->offset & 0xFF; - inst->b3 = symbol->offset >> 8; - } else { - inst->b3 = symbol->offset & 0xFF; - inst->b4 = symbol->offset >> 8; - } - + inst->bytes[inst->loc.length - 2] = symbol->offset & 0xFF; + inst->bytes[inst->loc.length - 1] = symbol->offset >> 8; free(inst->symbol); inst->symbol = NULL; } diff --git a/src/assembler/state.c b/src/assembler/state.c index 8466a89..8d31fc8 100644 --- a/src/assembler/state.c +++ b/src/assembler/state.c @@ -88,6 +88,7 @@ void asm_instructions_free(ASMInstruction *inst) { while (inst) { ASMInstruction *temp = inst->next; + free(inst->bytes); if (inst->symbol) free(inst->symbol); free(inst); @@ -102,7 +103,7 @@ void asm_data_free(ASMData *data) { while (data) { ASMData *temp = data->next; - free(data->data); + free(data->bytes); free(data); data = temp; } diff --git a/src/assembler/state.h b/src/assembler/state.h index 95a1af0..c2f6f40 100644 --- a/src/assembler/state.h +++ b/src/assembler/state.h @@ -40,7 +40,7 @@ typedef struct { struct ASMInstruction { ASMLocation loc; - uint8_t b1, b2, b3, b4; + uint8_t *bytes; char *symbol; const ASMLine *line; struct ASMInstruction *next; @@ -49,7 +49,7 @@ typedef struct ASMInstruction ASMInstruction; struct ASMData { ASMLocation loc; - uint8_t *data; + uint8_t *bytes; struct ASMData *next; }; typedef struct ASMData ASMData; diff --git a/src/assembler/tokenizer.c b/src/assembler/tokenizer.c index c6eed98..30125dc 100644 --- a/src/assembler/tokenizer.c +++ b/src/assembler/tokenizer.c @@ -155,7 +155,7 @@ static ErrorInfo* parse_data( data->loc.offset = offset; data->loc.length = 6; - data->data = (uint8_t*) strdup("foobar"); + data->bytes = (uint8_t*) strdup("foobar"); data->next = NULL; *data_ptr = data; @@ -182,7 +182,8 @@ static ErrorInfo* parse_instruction( inst->loc.offset = offset; inst->loc.length = 1; - inst->b1 = 0x3C; + uint8_t tmp = 0x3C; + inst->bytes = memcpy(malloc(1), &tmp, 1); inst->symbol = NULL; inst->line = line; inst->next = NULL;