c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

commit 517807165eaa9c87d6f492646d395a50cef30b27
parent d1149a2cdb83c6df97043cdc5e1ea46cf137c98f
Author: Andrew Laack <andrew@laack.co>
Date:   Fri, 16 Oct 2026 14:07:38 -0500

Added approximate cycle counts

Diffstat:
Mperf-aware-programming/8086-simulator/Makefile | 1+
Mperf-aware-programming/8086-simulator/sim.cpp | 28++++++++++++++++++++--------
Aperf-aware-programming/8086-simulator/source/0056.asm | 9+++++++++
3 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/perf-aware-programming/8086-simulator/Makefile b/perf-aware-programming/8086-simulator/Makefile @@ -9,3 +9,4 @@ run: build ./sim.out source/0049.asm ./sim.out source/0051.asm ./sim.out source/0051a.asm + ./sim.out source/0056.asm diff --git a/perf-aware-programming/8086-simulator/sim.cpp b/perf-aware-programming/8086-simulator/sim.cpp @@ -165,16 +165,17 @@ 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, bool word, std::vector<unsigned char>& memory) { +std::pair<std::string, int> 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) { - return source; + return {source,0}; } - return ""; + return {"", 0}; } auto reg = getReg(source); + int cycles = 0; if(reg != -1) { uint32_t cb = 0; try { @@ -196,6 +197,8 @@ std::string handleInstruction(std::string source, std::string dest, std::vector< } else { auto before = registers[reg/2].second; auto current = std::stoi(dest); + // immediate -> register + cycles = 4; if(mn == ADD) { current += before; @@ -222,6 +225,7 @@ std::string handleInstruction(std::string source, std::string dest, std::vector< } catch (...) { auto num = getReg(dest); if(num == -1) { + cycles = 14; // ea + mov reg, mem uint16_t destAddr = std::stoi(dest.substr(1,dest.size()-2)); uint16_t val1 = (memory[destAddr]); uint16_t val2 = (memory[destAddr+1]); @@ -231,14 +235,16 @@ std::string handleInstruction(std::string source, std::string dest, std::vector< auto before = registers[src/2].second; printf("\t; start in: 0x%.2X \t\tval: 0x%.2X->0x%.2X", in, before , val); registers[src/2].second = val; - return ""; + return {"",cycles}; } + cycles = 2; auto destVal = valueAt(registers,num); auto before = registers[reg/2].second; auto current = destVal; if(mn == ADD) { + cycles = 3; current += before; } @@ -262,7 +268,7 @@ std::string handleInstruction(std::string source, std::string dest, std::vector< } else { if(mn == FN) { - return ""; + return {"", 0}; } // not moving to register (e.g. store) @@ -288,7 +294,7 @@ std::string handleInstruction(std::string source, std::string dest, std::vector< memory[sourceAddr] = upper; memory[sourceAddr+1] = (lower >> 8); } - return ""; + return {"",cycles}; } int main(int argc, char** argv) { @@ -332,6 +338,7 @@ int main(int argc, char** argv) { std::unordered_map<std::string, size_t> knownFunctions{}; + int cycles = 0; for(size_t in = 0; in < lines.size() ; ) { auto& line = lines[in]; @@ -363,8 +370,10 @@ int main(int argc, char** argv) { std::cout << source; knownFunctions[source] = in; } - - std::string res = handleInstruction(source, dest, registers, mne,in, sourceP.second || destP.second, memory); + auto pr = handleInstruction(source, dest, registers, mne,in, sourceP.second || destP.second, memory); + std::string res = pr.first; + std::cout << "\tClocks: " << pr.second; + cycles += pr.second; std::cout << std::endl; if(res != "") { @@ -377,5 +386,8 @@ int main(int argc, char** argv) { std::cout << std::endl; std::cout << "FINAL REGISTERS:" << std::endl; printRegisters(registers); + std::cout << std::endl; + std::cout << "CYCLE COUNT:" << std::endl; + std::cout << '\t' << cycles << std::endl; } diff --git a/perf-aware-programming/8086-simulator/source/0056.asm b/perf-aware-programming/8086-simulator/source/0056.asm @@ -0,0 +1,9 @@ +mov bx, 1000 +mov bp, 2000 +mov si, 3000 +mov di, 4000 +mov cx, bx +mov dx, 12 +mov dx, [1000] +add cx, dx +add dx, 50