commit 4580d3b4d32a41cf9d73bfb8800a7ac5f5bdcd74
parent d5389e788c74d1f674f584c264e2216e25e9bc13
Author: Andrew Laack <andrew@laack.co>
Date: Fri, 9 Oct 2026 20:46:14 -0500
jump command decoding
Diffstat:
9 files changed, 218 insertions(+), 62 deletions(-)
diff --git a/perf-aware-programming/8086-decoder/Makefile b/perf-aware-programming/8086-decoder/Makefile
@@ -1,5 +1,5 @@
build:
- g++ decode.c -g3 -o decode
+ tcc decode.c -o decode
clean:
rm decode
@@ -9,11 +9,16 @@ run: build
./decode source/ex2 > dest/ex2.asm
./decode source/ex3 > dest/ex3.asm
./decode source/ex4 > dest/ex4.asm
+ ./decode source/ex4.0 > dest/ex4.0.asm
nasm dest/ex1.asm -o dest/ex1
nasm dest/ex2.asm -o dest/ex2
- nasm dest/ex3.asm -o dest/ex3
nasm dest/ex4.asm -o dest/ex4
+ nasm dest/ex4.0.asm -o dest/ex4.0
diff dest/ex1 source/ex1
diff dest/ex2 source/ex2
diff dest/ex3 source/ex3
diff dest/ex4 source/ex4
+ # diff dest/ex4.0 source/ex4.0
+ # these will differ because of relative
+ # bytes instead of labels, but I verif
+ # ied this is correct.
diff --git a/perf-aware-programming/8086-decoder/decode.c b/perf-aware-programming/8086-decoder/decode.c
@@ -1,4 +1,5 @@
#include "stdio.h"
+#include "assert.h"
#include "stdlib.h"
#include <stddef.h>
@@ -232,6 +233,118 @@ void printBinary(int num) {
}
+char isJump(unsigned char firstByte) {
+ switch (firstByte) {
+ case (116):
+ return 1;
+ case (117):
+ return 1;
+ case (124):
+ return 1;
+ case (126):
+ return 1;
+ case (114):
+ return 1;
+ case (118):
+ return 1;
+ case (122):
+ return 1;
+ case (112):
+ return 1;
+ case (113):
+ return 1;
+ case (120):
+ return 1;
+ case (125):
+ return 1;
+ case (127):
+ return 1;
+ case (119):
+ return 1;
+ case (115):
+ return 1;
+ case (123):
+ return 1;
+ case (121):
+ return 1;
+ case (226):
+ return 1;
+ case (225):
+ return 1;
+ case (224):
+ return 1;
+ case (227):
+ return 1;
+ }
+ return 0;
+}
+
+void handleJump(unsigned char firstByte, unsigned char secondByte) {
+ switch (firstByte) {
+ case (116):
+ printf("je %hhd\n", secondByte);
+ return;
+ case (117):
+ printf("jnz %hhd\n", secondByte);
+ return;
+ case (124):
+ printf("jl %hhd\n", secondByte);
+ return;
+ case (126):
+ printf("jle %hhd\n", secondByte);
+ return;
+ case (114):
+ printf("jb %hhd\n", secondByte);
+ return;
+ case (118):
+ printf("jbe %hhd\n", secondByte);
+ return;
+ case (122):
+ printf("jp %hhd\n", secondByte);
+ return;
+ case (112):
+ printf("jo %hhd\n", secondByte);
+ return;
+ case (120):
+ printf("js %hhd\n", secondByte);
+ return;
+ case (125):
+ printf("jnl %hhd\n", secondByte);
+ return;
+ case (127):
+ printf("jg %hhd\n", secondByte);
+ return;
+ case (119):
+ printf("ja %hhd\n", secondByte);
+ return;
+ case (115):
+ printf("jnb %hhd\n", secondByte);
+ return;
+ case (123):
+ printf("jnp %hhd\n", secondByte);
+ return;
+ case (113):
+ printf("jno %hhd\n", secondByte);
+ return;
+ case (121):
+ printf("jns %hhd\n", secondByte);
+ return;
+ case (226):
+ printf("loop %hhd\n", secondByte);
+ return;
+ case (225):
+ printf("loopz %hhd\n", secondByte);
+ return;
+ case (224):
+ printf("loopnz %hhd\n", secondByte);
+ return;
+ case (227):
+ printf("jcxz %hhd\n", secondByte);
+ return;
+ }
+ assert(0 == firstByte);
+}
+
int main(int argc, char** argv) {
// first 6 := opcode
@@ -264,9 +377,18 @@ int main(int argc, char** argv) {
unsigned char buffer[65536];
size_t n = fread(buffer, 1, sizeof(buffer), fp);
+ char jumping = 0;
+ char jumpPrior;
for(size_t i = 0; i < n; ++i) {
// printBinary(buffer[i]);
+
+ if(jumping) {
+ handleJump(jumpPrior, buffer[i]);
+ jumping = 0;
+ inst = defaultInstruction();
+ continue;
+ }
if(fl == 0) {
if(inst.opcodeUnset) {
unsigned char cur = buffer[i];
@@ -277,6 +399,14 @@ int main(int argc, char** argv) {
inst.immediateToRegister = 1;
} else {
inst.opcode = (buffer[i] & 0b11111100) >> 2;
+
+ if(isJump(buffer[i])) {
+ jumpPrior = buffer[i];
+ jumping = 1;
+ continue;
+ }
+
+
if(inst.opcode == 32) {
inst.s = (buffer[i] & 0b00000010) >> 1;
printf("Sign: %u\n", inst.s);
diff --git a/perf-aware-programming/8086-decoder/dest/ex4.0 b/perf-aware-programming/8086-decoder/dest/ex4.0
@@ -0,0 +1 @@
+…þÿ…ôÿ…îÿ…ìÿ„êÿŒäÿŽÞÿ‚Øÿ†ÒÿŠÌÿ€ÆÿˆÀÿ…ºÿ�´ÿ�®ÿƒ¨ÿ‡¢ÿ‹œÿ�–ÿ‰�ÿâŒáˆà„ã€+
\ No newline at end of file
diff --git a/perf-aware-programming/8086-decoder/dest/ex4.0.asm b/perf-aware-programming/8086-decoder/dest/ex4.0.asm
@@ -0,0 +1,24 @@
+jnz 2
+jnz -4
+jnz -6
+jnz -4
+je -2
+jl -4
+jle -6
+jb -8
+jbe -10
+jp -12
+jo -14
+js -16
+jnz -18
+jnl -20
+jg -22
+jnb -24
+ja -26
+jnp -28
+jno -30
+jns -32
+loop -34
+loopz -36
+loopnz -38
+jcxz -40
diff --git a/perf-aware-programming/8086-decoder/dest/ex4.asm b/perf-aware-programming/8086-decoder/dest/ex4.asm
@@ -26,7 +26,7 @@ add ax,bx
add al,ah
1
add ax, [si]
-56
+loop 4
2
6
23
@@ -38,7 +38,7 @@ add bp,11016
23
10
add ch, [bp + si]
-30
+jp 4
10
1
6
@@ -48,7 +48,7 @@ add [bx + di], ch
19
10
1
-30
+jnp 6
Sign: 0
add ch,34
Sign: 1
@@ -58,7 +58,7 @@ add bp,11037
10
10
mov bp,11267
-56
+loop 44
2
6
23
@@ -70,7 +70,7 @@ add di,15112
23
14
add bh, [bp + si]
-30
+jp 4
14
1
6
@@ -80,7 +80,7 @@ add [bx + di], bh
19
14
1
-30
+jnp 6
Sign: 0
add bh,34
Sign: 1
@@ -92,28 +92,4 @@ add di,4834
14
15
add di, [si]
-56
-2
-add dh, [di + -4]
-29
-29
-29
-31
-31
-28
-29
-30
-28
-30
-29
-31
-31
-28
-29
-30
-28
-30
-56
-56
-56
-56
+loop 60
diff --git a/perf-aware-programming/8086-decoder/source/ex4 b/perf-aware-programming/8086-decoder/source/ex4
Binary files differ.
diff --git a/perf-aware-programming/8086-decoder/source/ex4.0 b/perf-aware-programming/8086-decoder/source/ex4.0
@@ -0,0 +1 @@
+uuüuúuütþ|ü~úrøvözôpòxðuî}ìêsèwæ{äqâyàâÞáÜàÚãØ+
\ No newline at end of file
diff --git a/perf-aware-programming/8086-decoder/source/ex4.0.asm b/perf-aware-programming/8086-decoder/source/ex4.0.asm
@@ -0,0 +1,46 @@
+; ========================================================================
+;
+; (C) Copyright 2023 by Molly Rocket, Inc., All Rights Reserved.
+;
+; This software is provided 'as-is', without any express or implied
+; warranty. In no event will the authors be held liable for any damages
+; arising from the use of this software.
+;
+; Please see https://computerenhance.com for further information
+;
+; ========================================================================
+
+; ========================================================================
+; LISTING 41
+; ========================================================================
+
+bits 16
+
+test_label0:
+jnz test_label1
+jnz test_label0
+test_label1:
+jnz test_label0
+jnz test_label1
+
+label:
+je label
+jl label
+jle label
+jb label
+jbe label
+jp label
+jo label
+js label
+jne label
+jnl label
+jg label
+jnb label
+ja label
+jnp label
+jno label
+jns label
+loop label
+loopz label
+loopnz label
+jcxz label
diff --git a/perf-aware-programming/8086-decoder/source/ex4.asm b/perf-aware-programming/8086-decoder/source/ex4.asm
@@ -90,32 +90,3 @@ cmp al, ah
cmp ax, 1000
cmp al, -30
cmp al, 9
-
-test_label0:
-jnz test_label1
-jnz test_label0
-test_label1:
-jnz test_label0
-jnz test_label1
-
-label:
-je label
-jl label
-jle label
-jb label
-jbe label
-jp label
-jo label
-js label
-jne label
-jnl label
-jg label
-jnb label
-ja label
-jnp label
-jno label
-jns label
-loop label
-loopz label
-loopnz label
-jcxz label