Code Monkey home page Code Monkey logo

cpusim's Introduction

CpuSim

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.

How to run

Clone the repository and run it in Visual Studio or any other preferred editor/IDE. It is a simple dotnet 6 console application.

How to use

Once compiled, just type cpusim to show the usage message (which can be different for each commit) and read the instructions.

1st Lesson: Instruction Decoding on the 8086

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.

Example of running 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.

Example of showing debug information

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.

2nd Lesson: Decoding Multiple Instructions and Suffixes

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

cpusim's People

Contributors

amelco avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.