c-programming

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

commit 002314f842bfa3d514890b14b9c9b8dd2ee29322
parent e52a6bd1707b421bf33e18bf0816f644334b05fd
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 13 Oct 2026 19:19:53 -0500

Passing final check

Diffstat:
Mperf-aware-programming/8086-simulator/sim.cpp | 36++++++++++++++++++++++++++++++++----
1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/perf-aware-programming/8086-simulator/sim.cpp b/perf-aware-programming/8086-simulator/sim.cpp @@ -123,6 +123,17 @@ uint16_t valueAt(std::vector<std::pair<std::string,uint16_t>> registers, int idx return registers[idx/2].second; } + +uint16_t setSignFlag(bool value, uint16_t original) { + original = original & 0b1101111111111111; + return original + (value << 13); +} + +uint16_t setZeroFlag(bool value, uint16_t original) { + original = original & 0b0111111111111111; + return original + (value << 15); +} + int8_t handleInstruction(std::string source, std::string dest, std::vector<std::pair<std::string,uint16_t>>& registers, Mnemonic mn) { auto reg = getReg(source); if(reg != -1) { @@ -148,13 +159,21 @@ int8_t handleInstruction(std::string source, std::string dest, std::vector<std:: if(mn == ADD) { current += before; } - if(mn == SUB) { + if(mn == SUB || mn == CMP) { current = before - current; + auto pf = registers[8].second; + registers[8].second = setZeroFlag(current == 0,registers[8].second); + registers[8].second = setSignFlag((current & 0x8000) != 0,registers[8].second); + printf("\t; flag: 0x%.2X->0x%.2X", pf , registers[8].second); } if(mn != CMP) { registers[reg/2].second = current; - printf("\t; 0x%.2X->0x%.2X", before , current); + if(mn == SUB) { + printf("\t\tval: 0x%.2X->0x%.2X", before , current); + } else { + printf("\t; \t\t\t\tval: 0x%.2X->0x%.2X", before , current); + } } } } catch (...) { @@ -170,13 +189,22 @@ int8_t handleInstruction(std::string source, std::string dest, std::vector<std:: if(mn == ADD) { current += before; } - if(mn == SUB || mn == CMP) { // cmp is sub just for flags + + if(mn == SUB || mn == CMP) { current = before - current; + auto pf = registers[8].second; + registers[8].second = setZeroFlag(current == 0,registers[8].second); + registers[8].second = setSignFlag((current & 0x8000) != 0,registers[8].second); + printf("\t; flag: 0x%.2X->0x%.2X", pf , registers[8].second); } if(mn != CMP) { registers[reg/2].second = current; - printf("\t; 0x%.2X->0x%.2X", before , current); + if(mn == SUB) { + printf("\t\tval: 0x%.2X->0x%.2X", before , current); + } else { + printf("\t; \t\t\t\tval: 0x%.2X->0x%.2X", before , current); + } } } }