1-3 complete

This commit is contained in:
Hane 2026-01-28 17:19:32 +01:00
commit 53b4945844
5 changed files with 167 additions and 80 deletions

Binary file not shown.

View file

@ -17,6 +17,48 @@
/* CMP = 0b111, */ /* CMP = 0b111, */
/* } ARITHMETIC_OP; */ /* } ARITHMETIC_OP; */
typedef enum COND_JMP_OPC
{
JX = 0b0111,
LOOPX = 0b1110'00,
} JMP_OPC;
typedef enum LOOPX_INST
{
LOOP = 0b10,
LOOPZ = 0b01,
LOOPNZ = 0b00,
JCXZ = 0b11
} LOOPX_INST;
#define LOOPX_INST_TXT_LEN 7
char LOOPX_INST_TXT[0b100][LOOPX_INST_TXT_LEN] = { "loopnz", "loopz", "loop", "jcxz" };
typedef enum JX_INST
{
JZ = 0b0100,
JL = 0b1100,
JLE = 0b1110,
JB = 0b0010,
JBE = 0b0110,
JP = 0b1010,
JO = 0b0000,
JS = 0b1000,
JNE = 0b0101,
JNL = 0b1101,
JNLE = 0b1111,
JNB = 0b0011,
JNBE = 0b0111,
JNP = 0b1011,
JNO = 0b0001,
JNS = 0b1001,
} JX_INST;
#define JX_INST_TXT_LEN 5
char JX_INST_TXT[0b10000][JX_INST_TXT_LEN] = { "jo", "jno", "jb", "jnb", "jz", "jne", "jbe", "jnbe",
"js", "jns", "jp", "jnp", "jl", "jnl", "jle", "jnle" };
typedef enum ARITHMETIC_OP typedef enum ARITHMETIC_OP
{ {
ADD = 0b000, ADD = 0b000,
@ -351,7 +393,7 @@ int16_t calc_effective_disp(uint8_t high_order, uint8_t low_order, bool lo_only)
const uint8_t low_order_neg_mask = 0b1000'0000; const uint8_t low_order_neg_mask = 0b1000'0000;
int16_t effective_disp = 0; int16_t effective_disp = 0;
if (lo_only & ((low_order & low_order_neg_mask) >> 7)) if (lo_only && ((low_order & low_order_neg_mask) >> 7))
high_order = high_order | 0b1111'1111; high_order = high_order | 0b1111'1111;
int8_t *effective_disp_high_order =(int8_t*) &(effective_disp) + 1; int8_t *effective_disp_high_order =(int8_t*) &(effective_disp) + 1;
memcpy(&(effective_disp), &low_order, 1); memcpy(&(effective_disp), &low_order, 1);
@ -682,14 +724,14 @@ int ASC_I_RM_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int bytes_
effective_disp = calc_effective_disp(high_disp, low_disp, false); effective_disp = calc_effective_disp(high_disp, low_disp, false);
low_data = payload[2]; low_data = payload[2];
if (is_wide) if (is_wide && !is_sign)
{ {
high_data = payload[3]; high_data = payload[3];
effective_data = calc_effective_disp(high_data, low_data, false); effective_data = calc_effective_disp(high_data, low_data, false);
} }
else else
{ {
effective_data = calc_effective_disp(0, low_disp, true); effective_data = calc_effective_disp(0, low_data, true);
} }
fill_ea_string(&mem_data, EA_ENCODING_TXT[ea_table_value], effective_disp); fill_ea_string(&mem_data, EA_ENCODING_TXT[ea_table_value], effective_disp);
@ -828,6 +870,23 @@ int ASC_REGM_R_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int byte
return extra_bytes_read; return extra_bytes_read;
} }
void JMP_parse(uint8_t byte1, uint8_t byte2, binary_data *binary, int bytes_read, JMP_OPC op)
{
const uint8_t loopx_mask = 0b0000'0011;
const uint8_t jx_mask = 0b0000'1111;
int8_t disp = 0;
memcpy(&disp, &byte2, sizeof(uint8_t));
uint8_t jump_op = (op == LOOPX) ? (byte1 & loopx_mask) : (byte1 & jx_mask);
char nasm_offset[4] = { "\0" };
if (!disp) strcpy(nasm_offset, "$+");
else strcpy(nasm_offset, "$+2");
//We have to add +2 to displacement so that NASM can parse displacements correctly
printf("%s %s%+d \n", (op == LOOPX) ? LOOPX_INST_TXT[jump_op] : JX_INST_TXT[jump_op], nasm_offset, disp);
fprintf(output, "%s %s%+d \n", (op == LOOPX) ? LOOPX_INST_TXT[jump_op] : JX_INST_TXT[jump_op], nasm_offset, disp);
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc != 2) return -1; if (argc != 2) return -1;
@ -848,8 +907,9 @@ int main(int argc, char** argv)
fread(byte, sizeof(byte), 1, bin.binary); fread(byte, sizeof(byte), 1, bin.binary);
uint8_t manip_inst = (uint8_t)byte[0]; uint8_t manip_inst = (uint8_t)byte[0];
uint8_t inst = (uint8_t)byte[0]; uint8_t inst = (uint8_t)byte[0];
//First, we check for ASC. //First, we check for ASC or JMPs.
manip_inst = (manip_inst >> 1); manip_inst = (manip_inst >> 1);
if ((manip_inst & ASC_I_A_M) == ASC_I_A) if ((manip_inst & ASC_I_A_M) == ASC_I_A)
{ {
if (bytes_read >= bin.size) break; if (bytes_read >= bin.size) break;
@ -860,6 +920,15 @@ int main(int argc, char** argv)
continue; continue;
} }
manip_inst = (inst >> 2); manip_inst = (inst >> 2);
if (manip_inst == LOOPX)
{
if (bytes_read >= bin.size) break;
fread(byte, sizeof(byte), 1, bin.binary);
bytes_read++;
inst_byte2 = (uint8_t)byte[0];
JMP_parse(inst, inst_byte2, 0, 0, LOOPX);
continue;
}
if ((manip_inst & ASC_REGM_R_M) == ASC_REGM_R) if ((manip_inst & ASC_REGM_R_M) == ASC_REGM_R)
{ {
if (bytes_read >= bin.size) break; if (bytes_read >= bin.size) break;
@ -878,6 +947,16 @@ int main(int argc, char** argv)
bytes_read += ASC_I_RM_parse(inst, inst_byte2, 0, 0); bytes_read += ASC_I_RM_parse(inst, inst_byte2, 0, 0);
continue; continue;
} }
manip_inst = (inst >> 4);
if (manip_inst == JX)
{
if (bytes_read >= bin.size) break;
fread(byte, sizeof(byte), 1, bin.binary);
bytes_read++;
inst_byte2 = (uint8_t)byte[0];
JMP_parse(inst, inst_byte2, 0, 0, JX);
continue;
}
//Then, we're checking for all MOV except segment registers //Then, we're checking for all MOV except segment registers
manip_inst = (inst >> 1); manip_inst = (inst >> 1);
switch (manip_inst) switch (manip_inst)

