- Versão em português
A basic Intel 8086 CPU simulator.
Based on Computer, Enhance! course by Casey Muratori and documentation for Intel 8086 chip.
Each commit will be a new lesson. So the first commit will be lesson 1. The idea is to create code for each lesson and put it here as a way of documenting my progress and, at the same time serving as a code repository. It also lets you follow my thinking by getting the differences between commits.
I'll try to do everything from documentation to code comments in both English and Portuguese.
Clone the repository and run it in Visual Studio or any other preferred editor/IDE. It is a simple dotnet 6 console application.
Once compiled, just type cpusim
to show the usage message (which can be different for each commit) and read the instructions.
In this lesson, we will learn how to decode 8086 CPU instructions.
We will use the following instructions:
MOV
record for record.
It's one of the simplest instructions to decode, making it a good introduction to how the CPU works.
The objective of this lesson is to create a program that reads a file with binary instructions in 8086 assembly,
decode the MOV
instruction and print the result to the screen. For this we have two input files
single_register_mov
many_register_mov
which contains binary code with one and several MOV
instructions, respectively.
Below is the binary code of single_register_mov
. Note that it's only 2 bytes long.
The program receives as the first input parameter the name of the file to be read and prints the screen decoding. The figure below shows an example of running the program.
Note: You can optionally run debug mode for more information about what is going on, such as the values being temporarily read from the file.
The respective .asm
extension files contain the 8086 assembly code that generated the input files.
Therefore, the output result of the program must be the same as the .asm
file.
Interesting fact: It is possible to binary output the program using the command nasm filename.asm
(click here to download nasm).
The generated binary can be compared with the input file to verify that the program is correct.
On windows, to compare two binary files, just use the command fc file1 file2
.
Link to code: Lesson 1.
In this lesson, we will learn how to decode more instructions and suffixes. Let's begin implementing the immediate to register instruction. This instruction is used to move a value directly to a register.
A file called, single_immediate.asm
, contains the assembly code for this instruction.
Generate the binary file (nasm single_immediate.asm
) and run the program with the nasm output as the program input:
cpusim single_immediate
.
You can also run the program in debug mode (cpusim single_immediate debug
) to see the values being read from the file.
Link to immediate mode commit: Lesson 2, immediate