Started working on decoding 8086 instruction homework
This commit is contained in:
parent
e6b8315ca9
commit
5fac2ebfa8
3
8086_assembly_01/.gitignore
vendored
Normal file
3
8086_assembly_01/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
dasm
|
||||||
|
listing_0037_single_register_mov
|
||||||
|
listing_0038_many_register_mov
|
2
8086_assembly_01/Makefile
Normal file
2
8086_assembly_01/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
all:
|
||||||
|
clang++ -g dasm.cpp -o dasm
|
159
8086_assembly_01/dasm.cpp
Normal file
159
8086_assembly_01/dasm.cpp
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
#include <bits/types/FILE.h>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Bit patterns for the 8086 OPCODES
|
||||||
|
enum class OPCODES {
|
||||||
|
MOV = 0x88,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class OPMASKS {
|
||||||
|
MOV = 0xfc,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class FLAGS {
|
||||||
|
WORD = 0x01,
|
||||||
|
REG_DEST = 0x02,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class MODE {
|
||||||
|
MEM = 0x00,
|
||||||
|
MEM8 = 0x40,
|
||||||
|
MEM16 = 0x80,
|
||||||
|
REG = 0xc0,
|
||||||
|
};
|
||||||
|
|
||||||
|
uint16_t decode_mode(uint16_t instruction);
|
||||||
|
void decode_register(uint16_t instruction, bool word, char *dest);
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("Please provide a file to disassemble\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *filename = argv[1];
|
||||||
|
|
||||||
|
FILE *fp = fopen(filename, "rb");
|
||||||
|
|
||||||
|
if (fp) {
|
||||||
|
uint16_t inst = 0;
|
||||||
|
const char *op = "";
|
||||||
|
|
||||||
|
char out_filename[4096] = {0};
|
||||||
|
sprintf(out_filename, "%s_out.asm", filename);
|
||||||
|
|
||||||
|
FILE *out = fopen(out_filename, "w");
|
||||||
|
|
||||||
|
if (out) {
|
||||||
|
fprintf(out, "; Disassembled by DASM\n\nbits 16\n\n");
|
||||||
|
|
||||||
|
while (fread(&inst, sizeof(inst), 1, fp)) {
|
||||||
|
if ((inst & (uint16_t)OPMASKS::MOV) == (uint16_t)OPCODES::MOV) {
|
||||||
|
op = "mov";
|
||||||
|
|
||||||
|
bool reg_dest =
|
||||||
|
(inst & (uint16_t)FLAGS::REG_DEST) == (uint16_t)FLAGS::REG_DEST;
|
||||||
|
bool word = (inst & (uint16_t)FLAGS::WORD) == (uint16_t)FLAGS::WORD;
|
||||||
|
|
||||||
|
// NOTE: Using right shift will only work on little-endian CPUs
|
||||||
|
uint16_t operands_info = inst >> 8;
|
||||||
|
|
||||||
|
if (decode_mode(operands_info) == (uint16_t)MODE::REG) {
|
||||||
|
char rm[3] = {0};
|
||||||
|
char reg[3] = {0};
|
||||||
|
|
||||||
|
decode_register(operands_info, word, rm);
|
||||||
|
decode_register(operands_info >> 3, word, reg);
|
||||||
|
|
||||||
|
fprintf(out, "%s %s, %s\n", op, reg_dest ? reg : rm,
|
||||||
|
reg_dest ? rm : reg);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("It's not a mov operation\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(out);
|
||||||
|
} else {
|
||||||
|
printf("Failed to open output file\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
} else {
|
||||||
|
printf("Failed to open the selected file\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t decode_mode(uint16_t instruction) {
|
||||||
|
uint16_t mode_mask = 0xc0;
|
||||||
|
|
||||||
|
return instruction & mode_mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
void decode_register(uint16_t instruction, bool word, char *dest) {
|
||||||
|
uint16_t reg_mask = 0x07;
|
||||||
|
|
||||||
|
switch (instruction & reg_mask) {
|
||||||
|
case 0x00:
|
||||||
|
if (word) {
|
||||||
|
strcpy(dest, "ax");
|
||||||
|
} else {
|
||||||
|
strcpy(dest, "al");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x01:
|
||||||
|
if (word) {
|
||||||
|
strcpy(dest, "cx");
|
||||||
|
} else {
|
||||||
|
strcpy(dest, "cl");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x02:
|
||||||
|
if (word) {
|
||||||
|
strcpy(dest, "dx");
|
||||||
|
} else {
|
||||||
|
strcpy(dest, "dl");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x03:
|
||||||
|
if (word) {
|
||||||
|
strcpy(dest, "bx");
|
||||||
|
} else {
|
||||||
|
strcpy(dest, "bl");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x04:
|
||||||
|
if (word) {
|
||||||
|
strcpy(dest, "sp");
|
||||||
|
} else {
|
||||||
|
strcpy(dest, "ah");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x05:
|
||||||
|
if (word) {
|
||||||
|
strcpy(dest, "bp");
|
||||||
|
} else {
|
||||||
|
strcpy(dest, "ch");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x06:
|
||||||
|
if (word) {
|
||||||
|
strcpy(dest, "si");
|
||||||
|
} else {
|
||||||
|
strcpy(dest, "dh");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x07:
|
||||||
|
if (word) {
|
||||||
|
strcpy(dest, "di");
|
||||||
|
} else {
|
||||||
|
strcpy(dest, "bh");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
19
8086_assembly_01/listing_0037_single_register_mov.asm
Normal file
19
8086_assembly_01/listing_0037_single_register_mov.asm
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
; ========================================================================
|
||||||
|
;
|
||||||
|
; (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 37
|
||||||
|
; ========================================================================
|
||||||
|
|
||||||
|
bits 16
|
||||||
|
|
||||||
|
mov cx, bx
|
@ -0,0 +1,5 @@
|
|||||||
|
; Disassembled by DASM
|
||||||
|
|
||||||
|
bits 16
|
||||||
|
|
||||||
|
mov cx, bx
|
29
8086_assembly_01/listing_0038_many_register_mov.asm
Normal file
29
8086_assembly_01/listing_0038_many_register_mov.asm
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
; ========================================================================
|
||||||
|
;
|
||||||
|
; (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 38
|
||||||
|
; ========================================================================
|
||||||
|
|
||||||
|
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
|
15
8086_assembly_01/listing_0038_many_register_mov_out.asm
Normal file
15
8086_assembly_01/listing_0038_many_register_mov_out.asm
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
; Disassembled by DASM
|
||||||
|
|
||||||
|
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
|
Loading…
Reference in New Issue
Block a user