Binary file not shown.

View file

@ -1,26 +1,10 @@
; ======================================================================== ;bits 16
; ;
; (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
;add bx, [bx+si] ;add bx, [bx+si]
;add bx, [bp] ;add bx, [bp]
add si, 2 ;add si, 2
add bp, 2 ;add bp, 2
add cx, 8 ;add cx, 8
;add bx, [bp + 0] ;add bx, [bp + 0]
;add cx, [bx + 2] ;add cx, [bx + 2]
;add bh, [bp + si + 4] ;add bh, [bp + si + 4]
@ -31,21 +15,20 @@ add cx, 8
;add [bx + 2], cx ;add [bx + 2], cx
;add [bp + si + 4], bh ;add [bp + si + 4], bh
;add [bp + di + 6], di ;add [bp + di + 6], di
add byte [bx], 34 ;add byte [bx], 34
add word [bp + si + 1000], 29 ;add word [bp + si + 1000], 29
;add ax, [bp] ;add ax, [bp]
;add al, [bx + si] ;add al, [bx + si]
;add ax, bx ;add ax, bx
add al, ah ;add al, ah
add ax, 1000 ;add ax, 1000
add al, -30 ;add al, -30
add al, 9 ;add al, 9
;sub bx, [bx+si] ;sub bx, [bx+si]
;sub bx, [bp] ;sub bx, [bp]
sub si, 2 ;sub si, 2
sub bp, 2 ;sub bp, 2
sub cx, 8 ;sub cx, 8
;sub bx, [bp + 0] ;sub bx, [bp + 0]
;sub cx, [bx + 2] ;sub cx, [bx + 2]
;sub bh, [bp + si + 4] ;sub bh, [bp + si + 4]
@ -56,21 +39,20 @@ sub cx, 8
;sub [bx + 2], cx ;sub [bx + 2], cx
;sub [bp + si + 4], bh ;sub [bp + si + 4], bh
;sub [bp + di + 6], di ;sub [bp + di + 6], di
sub byte [bx], 34 ;sub byte [bx], 34
sub word [bx + di], 29 ;sub word [bx + di], 29
;sub ax, [bp] ;sub ax, [bp]
;sub al, [bx + si] ;sub al, [bx + si]
;sub ax, bx ;sub ax, bx
;sub al, ah ;sub al, ah
sub ax, 1000 ;sub ax, 1000
sub al, -30 ;sub al, -30
sub al, 9 ;sub al, 9
;cmp bx, [bx+si] ;cmp bx, [bx+si]
;cmp bx, [bp] ;cmp bx, [bp]
cmp si, 2 ;cmp si, 2
cmp bp, 2 ;cmp bp, 2
cmp cx, 8 ;cmp cx, 8
;cmp bx, [bp + 0] ;cmp bx, [bp + 0]
;cmp cx, [bx + 2] ;cmp cx, [bx + 2]
;cmp bh, [bp + si + 4] ;cmp bh, [bp + si + 4]
@ -81,41 +63,41 @@ cmp cx, 8
;cmp [bx + 2], cx ;cmp [bx + 2], cx
;cmp [bp + si + 4], bh ;cmp [bp + si + 4], bh
;cmp [bp + di + 6], di ;cmp [bp + di + 6], di
cmp byte [bx], 34 ;cmp byte [bx], 34
cmp word [4834], 29 ;cmp word [4834], 29
;cmp ax, [bp] ;cmp ax, [bp]
;cmp al, [bx + si] ;cmp al, [bx + si]
;cmp ax, bx ;cmp ax, bx
;cmp al, ah ;cmp al, ah
cmp ax, 1000 ;cmp ax, 1000
cmp al, -30 ;cmp al, -30
cmp al, 9 ;cmp al, 9
; test_label0: test_label0:
; jnz test_label1 jnz test_label1
; jnz test_label0 jnz test_label0
; test_label1: test_label1:
; jnz test_label0 jnz test_label0
; jnz test_label1 jnz test_label1
;
; label: label:
; je label je label
; jl label jl label
; jle label jle label
; jb label jb label
; jbe label jbe label
; jp label jp label
; jo label jo label
; js label js label
; jne label jne label
; jnl label jnl label
; jg label jg label
; jnb label jnb label
; ja label ja label
; jnp label jnp label
; jno label jno label
; jns label jns label
; loop label loop label
; loopz label loopz label
; loopnz label loopnz label
; jcxz label jcxz label

View file

@ -0,0 +1,26 @@
bits 16
jne $+2+2
jne $+2-4
jne $+2-6
jne $+2-4
jz $+2-2
jl $+2-4
jle $+2-6
jb $+2-8
jbe $+2-10
jp $+2-12
jo $+2-14
js $+2-16
jne $+2-18
jnl $+2-20
jnle $+2-22
jnb $+2-24
jnbe $+2-26
jnp $+2-28
jno $+2-30
jns $+2-32
loop $+2-34
loopz $+2-36
loopnz $+2-38
jcxz $+2-40