Initial commit
This commit is contained in:
commit
3601436777
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*/build
|
459
part1/_clang-format
Normal file
459
part1/_clang-format
Normal file
@ -0,0 +1,459 @@
|
||||
---
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
---
|
||||
Language: Cpp
|
||||
|
||||
# Align parameters on the open bracket, e.g.:
|
||||
# someLongFunction(argument1,
|
||||
# argument2);
|
||||
AlignAfterOpenBracket: Align
|
||||
|
||||
# Align array column and left justify the columns e.g.:
|
||||
# struct test demo[] =
|
||||
# {
|
||||
# {56, 23, "hello"},
|
||||
# {-1, 93463, "world"},
|
||||
# {7, 5, "!!" }
|
||||
# };
|
||||
AlignArrayOfStructures: Left
|
||||
|
||||
# Align assignments on consecutive lines. This will result in formattings like:
|
||||
#
|
||||
# int a = 1;
|
||||
# int somelongname = 2;
|
||||
# double c = 3;
|
||||
#
|
||||
# int d = 3;
|
||||
# /* A comment. */
|
||||
# double e = 4;
|
||||
AlignConsecutiveAssignments: Consecutive
|
||||
AlignConsecutiveBitFields: Consecutive
|
||||
AlignConsecutiveDeclarations: Consecutive
|
||||
AlignConsecutiveMacros: Consecutive
|
||||
|
||||
# Align escaped newlines as far left as possible.
|
||||
# #define A \
|
||||
# int aaaa; \
|
||||
# int b; \
|
||||
# int dddddddddd;
|
||||
AlignEscapedNewlines: Left
|
||||
|
||||
# Horizontally align operands of binary and ternary expressions.
|
||||
# Specifically, this aligns operands of a single expression that needs to be
|
||||
# split over multiple lines, e.g.:
|
||||
#
|
||||
# int aaa = bbbbbbbbbbbbbbb +
|
||||
# ccccccccccccccc;
|
||||
AlignOperands: Align
|
||||
|
||||
# true: false:
|
||||
# int a; // My comment a vs. int a; // My comment a
|
||||
# int b = 2; // comment b int b = 2; // comment about b
|
||||
AlignTrailingComments: true
|
||||
|
||||
# If the function declaration doesn’t fit on a line, allow putting all
|
||||
# parameters of a function declaration onto the next line even if
|
||||
# BinPackParameters is false.
|
||||
#
|
||||
# true:
|
||||
# void myFunction(
|
||||
# int a, int b, int c, int d, int e);
|
||||
#
|
||||
# false:
|
||||
# void myFunction(int a,
|
||||
# int b,
|
||||
# int c,
|
||||
# int d,
|
||||
# int e);
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
AllowShortBlocksOnASingleLine: Never # "while (true) { continue; }" can be put on a single line.
|
||||
|
||||
# If true, short case labels will be contracted to a single line.
|
||||
#
|
||||
# true: false:
|
||||
# switch (a) { vs. switch (a) {
|
||||
# case 1: x = 1; break; case 1:
|
||||
# case 2: return; x = 1;
|
||||
# } break;
|
||||
# case 2:
|
||||
# return;
|
||||
# }
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
AllowShortEnumsOnASingleLine: true # enum { A, B } myEnum;
|
||||
|
||||
# Only merge functions defined inside a class. Implies “empty”.
|
||||
#
|
||||
# class Foo {
|
||||
# void f() { foo(); }
|
||||
# };
|
||||
# void f() {
|
||||
# foo();
|
||||
# }
|
||||
# void f() {}
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
|
||||
# Only merge empty lambdas.
|
||||
#
|
||||
# auto lambda = [](int a) {}
|
||||
# auto lambda2 = [](int a) {
|
||||
# return a;
|
||||
# };
|
||||
AllowShortLambdasOnASingleLine: Empty
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
|
||||
# true: false:
|
||||
# aaaa = vs. aaaa = "bbbb"
|
||||
# "bbbb" "cccc";
|
||||
# "cccc";
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
|
||||
# Force break after template declaration only when the following declaration
|
||||
# spans multiple lines.
|
||||
#
|
||||
# template <typename T> T foo() {
|
||||
# }
|
||||
# template <typename T>
|
||||
# T foo(int aaaaaaaaaaaaaaaaaaaaa,
|
||||
# int bbbbbbbbbbbbbbbbbbbbb) {
|
||||
# }
|
||||
AlwaysBreakTemplateDeclarations: MultiLine
|
||||
|
||||
# If false, a function call’s arguments will either be all on the same line or
|
||||
# will have one line each.
|
||||
#
|
||||
# true:
|
||||
# void f() {
|
||||
# f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,
|
||||
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
|
||||
# }
|
||||
#
|
||||
# false:
|
||||
# void f() {
|
||||
# f(aaaaaaaaaaaaaaaaaaaa,
|
||||
# aaaaaaaaaaaaaaaaaaaa,
|
||||
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
|
||||
# }
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false # As BinPackArguments but for function definition parameters
|
||||
|
||||
# Add space after the : only (space may be added before if needed for
|
||||
# AlignConsecutiveBitFields).
|
||||
#
|
||||
# unsigned bf: 2;
|
||||
BitFieldColonSpacing: After
|
||||
|
||||
# LooooooooooongType loooooooooooooooooooooongVariable =
|
||||
# someLooooooooooooooooongFunction();
|
||||
#
|
||||
# bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
|
||||
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==
|
||||
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
|
||||
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >
|
||||
# ccccccccccccccccccccccccccccccccccccccccc;
|
||||
BreakBeforeBinaryOperators: None
|
||||
|
||||
# Always attach braces to surrounding context, but break before braces on
|
||||
# function, namespace and class definitions.
|
||||
BreakBeforeBraces: Linux
|
||||
|
||||
# true:
|
||||
# template<typename T>
|
||||
# concept ...
|
||||
#
|
||||
# false:
|
||||
# template<typename T> concept ...
|
||||
BreakBeforeConceptDeclarations: false
|
||||
|
||||
# true:
|
||||
# veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription
|
||||
# ? firstValue
|
||||
# : SecondValueVeryVeryVeryVeryLong;
|
||||
#
|
||||
# false:
|
||||
# veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ?
|
||||
# firstValue :
|
||||
# SecondValueVeryVeryVeryVeryLong;
|
||||
BreakBeforeTernaryOperators: true
|
||||
|
||||
# Break constructor initializers before the colon and commas, and align the
|
||||
# commas with the colon.
|
||||
#
|
||||
# Constructor()
|
||||
# : initializer1()
|
||||
# , initializer2()
|
||||
BreakConstructorInitializers: BeforeComma
|
||||
|
||||
# Break inheritance list only after the commas.
|
||||
#
|
||||
# class Foo : Base1,
|
||||
# Base2
|
||||
# {};
|
||||
BreakInheritanceList: AfterComma
|
||||
|
||||
# true:
|
||||
# const char* x = "veryVeryVeryVeryVeryVe"
|
||||
# "ryVeryVeryVeryVeryVery"
|
||||
# "VeryLongString";
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 100
|
||||
|
||||
# false:
|
||||
# namespace Foo {
|
||||
# namespace Bar {
|
||||
# }
|
||||
# }
|
||||
CompactNamespaces: false
|
||||
|
||||
# true: false:
|
||||
# vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
|
||||
# vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} };
|
||||
# f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]);
|
||||
# new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 };
|
||||
Cpp11BracedListStyle: true
|
||||
|
||||
# Analyze the formatted file for the most used line ending (\r\n or \n). UseCRLF
|
||||
# is only used as a fallback if none can be derived.
|
||||
DeriveLineEnding: true
|
||||
DerivePointerAlignment: true # As per DeriveLineEnding except for pointers and references
|
||||
|
||||
# Add empty line only when access modifier starts a new logical block. Logical
|
||||
# block is a group of one or more member fields or functions.
|
||||
#
|
||||
# struct foo {
|
||||
# private:
|
||||
# int i;
|
||||
#
|
||||
# protected:
|
||||
# int j;
|
||||
# /* comment */
|
||||
# public:
|
||||
# foo() {}
|
||||
#
|
||||
# private:
|
||||
# protected:
|
||||
# };
|
||||
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||
|
||||
# true: false:
|
||||
# namespace a { vs. namespace a {
|
||||
# foo(); foo();
|
||||
# bar(); bar();
|
||||
# } // namespace a }
|
||||
FixNamespaceComments: true
|
||||
|
||||
# false: true:
|
||||
# class C { vs. class C {
|
||||
# class D { class D {
|
||||
# void bar(); void bar();
|
||||
# protected: protected:
|
||||
# D(); D();
|
||||
# }; };
|
||||
# public: public:
|
||||
# C(); C();
|
||||
# }; };
|
||||
# void foo() { void foo() {
|
||||
# return 1; return 1;
|
||||
# } }
|
||||
IndentAccessModifiers: false
|
||||
|
||||
# false: true:
|
||||
# switch (fool) { vs. switch (fool) {
|
||||
# case 1: { case 1:
|
||||
# bar(); {
|
||||
# } break; bar();
|
||||
# default: { }
|
||||
# plop(); break;
|
||||
# } default:
|
||||
# } {
|
||||
# plop();
|
||||
# }
|
||||
# }
|
||||
IndentCaseBlocks: false
|
||||
|
||||
# false: true:
|
||||
# switch (fool) { vs. switch (fool) {
|
||||
# case 1: case 1:
|
||||
# bar(); bar();
|
||||
# break; break;
|
||||
# default: default:
|
||||
# plop(); plop();
|
||||
# } }
|
||||
IndentCaseLabels: true
|
||||
|
||||
# extern "C" {
|
||||
# void foo();
|
||||
# }
|
||||
IndentExternBlock: NoIndent
|
||||
|
||||
# Indents directives before the hash.
|
||||
#
|
||||
# #if FOO
|
||||
# #if BAR
|
||||
# #include <foo>
|
||||
# #endif
|
||||
# #endif
|
||||
IndentPPDirectives: BeforeHash
|
||||
|
||||
# true: false:
|
||||
# if (foo) { vs. if (foo) {
|
||||
# bar();
|
||||
# bar(); }
|
||||
# }
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
|
||||
# The maximum number of consecutive empty lines to keep.
|
||||
#
|
||||
# MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0
|
||||
# int f() { int f() {
|
||||
# int = 1; int i = 1;
|
||||
# i = foo();
|
||||
# i = foo(); return i;
|
||||
# }
|
||||
# return i;
|
||||
# }
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
|
||||
# Put all constructor initializers on the current line if they fit. Otherwise,
|
||||
# put each one on its own line.
|
||||
#
|
||||
# Constructor() : a(), b()
|
||||
#
|
||||
# Constructor()
|
||||
# : aaaaaaaaaaaaaaaaaaaa(),
|
||||
# bbbbbbbbbbbbbbbbbbbb(),
|
||||
# ddddddddddddd()
|
||||
PackConstructorInitializers: CurrentLine
|
||||
PointerAlignment: Right
|
||||
|
||||
# false:
|
||||
# // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information
|
||||
# /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */
|
||||
#
|
||||
# true:
|
||||
# // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
|
||||
# // information
|
||||
# /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
|
||||
# * information */
|
||||
ReflowComments: true
|
||||
|
||||
# false: true:
|
||||
#
|
||||
# if (isa<FunctionDecl>(D)) { vs. if (isa<FunctionDecl>(D))
|
||||
# handleFunctionDecl(D); handleFunctionDecl(D);
|
||||
# } else if (isa<VarDecl>(D)) { else if (isa<VarDecl>(D))
|
||||
# handleVarDecl(D); handleVarDecl(D);
|
||||
# }
|
||||
#
|
||||
# if (isa<VarDecl>(D)) { vs. if (isa<VarDecl>(D)) {
|
||||
# for (auto *A : D.attrs()) { for (auto *A : D.attrs())
|
||||
# if (shouldProcessAttr(A)) { if (shouldProcessAttr(A))
|
||||
# handleAttr(A); handleAttr(A);
|
||||
# } }
|
||||
# }
|
||||
# }
|
||||
#
|
||||
# if (isa<FunctionDecl>(D)) { vs. if (isa<FunctionDecl>(D))
|
||||
# for (auto *A : D.attrs()) { for (auto *A : D.attrs())
|
||||
# handleAttr(A); handleAttr(A);
|
||||
# }
|
||||
# }
|
||||
#
|
||||
# if (auto *D = (T)(D)) { vs. if (auto *D = (T)(D)) {
|
||||
# if (shouldProcess(D)) { if (shouldProcess(D))
|
||||
# handleVarDecl(D); handleVarDecl(D);
|
||||
# } else { else
|
||||
# markAsIgnored(D); markAsIgnored(D);
|
||||
# } }
|
||||
# }
|
||||
#
|
||||
# if (a) { vs. if (a)
|
||||
# b(); b();
|
||||
# } else { else if (c)
|
||||
# if (c) { d();
|
||||
# d(); else
|
||||
# } else { e();
|
||||
# e();
|
||||
# }
|
||||
# }
|
||||
RemoveBracesLLVM: true
|
||||
|
||||
# Never v.s. Always
|
||||
# #include <cstring> #include <cstring>
|
||||
# struct Foo {
|
||||
# int a, b, c; struct Foo {
|
||||
# }; int a, b, c;
|
||||
# namespace Ns { };
|
||||
# class Bar {
|
||||
# public: namespace Ns {
|
||||
# struct Foobar { class Bar {
|
||||
# int a; public:
|
||||
# int b; struct Foobar {
|
||||
# }; int a;
|
||||
# private: int b;
|
||||
# int t; };
|
||||
# int method1() {
|
||||
# // ... private:
|
||||
# } int t;
|
||||
# enum List {
|
||||
# ITEM1, int method1() {
|
||||
# ITEM2 // ...
|
||||
# }; }
|
||||
# template<typename T>
|
||||
# int method2(T x) { enum List {
|
||||
# // ... ITEM1,
|
||||
# } ITEM2
|
||||
# int i, j, k; };
|
||||
# int method3(int par) {
|
||||
# // ... template<typename T>
|
||||
# } int method2(T x) {
|
||||
# }; // ...
|
||||
# class C {}; }
|
||||
# }
|
||||
# int i, j, k;
|
||||
#
|
||||
# int method3(int par) {
|
||||
# // ...
|
||||
# }
|
||||
# };
|
||||
#
|
||||
# class C {};
|
||||
# }
|
||||
SeparateDefinitionBlocks: Always
|
||||
|
||||
# true: false:
|
||||
# int a = 5; vs. int a= 5;
|
||||
# a += 42; a+= 42;
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
|
||||
# Put a space before opening parentheses only after control statement keywords
|
||||
# (for/if/while...).
|
||||
#
|
||||
# void f() {
|
||||
# if (true) {
|
||||
# f();
|
||||
# }
|
||||
# }
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpacesBeforeTrailingComments: 1
|
||||
|
||||
# static_cast<int>(arg);
|
||||
# std::function<void(int)> fct;
|
||||
SpacesInAngles: Never
|
||||
|
||||
Standard: Auto
|
||||
|
||||
# Macros which are ignored in front of a statement, as if they were an
|
||||
# attribute. So that they are not parsed as identifier, for example for Qts
|
||||
# emit.
|
||||
# unsigned char data = 'x';
|
||||
# emit signal(data); // This is parsed as variable declaration.
|
||||
#
|
||||
# vs.
|
||||
#
|
||||
# unsigned char data = 'x';
|
||||
# emit signal(data); // Now it's fine again.
|
||||
StatementAttributeLikeMacros: [emit]
|
||||
---
|
16
part1/build.bat
Normal file
16
part1/build.bat
Normal file
@ -0,0 +1,16 @@
|
||||
@echo off
|
||||
set script_dir_backslash=%~dp0
|
||||
set script_dir=%script_dir_backslash:~0,-1%
|
||||
set build_dir=%script_dir%\build
|
||||
if not exist %build_dir% mkdir %build_dir%
|
||||
|
||||
pushd build
|
||||
|
||||
copy /Y %script_dir%\listing_0037_single_register_mov %build_dir% 1>NUL
|
||||
copy /Y %script_dir%\listing_0038_many_register_mov %build_dir% 1>NUL
|
||||
copy /Y %script_dir%\listing_0039_more_movs %build_dir% 1>NUL
|
||||
copy /Y %script_dir%\listing_0040_challenge_movs %build_dir% 1>NUL
|
||||
|
||||
cl %script_dir%\sim8086.c /W4 /WX /Z7 /nologo
|
||||
|
||||
popd
|
1
part1/listing_0037_single_register_mov
Normal file
1
part1/listing_0037_single_register_mov
Normal file
@ -0,0 +1 @@
|
||||
菓
|
19
part1/listing_0037_single_register_mov.asm
Normal file
19
part1/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
|
1
part1/listing_0038_many_register_mov
Normal file
1
part1/listing_0038_many_register_mov
Normal file
@ -0,0 +1 @@
|
||||
‰Ùˆå‰Ú‰Þ‰ûˆÈˆí‰Ã‰ó‰ü‰Å
|
29
part1/listing_0038_many_register_mov.asm
Normal file
29
part1/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
|
BIN
part1/listing_0039_more_movs
Normal file
BIN
part1/listing_0039_more_movs
Normal file
Binary file not shown.
45
part1/listing_0039_more_movs.asm
Normal file
45
part1/listing_0039_more_movs.asm
Normal file
@ -0,0 +1,45 @@
|
||||
; ========================================================================
|
||||
;
|
||||
; (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 39
|
||||
; ========================================================================
|
||||
|
||||
bits 16
|
||||
|
||||
; Register-to-register
|
||||
mov si, bx
|
||||
mov dh, al
|
||||
|
||||
; 8-bit immediate-to-register
|
||||
mov cx, 12
|
||||
mov cx, -12
|
||||
|
||||
; 16-bit immediate-to-register
|
||||
mov dx, 3948
|
||||
mov dx, -3948
|
||||
|
||||
; Source address calculation
|
||||
mov al, [bx + si]
|
||||
mov bx, [bp + di]
|
||||
mov dx, [bp]
|
||||
|
||||
; Source address calculation plus 8-bit displacement
|
||||
mov ah, [bx + si + 4]
|
||||
|
||||
; Source address calculation plus 16-bit displacement
|
||||
mov al, [bx + si + 4999]
|
||||
|
||||
; Dest address calculation
|
||||
mov [bx + di], cx
|
||||
mov [bp + si], cl
|
||||
mov [bp], ch
|
BIN
part1/listing_0040_challenge_movs
Normal file
BIN
part1/listing_0040_challenge_movs
Normal file
Binary file not shown.
38
part1/listing_0040_challenge_movs.asm
Normal file
38
part1/listing_0040_challenge_movs.asm
Normal file
@ -0,0 +1,38 @@
|
||||
; ========================================================================
|
||||
;
|
||||
; (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 40
|
||||
; ========================================================================
|
||||
|
||||
bits 16
|
||||
|
||||
; Signed displacements
|
||||
mov ax, [bx + di - 37]
|
||||
mov [si - 300], cx
|
||||
mov dx, [bx - 32]
|
||||
|
||||
; Explicit sizes
|
||||
mov [bp + di], byte 7
|
||||
mov [di + 901], word 347
|
||||
|
||||
; Direct address
|
||||
mov bp, [5]
|
||||
mov bx, [3458]
|
||||
|
||||
; Memory-to-accumulator test
|
||||
mov ax, [2555]
|
||||
mov ax, [16]
|
||||
|
||||
; Accumulator-to-memory test
|
||||
mov [2554], ax
|
||||
mov [15], ax
|
279
part1/sim8086.c
Normal file
279
part1/sim8086.c
Normal file
@ -0,0 +1,279 @@
|
||||
#define WIN32_MEAN_AND_LEAN
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct S86_Buffer S86_Buffer;
|
||||
struct S86_Buffer {
|
||||
char *data;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
typedef struct S86_Str8 S86_Str8;
|
||||
struct S86_Str8 {
|
||||
char *data;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
typedef struct S86_Globals S86_Globals;
|
||||
struct S86_Globals {
|
||||
HANDLE stdout_handle;
|
||||
};
|
||||
|
||||
S86_Globals s86_globals;
|
||||
|
||||
#define S86_ASSERT(expr) if (!(expr)) { __debugbreak(); }
|
||||
#define S86_ARRAY_UCOUNT(array) sizeof((array))/sizeof((array)[0])
|
||||
#define S86_STR8(string) (S86_Str8){.data = (string), .size = S86_ARRAY_UCOUNT(string) - 1 }
|
||||
#define S86_STR8_FMT(string) (int)((string).size), (string).data
|
||||
#define S86_CAST(Type) (Type)
|
||||
|
||||
S86_Buffer S86_ReadFile(char const *file_path)
|
||||
{
|
||||
S86_Buffer result = {0};
|
||||
|
||||
// NOTE: Determine file size
|
||||
// =========================================================================
|
||||
WIN32_FILE_ATTRIBUTE_DATA file_attrib_data = {0};
|
||||
if (GetFileAttributesEx(file_path, GetFileExInfoStandard, &file_attrib_data) == 0)
|
||||
return result;
|
||||
|
||||
// NOTE: Open file
|
||||
// =========================================================================
|
||||
HANDLE file_handle = CreateFile(
|
||||
/*LPCSTR lpFileName*/ file_path,
|
||||
/*DWORD dwDesiredAccess*/ GENERIC_READ,
|
||||
/*DWORD dwShareMode*/ 0,
|
||||
/*LPSECURITY_ATTRIBUTES lpSecurityAttributes*/ NULL,
|
||||
/*DWORD dwCreationDisposition*/ OPEN_EXISTING,
|
||||
/*DWORD dwFlagsAndAttributes*/ 0,
|
||||
/*HANDLE hTemplateFile*/ NULL
|
||||
);
|
||||
|
||||
if (file_handle == INVALID_HANDLE_VALUE)
|
||||
return result;
|
||||
|
||||
// NOTE: Allocate buffer
|
||||
// =========================================================================
|
||||
uint64_t file_size = (uint64_t)file_attrib_data.nFileSizeHigh << 32 | (uint64_t)file_attrib_data.nFileSizeLow << 0;
|
||||
S86_ASSERT(file_size < (DWORD)-1);
|
||||
char *buffer = VirtualAlloc(
|
||||
/*LPVOID lpAddress*/ NULL,
|
||||
/*SIZE_T dwSize*/ file_size,
|
||||
/*DWORD flAllocationType*/ MEM_COMMIT | MEM_RESERVE,
|
||||
/*DWORD flProtect*/ PAGE_READWRITE
|
||||
);
|
||||
|
||||
if (!buffer)
|
||||
goto end;
|
||||
|
||||
// NOTE: Read file to buffer
|
||||
// =========================================================================
|
||||
DWORD bytes_read = 0;
|
||||
BOOL read_file_result = ReadFile(
|
||||
/*HANDLE hFile*/ file_handle,
|
||||
/*LPVOID lpBuffer*/ buffer,
|
||||
/*DWORD nNumberOfBytesToRead*/ S86_CAST(DWORD)file_size,
|
||||
/*LPDWORD lpNumberOfBytesRead*/ &bytes_read,
|
||||
/*LPOVERLAPPED lpOverlapped*/ NULL
|
||||
);
|
||||
|
||||
// NOTE: Handle read result
|
||||
// =========================================================================
|
||||
if (read_file_result == 0) {
|
||||
VirtualFree(buffer, 0, MEM_RELEASE);
|
||||
} else {
|
||||
result.data = buffer;
|
||||
result.size = file_size;
|
||||
}
|
||||
|
||||
end:
|
||||
CloseHandle(file_handle);
|
||||
return result;
|
||||
};
|
||||
|
||||
bool S86_BufferIsValid(S86_Buffer buffer)
|
||||
{
|
||||
bool result = buffer.data && buffer.size;
|
||||
return result;
|
||||
}
|
||||
|
||||
void S86_PrintLn(S86_Str8 string)
|
||||
{
|
||||
if (s86_globals.stdout_handle == NULL)
|
||||
s86_globals.stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
|
||||
S86_ASSERT(string.size < S86_CAST(DWORD)-1);
|
||||
DWORD chars_written = 0;
|
||||
WriteConsoleA(s86_globals.stdout_handle, string.data, (DWORD)string.size, &chars_written, NULL);
|
||||
WriteConsoleA(s86_globals.stdout_handle, "\n", 1, &chars_written, NULL);
|
||||
}
|
||||
|
||||
void S86_PrintLnFmt(char const *fmt, ...)
|
||||
{
|
||||
va_list args, args_copy;
|
||||
va_start(args, fmt);
|
||||
|
||||
va_copy(args_copy, args);
|
||||
int string_size = vsnprintf(NULL, 0, fmt, args_copy);
|
||||
va_end(args_copy);
|
||||
|
||||
char buffer[8192];
|
||||
S86_ASSERT(string_size >= 0 && string_size < S86_ARRAY_UCOUNT(buffer));
|
||||
if (string_size) {
|
||||
vsnprintf(buffer, sizeof(buffer), fmt, args);
|
||||
S86_Str8 string = {.data = buffer, .size = string_size};
|
||||
S86_PrintLn(string);
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
typedef enum S86_ModEncoding S86_ModEncoding;
|
||||
enum S86_ModEncoding {
|
||||
S86_ModEncoding_MemModeNoDisplace = 0b00,
|
||||
S86_ModEncoding_MemMode8 = 0b01,
|
||||
S86_ModEncoding_MemMode16 = 0b10,
|
||||
S86_ModEncoding_RegisterMode = 0b11,
|
||||
};
|
||||
|
||||
S86_Str8 REGISTER_FIELD_ENCODING[2][8] = {
|
||||
[0b0] =
|
||||
{
|
||||
S86_STR8("AL"),
|
||||
S86_STR8("CL"),
|
||||
S86_STR8("DL"),
|
||||
S86_STR8("BL"),
|
||||
S86_STR8("AH"),
|
||||
S86_STR8("CH"),
|
||||
S86_STR8("DH"),
|
||||
S86_STR8("BH"),
|
||||
},
|
||||
[0b1] =
|
||||
{
|
||||
S86_STR8("AX"),
|
||||
S86_STR8("CX"),
|
||||
S86_STR8("DX"),
|
||||
S86_STR8("BX"),
|
||||
S86_STR8("SP"),
|
||||
S86_STR8("BP"),
|
||||
S86_STR8("SI"),
|
||||
S86_STR8("DI"),
|
||||
},
|
||||
};
|
||||
|
||||
typedef enum S86_InstructionType S86_InstructionType;
|
||||
enum S86_InstructionType {
|
||||
S86_InstructionType_MOVRegOrMemToOrFromReg,
|
||||
S86_InstructionType_MOVImmediateToRegOrMem,
|
||||
S86_InstructionType_MOVImmediateToReg,
|
||||
S86_InstructionType_MOVMemToAccum,
|
||||
S86_InstructionType_MOVAccumToMem,
|
||||
S86_InstructionType_MOVRegOrMemToSegReg,
|
||||
S86_InstructionType_MOVSegRegToRegOrMem,
|
||||
S86_InstructionType_Count,
|
||||
};
|
||||
|
||||
typedef struct S86_Instruction S86_Instruction;
|
||||
struct S86_Instruction {
|
||||
uint16_t op_mask;
|
||||
uint16_t op_bits;
|
||||
} S86_INSTRUCTIONS[S86_InstructionType_Count] = {
|
||||
[S86_InstructionType_MOVRegOrMemToOrFromReg] = {.op_mask = 0b1111'1100'0000'0000, .op_bits = 0b1000'1000'0000'0000},
|
||||
[S86_InstructionType_MOVImmediateToRegOrMem] = {.op_mask = 0b1111'1110'0011'1000, .op_bits = 0b1100'0110'0000'0000},
|
||||
[S86_InstructionType_MOVImmediateToReg] = {.op_mask = 0b1111'0000'0000'0000, .op_bits = 0b1011'0000'0000'0000},
|
||||
[S86_InstructionType_MOVMemToAccum] = {.op_mask = 0b1111'1110'0000'0000, .op_bits = 0b1010'0000'0000'0000},
|
||||
[S86_InstructionType_MOVAccumToMem] = {.op_mask = 0b1111'1110'0000'0000, .op_bits = 0b1010'0010'0000'0000},
|
||||
[S86_InstructionType_MOVRegOrMemToSegReg] = {.op_mask = 0b1111'1111'0010'0000, .op_bits = 0b1000'1110'0000'0000},
|
||||
[S86_InstructionType_MOVSegRegToRegOrMem] = {.op_mask = 0b1111'1111'0010'0000, .op_bits = 0b1000'1100'0000'0000},
|
||||
};
|
||||
|
||||
typedef enum S86_OpDataSize S86_OpDataSize;
|
||||
enum S86_OpDataSize { S86_OpDataSize_Byte, S86_OpDataSize_Word };
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
S86_PrintLn(S86_STR8("usage: sim8086.exe <binary asm file>"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
char const *file_path = argv[1];
|
||||
S86_Buffer buffer = S86_ReadFile(file_path);
|
||||
if (!S86_BufferIsValid(buffer)) {
|
||||
S86_PrintLnFmt("File read failed [path=\"%s\"]", argv[1], buffer.size);
|
||||
return -1;
|
||||
}
|
||||
|
||||
S86_ASSERT(buffer.size % 2 == 0); // We expect 2 byte instructions
|
||||
for (size_t buffer_index = 0; buffer_index < (buffer.size / 2); buffer_index++) {
|
||||
char byte0 = buffer.data[buffer_index + 0];
|
||||
char byte1 = buffer.data[buffer_index + 1];
|
||||
uint16_t byte01 = (uint16_t)byte0 << 8 | (uint16_t)byte1 << 0;
|
||||
|
||||
for (size_t instruction_index = 0;
|
||||
instruction_index < S86_ARRAY_UCOUNT(S86_INSTRUCTIONS);
|
||||
instruction_index++)
|
||||
{
|
||||
S86_Instruction instruction = S86_INSTRUCTIONS[instruction_index];
|
||||
if ((byte01 & instruction.op_mask) != instruction.op_bits)
|
||||
continue;
|
||||
|
||||
S86_InstructionType type = S86_CAST(S86_InstructionType)instruction_index;
|
||||
switch (type) {
|
||||
case S86_InstructionType_MOVRegOrMemToOrFromReg: {
|
||||
uint8_t d = (byte0 & 0b0000'0010) >> 1;
|
||||
uint8_t w = (byte0 & 0b0000'0001) >> 0;
|
||||
uint8_t mod = (byte1 & 0b1100'0000) >> 6;
|
||||
uint8_t reg = (byte1 & 0b0011'1000) >> 3;
|
||||
uint8_t rm = (byte1 & 0b0000'0111) >> 0;
|
||||
|
||||
uint8_t instr_dest = d ? reg : rm;
|
||||
uint8_t instr_src = d ? rm : reg;
|
||||
|
||||
S86_OpDataSize data_size = w ? S86_OpDataSize_Word : S86_OpDataSize_Byte;
|
||||
S86_ASSERT(mod == 0b11); // register-to-register
|
||||
|
||||
S86_Str8 src_register = REGISTER_FIELD_ENCODING[w][instr_src];
|
||||
S86_Str8 dest_register = REGISTER_FIELD_ENCODING[w][instr_dest];
|
||||
|
||||
S86_PrintLnFmt("MOV %.*s %.*s", S86_STR8_FMT(src_register), S86_STR8_FMT(dest_register));
|
||||
|
||||
#if 0
|
||||
if (mod == 0b01) {
|
||||
// 8 bit displacement
|
||||
} else if (mode == 0b10) {
|
||||
// 16 bit displacement
|
||||
}
|
||||
#endif
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVImmediateToRegOrMem: {
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVImmediateToReg: {
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVMemToAccum: {
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVAccumToMem: {
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVRegOrMemToSegReg: {
|
||||
} break;
|
||||
|
||||
case S86_InstructionType_MOVSegRegToRegOrMem: {
|
||||
} break;
|
||||
|
||||
default: {
|
||||
S86_ASSERT(!"Unknown instruction");
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user