diff --git a/1-2/8086coded b/1-2/8086coded index e681639..6631f53 100755 Binary files a/1-2/8086coded and b/1-2/8086coded differ diff --git a/1-2/decoder.c b/1-2/decoder.c index 821731d..07e87f1 100644 --- a/1-2/decoder.c +++ b/1-2/decoder.c @@ -63,7 +63,7 @@ char WORD_SIGNAL_TXT[0b10][WORD_SIGNAL_LEN] = { "byte ", "word " }; typedef struct { FILE *binary; - uint64_t size; + uint64_t size; } binary_data; FILE *output; @@ -160,9 +160,19 @@ void fill_ea_string(output_string *str, char* content, int16_t displacement) { if (strlen(content) > 0) { - memcpy(str->string, " + ", 3); - str->string += 3; - str->len += 3; + *(str->string) = ' '; + if(displacement >= 0) + { + *(str->string + 1) = '+'; + *(str->string + 2) = ' '; + str->string += 3; + str->len += 3; + } + else + { + str->string += 1; + str->len += 1; + } } disp_len = sprintf(str->string, "%d", displacement); str->string += disp_len; @@ -296,11 +306,23 @@ int MOV_I_T_RM_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int byte fprintf(output, "%s %s, %s%d\n", "mov", mem_data.string, WORD_SIGNAL_TXT[is_wide], wide_data); break; } - return extra_bytes_read; } +int16_t calc_effective_disp(uint8_t high_order, uint8_t low_order, bool lo_only) +{ + const uint8_t low_order_neg_mask = 0b1000'0000; + int16_t effective_disp = 0; + + if (lo_only & ((low_order & low_order_neg_mask) >> 7)) + high_order = high_order | 0b1111'1111; + int8_t *effective_disp_high_order =(int8_t*) &(effective_disp) + 1; + memcpy(&(effective_disp), &low_order, 1); + memcpy(effective_disp_high_order, &high_order, 1); + return effective_disp; +} + int MOV_RM_TF_R_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int bytes_read) { const uint8_t wide_mask = 0b0000'0001; @@ -312,8 +334,13 @@ int MOV_RM_TF_R_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int byt uint8_t low_disp = 0; uint8_t high_disp = 0; uint16_t wide_disp = 0; + int16_t effective_disp = 0; uint8_t extra_bytes_read = 0; + output_string mem_data; + mem_data.len = 0; + mem_data.string = calloc(18, sizeof(char)); //"[bp + di + 65535]\0" + bool reg_is_dest = (byte1 & dest_mask); uint8_t inst_mod = (byte2 & mod_mask) >> 6; @@ -342,28 +369,22 @@ int MOV_RM_TF_R_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int byt fread(byte, sizeof(byte), 1, bin.binary); extra_bytes_read++; low_disp = (uint8_t)byte[0]; + reg_value = ((byte2 & regm_mask) >> 3) + ((byte1 & wide_mask) * 8); ea_table_value = (byte2 & (regm_mask >> 3)); - + effective_disp = calc_effective_disp(0, low_disp, true); + //EA STRING GENERATION - *ea_string_write_ptr = '['; - ea_string_write_ptr++; - memcpy(ea_string_write_ptr, EA_ENCODING_TXT[ea_table_value], strlen(EA_ENCODING_TXT[ea_table_value])); - ea_string_write_ptr += strlen(EA_ENCODING_TXT[ea_table_value]); - memcpy(ea_string_write_ptr, " + ", 3); - ea_string_write_ptr += 3; - disp_len = sprintf(ea_string_write_ptr, "%d", low_disp); - ea_string_write_ptr += disp_len; - *ea_string_write_ptr = ']'; + fill_ea_string(&mem_data, EA_ENCODING_TXT[ea_table_value], effective_disp); //END printf("%s %s, %s\n", "mov", (reg_is_dest ? REG_ENCODING_TXT[reg_value] - : ea_string) - , (reg_is_dest ? ea_string + : mem_data.string) + , (reg_is_dest ? mem_data.string : REG_ENCODING_TXT[reg_value])); fprintf(output, "%s %s, %s\n", "mov", (reg_is_dest ? REG_ENCODING_TXT[reg_value] - : ea_string) - , (reg_is_dest ? ea_string + : mem_data.string) + , (reg_is_dest ? mem_data.string : REG_ENCODING_TXT[reg_value])); break; case (MEM_16BIT_DISP): @@ -375,30 +396,22 @@ int MOV_RM_TF_R_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int byt high_disp = (uint8_t)byte[0]; //Composing wide displacement - wide_disp = high_disp << 8; - wide_disp = wide_disp | low_disp; + effective_disp = calc_effective_disp(high_disp, low_disp, false); + reg_value = ((byte2 & regm_mask) >> 3) + ((byte1 & wide_mask) * 8); ea_table_value = (byte2 & (regm_mask >> 3)); //EA STRING GENERATION - *ea_string_write_ptr = '['; - ea_string_write_ptr++; - memcpy(ea_string_write_ptr, EA_ENCODING_TXT[ea_table_value], strlen(EA_ENCODING_TXT[ea_table_value])); - ea_string_write_ptr += strlen(EA_ENCODING_TXT[ea_table_value]); - memcpy(ea_string_write_ptr, " + ", 3); - ea_string_write_ptr += 3; - disp_len = sprintf(ea_string_write_ptr, "%d", wide_disp); - ea_string_write_ptr += disp_len; - *ea_string_write_ptr = ']'; + fill_ea_string(&mem_data, EA_ENCODING_TXT[ea_table_value], effective_disp); //END printf("%s %s, %s\n", "mov", (reg_is_dest ? REG_ENCODING_TXT[reg_value] - : ea_string) - , (reg_is_dest ? ea_string + : mem_data.string) + , (reg_is_dest ? mem_data.string : REG_ENCODING_TXT[reg_value])); fprintf(output, "%s %s, %s\n", "mov", (reg_is_dest ? REG_ENCODING_TXT[reg_value] - : ea_string) - , (reg_is_dest ? ea_string + : mem_data.string) + , (reg_is_dest ? mem_data.string : REG_ENCODING_TXT[reg_value])); break; case (MEM_NO_DISP): diff --git a/1-2/listing-39.asm b/1-2/listing-39.asm index ecce8c7..58038ce 100644 --- a/1-2/listing-39.asm +++ b/1-2/listing-39.asm @@ -25,7 +25,8 @@ mov ah, [bx + si + 4] ; Source address calculation plus 16-bit displacement mov al, [bx + si + 4999] -; Dest address calculation + ; Dest address calculation + mov [bx + di], cx mov [bp + si], cl mov [bp], ch diff --git a/1-2/listing-40 b/1-2/listing-40 index 63b3ec5..13c1744 100644 Binary files a/1-2/listing-40 and b/1-2/listing-40 differ diff --git a/1-2/listing-40.asm b/1-2/listing-40.asm index bd8aa5f..d825a11 100644 --- a/1-2/listing-40.asm +++ b/1-2/listing-40.asm @@ -1,10 +1,17 @@ bits 16 ; Signed displacements +mov ax, [bx - 100] +mov ax, [bx - 1000] +mov ax, [bx - 3] +mov ax, [bx + 3] +mov ax, [bx + 200] +mov ax, [bx + 2000] +mov ax, [bx + di - 40000] mov ax, [bx + di - 37] mov [si - 300], cx mov dx, [bx - 32] - + ; Explicit sizes mov [bp + di], byte 7 mov [di + 901], word 347 @@ -16,7 +23,7 @@ mov bx, [3458] ; Memory-to-accumulator test mov ax, [2555] mov ax, [16] - + ; Accumulator-to-memory test mov [2554], ax mov [15], ax diff --git a/1-2/output b/1-2/output index 405b804..13c1744 100644 Binary files a/1-2/output and b/1-2/output differ diff --git a/1-2/output.asm b/1-2/output.asm index 3f42057..2a6f91f 100644 --- a/1-2/output.asm +++ b/1-2/output.asm @@ -1,18 +1,16 @@ bits 16 -mov si, bx -mov dh, al -mov cl, 12 -mov ch, 244 -mov cx, 12 -mov cx, 65524 -mov dx, 3948 -mov dx, 61588 -mov al, [bx + si] -mov bx, [bp + di] -mov dx, [bp + 0] -mov ah, [bx + si + 4] -mov al, [bx + si + 4999] -mov [bx + di], cx -mov [bp + si], cl -mov [bp + 0], ch +mov ax, [bx + 200] +mov ax, [bx + 2000] +mov ax, [bx + di + 25536] +mov ax, [bx + di -37] +mov [si -300], cx +mov dx, [bx -32] +mov [bp + di], byte 7 +mov [di + 901], word 347 +mov bp, [5] +mov bx, [3458] +mov ax, [2555] +mov ax, [16] +mov [2554], ax +mov [15], ax