commit 72c1f1c700d60fcaa3aa388adaad3450d0c5b4fb
parent 0ba49b37e6e3d51bc80c1d1cd6cdf9cd923e70ac
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 14 Oct 2026 19:43:46 -0500
Working with stores
Diffstat:
4 files changed, 70 insertions(+), 9 deletions(-)
diff --git a/perf-aware-programming/8086-simulator/Makefile b/perf-aware-programming/8086-simulator/Makefile
@@ -7,3 +7,4 @@ run: build
./sim.out source/0046.asm
./sim.out source/0048.asm
./sim.out source/0049.asm
+ ./sim.out source/0051.asm
diff --git a/perf-aware-programming/8086-simulator/sim.cpp b/perf-aware-programming/8086-simulator/sim.cpp
@@ -78,20 +78,35 @@ std::string readFileToString(std::string filePath) {
// assumes entire instruction is input
-std::string parseDestination(std::string st) {
+std::pair<std::string, bool> parseDestination(std::string st) {
std::string source = st.substr(st.find(' ')+1);
- return source.substr(source.find(' ')+1);
+ if(source.substr(0,4) == "word") {
+ source = source.substr(5);
+ }
+ source = source.substr(source.find(' ')+1);
+ bool word = false;
+ if(source.substr(0,4) == "word") {
+ source = source.substr(5);
+ word = true;
+ }
+ return {source, word};
}
// assumes entire instruction is input
-std::string parseSource(std::string st, Mnemonic mn) {
+std::pair<std::string, bool> parseSource(std::string st, Mnemonic mn) {
if(mn == FN) {
- return st.substr(0,st.size() - 1);
+ return {st.substr(0,st.size() - 1), false};
}
else {
std::string source = st.substr(st.find(' ')+1);
- return source.substr(0,source.find(','));
+ source = source.substr(0,source.find(','));
+ bool word = false;
+ if(source.substr(0,4) == "word") {
+ source = source.substr(5);
+ word = true;
+ }
+ return {source, word};
}
}
@@ -150,7 +165,7 @@ uint16_t setZeroFlag(bool value, uint16_t original) {
}
// return where to jump to
-std::string handleInstruction(std::string source, std::string dest, std::vector<std::pair<std::string,uint16_t>>& registers, Mnemonic mn, uint16_t in) {
+std::string handleInstruction(std::string source, std::string dest, std::vector<std::pair<std::string,uint16_t>>& registers, Mnemonic mn, uint16_t in, bool word, std::vector<unsigned char>& memory) {
if(mn == JNZ) {
if((registers[8].second & 0b1000000000000000) == 0) {
@@ -207,6 +222,7 @@ std::string handleInstruction(std::string source, std::string dest, std::vector<
} catch (...) {
auto num = getReg(dest);
if(num == -1) {
+ std::cout << "UNFOUND REG" << std::endl;
return "";
}
auto destVal = valueAt(registers,num);
@@ -235,6 +251,26 @@ std::string handleInstruction(std::string source, std::string dest, std::vector<
}
}
}
+ } else {
+
+ if(mn == FN) {
+ return "";
+ }
+
+ // not moving to register (e.g. store)
+ std::string sourceVS = source.substr(1);
+ sourceVS = sourceVS.substr(0,sourceVS.size() - 1);
+ uint16_t sourceAddr = std::stoi(sourceVS);
+
+ uint16_t destV = std::stoi(dest);
+ // little endian so re-order bytes
+
+ uint16_t lower = (destV << 8);
+ uint16_t upper = (destV >> 8);
+ destV = lower + upper;
+
+ printf("\t;\t\t\t\tval: 0x%.2X->0x%.2X", memory[sourceAddr] , destV);
+ memory[sourceAddr] = destV;
}
return "";
}
@@ -251,6 +287,12 @@ int main(int argc, char** argv) {
registers.push_back({"di",0});
registers.push_back({"flags",0});
+ // one limitation of this is we aren't loading the instructions into memory
+ // but they really do exist there. This is a limitation of the non-binary
+ // approach where we are basically creating an 8086 interpreter.
+
+ std::vector<unsigned char> memory(1024*1024,0);
+
std::string ftext = readFileToString(argv[1]);
std::vector<std::string> lines {};
@@ -284,8 +326,12 @@ int main(int argc, char** argv) {
std::cout << " ";
}
- std::string source = parseSource(line, mne);
- std::string dest = parseDestination(line);
+ std::pair<std::string, bool> sourceP = parseSource(line, mne);
+ std::pair<std::string, bool> destP = parseDestination(line);
+
+ auto source = sourceP.first;
+ auto dest = destP.first;
+
if(mne != FN) {
if(mne != JNZ) {
std::cout << source << ", " << dest;
@@ -297,7 +343,7 @@ int main(int argc, char** argv) {
knownFunctions[source] = in;
}
- std::string res = handleInstruction(source, dest, registers, mne,in);
+ std::string res = handleInstruction(source, dest, registers, mne,in, sourceP.second || destP.second, memory);
std::cout << std::endl;
if(res != "") {
diff --git a/perf-aware-programming/8086-simulator/source/0051.asm b/perf-aware-programming/8086-simulator/source/0051.asm
@@ -0,0 +1,4 @@
+mov word [1000], 1
+mov word [1002], 2
+mov word [1004], 3
+mov word [1006], 4
diff --git a/perf-aware-programming/8086-simulator/source/0051a.asm b/perf-aware-programming/8086-simulator/source/0051a.asm
@@ -0,0 +1,10 @@
+mov word [1000], 1
+mov word [1002], 2
+mov word [1004], 3
+mov word [1006], 4
+mov bx, 1000
+mov word [bx + 4], 10
+mov bx, word [1000]
+mov cx, word [1002]
+mov dx, word [1004]
+mov bp, word [1006]