First assigment done (w/o writing to file)

This commit is contained in:
Hane 2026-01-23 20:25:14 +01:00
commit 53463a5c6d
8 changed files with 129 additions and 0 deletions

BIN
1-1/8086coded Executable file

Binary file not shown.

3
1-1/compile.sh Executable file
View file

@ -0,0 +1,3 @@
#! /usr/bin/env bash
gcc 1-1/decoder.c -O0 -g -gdwarf -o 1-1/8086coded

104
1-1/decoder.c Normal file
View file

@ -0,0 +1,104 @@
#include <stdint.h>
#include <stdio.h>
#include <limits.h>
enum DT_INSTRUCTIONS
{
MOV_RM_TF_R = 0b1000'10, //d w
MOV_I_T_RM = 0b1100'011, // w
MOV_I_T_R = 0b1011, // w reg
MOV_M_T_A = 0b1010'000, // w
MOV_A_T_M = 0b1010'001, // w
MOV_RM_T_SR = 0b1000'1110,
MOV_SR_T_RM = 0b1000'1100,
};
enum MOV_REGISTER_MODE
{
MEM_NO_DISP = 0b00,
MEM_8BIT_DISP = 0b01,
MEM_16BIT_DISP = 0b010,
REG_NO_DISP = 0b11
};
char REG_ENCODING_TXT[0b10000][3] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"};
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
};
typedef struct
{
FILE *binary;
uint64_t size;
} binary_data;
void MOV_RM_TF_R_parse(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
{
const uint8_t wide_mask = 0b0000'0001;
const uint8_t dest_mask = 0b0000'0010;
const uint8_t mod_mask = 0b1100'0000;
const uint8_t regm_mask = 0b0011'1000;
if ((byte2 & mod_mask) != mod_mask) return;
//Since we're only doing register mode/register to register, both reg and r/m are affected by the W bit
bool reg_is_dest = (byte1 & dest_mask);
//if D=1, dest is retrieved from the reg field in byte2 and src from r/m
uint8_t dest_value = (reg_is_dest) ? ((byte2 & regm_mask) >> 3) + ((byte1 & wide_mask) * 8) : (byte2 & (regm_mask >> 3)) + ((byte1 & wide_mask) * 8);
uint8_t src_value = (reg_is_dest) ? (byte2 & (regm_mask >> 3)) + ((byte1 & wide_mask) * 8) : ((byte2 & regm_mask) >> 3) + ((byte1 & wide_mask) * 8);
printf("%s, %s\n", REG_ENCODING_TXT[dest_value], REG_ENCODING_TXT[src_value]);
return;
}
int main(int argc, char** argv)
{
if (argc != 2) return -1;
if (CHAR_BIT != 8) return -2;
binary_data bin;
unsigned char byte[1];
bin.binary = fopen(argv[1], "rb");
fseek(bin.binary, 0, SEEK_END);
bin.size = ftell(bin.binary);
fseek(bin.binary, 0, SEEK_SET);
for (int bytes_read = 0; bytes_read < bin.size; bytes_read++)
{
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
manip_inst = (manip_inst >> 2);
if (manip_inst != MOV_RM_TF_R) break;
printf("%s ", "mov");
if (bytes_read >= bin.size) break;
fread(byte, sizeof(byte), 1, bin.binary);
bytes_read++;
uint8_t inst_byte2 = (uint8_t)byte[0];
MOV_RM_TF_R_parse(inst, inst_byte2, 0, 0);
}
return 0;
}

1
1-1/listing-37 Executable file
View file

@ -0,0 +1 @@

3
1-1/listing-37.asm Normal file
View file

@ -0,0 +1,3 @@
bits 16
mov cx, bx

1
1-1/listing-38 Normal file
View file

@ -0,0 +1 @@
‰Ùˆå‰Ú‰Þ‰ûˆÈˆí‰Ã‰ó‰ü‰Å

13
1-1/listing-38.asm Normal file
View file

@ -0,0 +1,13 @@
bits 16
mov cx, bx
mov ch, ah
mov dx, bx
mov si, bx
mov bx, di
mov al, cl
mov ch, ch
mov bx, ax
mov bx, si
mov sp, di
mov bp, ax

4
1-1/nasm-compile.sh Normal file
View file

@ -0,0 +1,4 @@
#! /usr/env/bin bash
nasm -f bin 1-1/listing-38.asm -o 1-1/listing-37
nasm -f bin 1-1/listing-38.asm -o 1-1/listing-38