diff --git a/1-2/8086coded b/1-2/8086coded index 0b5107d..96478de 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 6d7b66c..4791f4d 100644 --- a/1-2/decoder.c +++ b/1-2/decoder.c @@ -30,29 +30,29 @@ char REG_ENCODING_TXT[0b10000][REG_ENCODING_TXT_LEN] = { "al", "cl", "dl", "bl", char EA_ENCODING_TXT[0b1000][EA_ENCODING_TXT_LEN] = { "bx + si", "bx + di", "bp + si", "bp + di", "si", "di", "bp", "bx"}; -enum REG_ENCODING - { - R_AL = 0b000, - R_CL = 0b001, - R_DL = 0b010, - R_BL = 0b011, - R_AH = 0b100, - R_CH = 0b101, - R_DH = 0b110, - R_BH = 0b111 - }; +/* enum REG_ENCODING */ +/* { */ +/* R_AL = 0b000, */ +/* R_CL = 0b001, */ +/* R_DL = 0b010, */ +/* R_BL = 0b011, */ +/* R_AH = 0b100, */ +/* R_CH = 0b101, */ +/* R_DH = 0b110, */ +/* R_BH = 0b111 */ +/* }; */ -enum WREG_ENCODING - { - WR_AX = 0b000, - WR_CX = 0b001, - WR_DX = 0b010, - WR_BX = 0b011, - WR_SP = 0b100, - WR_BP = 0b101, - WR_SI = 0b110, - WR_DI = 0b111 - }; +/* enum WREG_ENCODING */ +/* { */ +/* WR_AX = 0b000, */ +/* WR_CX = 0b001, */ +/* WR_DX = 0b010, */ +/* WR_BX = 0b011, */ +/* WR_SP = 0b100, */ +/* WR_BP = 0b101, */ +/* WR_SI = 0b110, */ +/* WR_DI = 0b111 */ +/* }; */ typedef struct { @@ -63,6 +63,35 @@ typedef struct FILE *output; binary_data bin; +int MOV_I_T_R_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int bytes_read) +{ + const uint8_t wide_mask = 0b0000'1000; + const uint8_t reg_mask = 0b0000'0111; + unsigned char byte[1]; + uint8_t extra_bytes_read = 0; + uint16_t full_im = byte2; + uint8_t high_im = 0; + uint8_t reg_value = 0; + + //Immediate value retrieval + if (wide_mask & byte1) + { + fread(byte, sizeof(byte), 1, bin.binary); + extra_bytes_read++; + high_im = (uint8_t)byte[0]; + full_im = full_im + (high_im << 8); + } + + //target reg retrieval + reg_value = (byte1 & reg_mask) + (((byte1 & wide_mask) >> 3) * 8); + + printf("%s %s, %d\n", "mov", REG_ENCODING_TXT[reg_value], full_im); + fprintf(output, "%s %s, %d\n", "mov", REG_ENCODING_TXT[reg_value], full_im); + + return extra_bytes_read; +} + + 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; @@ -237,14 +266,27 @@ int main(int argc, char** argv) fread(byte, sizeof(byte), 1, bin.binary); uint8_t manip_inst = (uint8_t)byte[0]; uint8_t inst = (uint8_t)byte[0]; - //For now we're just checking for RM_TF_R + //We're checking for MOV RM_TF_R and MOV I_T_R manip_inst = (manip_inst >> 2); - if (manip_inst != MOV_RM_TF_R) break; - if (bytes_read >= bin.size) break; - fread(byte, sizeof(byte), 1, bin.binary); - bytes_read++; - uint8_t inst_byte2 = (uint8_t)byte[0]; - bytes_read += MOV_RM_TF_R_parse(inst, inst_byte2, 0, 0); + if (manip_inst == MOV_RM_TF_R) + { + if (bytes_read >= bin.size) break; + fread(byte, sizeof(byte), 1, bin.binary); + bytes_read++; + uint8_t inst_byte2 = (uint8_t)byte[0]; + bytes_read += MOV_RM_TF_R_parse(inst, inst_byte2, 0, 0); + continue; + } + manip_inst = (manip_inst >> 2); + if (manip_inst == MOV_I_T_R) + { + if (bytes_read >= bin.size) break; + fread(byte, sizeof(byte), 1, bin.binary); + bytes_read++; + uint8_t inst_byte2 = (uint8_t)byte[0]; + bytes_read += MOV_I_T_R_parse(inst, inst_byte2, 0, 0); + continue; + } } return 0; diff --git a/1-2/listing-39 b/1-2/listing-39 new file mode 100644 index 0000000..405b804 Binary files /dev/null and b/1-2/listing-39 differ diff --git a/1-2/listing-39.asm b/1-2/listing-39.asm new file mode 100644 index 0000000..ecce8c7 --- /dev/null +++ b/1-2/listing-39.asm @@ -0,0 +1,31 @@ +bits 16 + +; Register-to-register +mov si, bx +mov dh, al + +; 8-bit immediate-to-register +mov cl, 12 +mov ch, -12 + +; 16-bit immediate-to-register +mov cx, 12 +mov cx, -12 +mov dx, 3948 +mov dx, -3948 + +; Source address calculation +mov al, [bx + si] +mov bx, [bp + di] +mov dx, [bp] + +; Source address calculation plus 8-bit displacement +mov ah, [bx + si + 4] + +; Source address calculation plus 16-bit displacement +mov al, [bx + si + 4999] + +; Dest address calculation +mov [bx + di], cx +mov [bp + si], cl +mov [bp], ch diff --git a/1-2/output b/1-2/output new file mode 100644 index 0000000..405b804 Binary files /dev/null and b/1-2/output differ diff --git a/1-2/output.asm b/1-2/output.asm new file mode 100644 index 0000000..3f42057 --- /dev/null +++ b/1-2/output.asm @@ -0,0 +1,18 @@ +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