c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

commit ea44d7ecfa04c0463d8fca5524632dff2d110169
parent 6ca7b8f5db4136e481c01c0bc9330c45f9f1fb66
Author: Andrew Laack <andrew@laack.co>
Date:   Thu,  8 Oct 2026 22:36:02 -0500

First few working w/ add instruction

Diffstat:
Mperf-aware-programming/8086-decoder/decode.c | 39+++++++++++++++++++++++++++++----------
Dperf-aware-programming/8086-decoder/dest/ex4 | 2--
Mperf-aware-programming/8086-decoder/dest/ex4.asm | 116++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 144 insertions(+), 13 deletions(-)

diff --git a/perf-aware-programming/8086-decoder/decode.c b/perf-aware-programming/8086-decoder/decode.c @@ -4,6 +4,7 @@ typedef struct { unsigned char opcode; + unsigned char opcodeUnset; unsigned char d; unsigned char w; unsigned char mod; @@ -131,15 +132,23 @@ void handleEAC(Instruction inst) { } +void printInstructionMnemonic(Instruction inst) { + if(inst.opcode == 34 || inst.opcode == 0b1011) { + printf("mov "); + } else { + printf("add "); + } +} + // return 1 to inform caller to give us more bytes // return 0 if done unsigned char printInstruction(Instruction inst) { - if(inst.opcode == 34) { + if(inst.opcode == 34 || inst.opcode == 0) { // register mode if(inst.mod == 0b11) { if(inst.d == 1) { - printf("mov "); + printInstructionMnemonic(inst); char* enc1 = regMapping(inst.reg,inst.w); printf("%c%c,", enc1[0], enc1[1]); free(enc1); @@ -149,7 +158,7 @@ unsigned char printInstruction(Instruction inst) { free(enc2); } else { - printf("mov "); + printInstructionMnemonic(inst); char* enc1 = regMapping(inst.rm,inst.w); printf("%c%c,", enc1[0], enc1[1]); free(enc1); @@ -162,7 +171,7 @@ unsigned char printInstruction(Instruction inst) { if(inst.dlUnset) { return 1; } - printf("mov "); + printInstructionMnemonic(inst); handleEAC(inst); } else if(inst.mod == 0b10){ // both bytes @@ -170,12 +179,12 @@ unsigned char printInstruction(Instruction inst) { return 1; } - printf("mov "); + printInstructionMnemonic(inst); handleEAC(inst); } else if(inst.mod == 0b00){ - printf("mov "); + printInstructionMnemonic(inst); handleEAC(inst); // memory mode, no displacement return 0; @@ -190,7 +199,7 @@ unsigned char printInstruction(Instruction inst) { return 1; } - printf("mov "); + printInstructionMnemonic(inst); char* enc1 = regMapping(inst.reg,inst.w); printf("%c%c,", enc1[0], enc1[1]); @@ -203,17 +212,25 @@ unsigned char printInstruction(Instruction inst) { free(enc1); } else { - //printf("%u\n", (inst.opcode)); + printf("%u\n", (inst.opcode)); } return 0; } Instruction defaultInstruction() { - Instruction inst = (Instruction){ 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0}; + Instruction inst = (Instruction){ 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0}; return inst; } +void printBinary(int num) { + for (int i = sizeof(char) * 8 - 1; i >= 0; i--) { + printf("%d", (num >> i) & 1); + } + printf("\n"); +} + + int main(int argc, char** argv) { // first 6 := opcode @@ -248,8 +265,9 @@ int main(int argc, char** argv) { size_t n = fread(buffer, 1, sizeof(buffer), fp); for(size_t i = 0; i < n; ++i) { + // printBinary(buffer[i]); if(fl == 0) { - if(inst.opcode == 0) { + if(inst.opcodeUnset) { unsigned char cur = buffer[i]; if((cur & 0b11110000) == 0b10110000) { inst.opcode = 0b1011; @@ -261,6 +279,7 @@ int main(int argc, char** argv) { inst.d = (buffer[i] & 0b00000010) >> 1; inst.w = buffer[i] & 0b00000001; } + inst.opcodeUnset = 0; } else { if(inst.immediateToRegister == 1) { if(inst.dlUnset == 1) { diff --git a/perf-aware-programming/8086-decoder/dest/ex4 b/perf-aware-programming/8086-decoder/dest/ex4 @@ -1 +0,0 @@ -½,- \ No newline at end of file diff --git a/perf-aware-programming/8086-decoder/dest/ex4.asm b/perf-aware-programming/8086-decoder/dest/ex4.asm @@ -1 +1,115 @@ -mov bp,11267 +add bx, [bx + si] +add bx, [bp] +32 +add al, [bp + di + 709] +32 +2 +23 +add cx, [bx + 2] +add bh, [bp + si + 4] +add di, [bp + di + 6] +add [bx + si], bx +add [bp], bx +add [bp], bx +add [bx + 2], cx +add [bp + si + 4], bh +add [bp + di + 6], di +32 +8 +32 +add bx, [di] +add ax, [bp] +add al, [bx + si] +add ax,bx +add al,ah +1 +add ax, [si] +56 +2 +6 +23 +32 +add al, [bp + di + 749] +32 +2 +23 +10 +add ch, [bp + si] +30 +10 +1 +6 +23 +10 +add [bx + di], ch +19 +10 +1 +30 +32 +8 +10 +10 +add [bp + si], ch +add [bx + di], ch +54 +56 +58 +mov ah,44 +2 +6 +23 +32 +add al, [bp + di + 765] +32 +2 +23 +14 +add bh, [bp + si] +30 +14 +1 +6 +23 +14 +add [bx + di], bh +19 +14 +1 +30 +32 +8 +15 +4 +14 +add [bp + si], bh +add [bx + di], bh +54 +56 +58 +15 +15 +29 +29 +29 +29 +29 +31 +31 +28 +29 +30 +28 +30 +29 +31 +31 +28 +29 +30 +28 +30 +56 +56 +56 +56