1-2: Assignment complete w/o challenge

This commit is contained in:
Hane 2026-01-25 00:49:23 +01:00
commit 1a7041ab9d
6 changed files with 120 additions and 29 deletions

View file

@ -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;