cpp-programming

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

commit 9f093fbcb708477da1b0a4c3f3111b50e807fc48
parent ed6d8cd0e9ec043f252235e9365b96383bcbf9e7
Author: Andrew Laack <andrew@laack.co>
Date:   Mon, 19 Oct 2026 23:02:44 -0500

Started working on lox interpreter

Diffstat:
A.gitignore | 2++
Alox/Makefile | 4++++
Alox/main.cpp | 40++++++++++++++++++++++++++++++++++++++++
Alox/main.lox | 1+
Aperf/8086-simulator/Makefile | 12++++++++++++
Aperf/8086-simulator/sim.cpp | 393+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aperf/8086-simulator/source/0043.asm | 8++++++++
Aperf/8086-simulator/source/0044.asm | 12++++++++++++
Aperf/8086-simulator/source/0046.asm | 8++++++++
Aperf/8086-simulator/source/0048.asm | 5+++++
Aperf/8086-simulator/source/0049.asm | 6++++++
Aperf/8086-simulator/source/0051.asm | 4++++
Aperf/8086-simulator/source/0051a.asm | 10++++++++++
Aperf/8086-simulator/source/0056.asm | 9+++++++++
Aperf/8086-simulator/source/og/listing_0043_immediate_movs.asm | 27+++++++++++++++++++++++++++
Aperf/8086-simulator/source/og/listing_0044_register_movs.asm | 32++++++++++++++++++++++++++++++++
16 files changed, 573 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +*.out +*.swp diff --git a/lox/Makefile b/lox/Makefile @@ -0,0 +1,4 @@ +build: + g++ main.cpp -o lox.out +run: build + ./lox.out main.lox diff --git a/lox/main.cpp b/lox/main.cpp @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <iostream> +#include <fstream> +#include <filesystem> + +std::string readFileToString(std::string filePath) { + auto in = std::ifstream(filePath); + if (in.fail()) { + // TODO: Handle + } + std::ostringstream sstr; + sstr << in.rdbuf(); + return sstr.str(); +} + +void runPrompt() { + printf("> "); + std::string inp; + std::cin >> inp; + std::cout << inp << std::endl; +} + +void runFile(char* file) { + std::string fileString = readFileToString(file); + printf(fileString.c_str()); +} + +int main (int argc, char** argv) { + if(argc > 2) { + printf("usage: lox [source]\n"); + return 64; + } + if(argc == 1) { + runPrompt(); + } + else { + runFile(argv[1]); + } + +} diff --git a/lox/main.lox b/lox/main.lox @@ -0,0 +1 @@ +print "hello world!" diff --git a/perf/8086-simulator/Makefile b/perf/8086-simulator/Makefile @@ -0,0 +1,12 @@ +build: + g++ sim.cpp -o sim.out + +run: build + ./sim.out source/0043.asm + ./sim.out source/0044.asm + ./sim.out source/0046.asm + ./sim.out source/0048.asm + ./sim.out source/0049.asm + ./sim.out source/0051.asm + ./sim.out source/0051a.asm + ./sim.out source/0056.asm diff --git a/perf/8086-simulator/sim.cpp b/perf/8086-simulator/sim.cpp @@ -0,0 +1,393 @@ +#include <cstdint> +#include <filesystem> +#include <unordered_map> +#include <fstream> +#include <string> +#include <vector> +#include <unistd.h> +#include <iostream> + +enum Mnemonic { + MOV, + ADD, + SUB, + CMP, + JNZ, + FN, + UNKNOWN +}; + +void printMnemonic(Mnemonic mn) { + switch(mn) { + case (MOV): + std::cout << "mov"; + break; + case (SUB): + std::cout << "sub"; + break; + case (ADD): + std::cout << "add"; + break; + case (CMP): + std::cout << "cmp"; + break; + case (JNZ): + std::cout << "jnz"; + break; + case (UNKNOWN): + std::cout << "unknown"; + break; + } +} + +int isHalfReg(std::string reg) { + if(reg[1] == 'l' || reg[1] == 'h') { + return 1; + } + return 0; +} + +Mnemonic getMnemonic(std::string line) { + if(line.substr(0,3) == "mov") { + return MOV; + } + if(line.substr(0,3) == "add") { + return ADD; + } + if(line.substr(0,3) == "sub") { + return SUB; + } + if(line.substr(0,3) == "cmp") { + return CMP; + } + if(line.substr(0,3) == "jnz") { + return JNZ; + } + if(line.find(":") != std::string::npos) { + return FN; + } + return UNKNOWN; +} + +std::string readFileToString(std::string filePath) { + auto in = std::ifstream(filePath); + std::ostringstream sstr; + sstr << in.rdbuf(); + return sstr.str(); +} + + +// assumes entire instruction is input +std::pair<std::string, bool> parseDestination(std::string st) { + std::string source = st.substr(st.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::pair<std::string, bool> parseSource(std::string st, Mnemonic mn) { + if(mn == FN) { + return {st.substr(0,st.size() - 1), false}; + } + else { + std::string source = st.substr(st.find(' ')+1); + source = source.substr(0,source.find(',')); + bool word = false; + if(source.substr(0,4) == "word") { + source = source.substr(5); + word = true; + } + return {source, word}; + } +} + +void printRegisters(std::vector<std::pair<std::string,uint16_t>> registers) { + for(auto& reg : registers) { + printf("\t%s:\t0x%.2X\n", reg.first.c_str(), reg.second); + } +} + + +// return -1 for not register or byte offset into register vector +int8_t getReg(std::string location) { + if (location == "ax") { + return 0; + } + if (location == "bx") { + return 2; + } + if (location == "cx") { + return 4; + } + if (location == "dx") { + return 6; + } + if (location == "sp") { + return 8; + } + if (location == "bp") { + return 10; + } + if (location == "si") { + return 12; + } + if (location == "di") { + return 14; + } + + return -1; +} + + + +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); +} + +// return where to jump to +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,0}; + } + return {"", 0}; + } + + auto reg = getReg(source); + int cycles = 0; + if(reg != -1) { + uint32_t cb = 0; + try { + if(isHalfReg(source)) { + auto before = registers[reg/2].second; + auto current = registers[reg/2].second; + if(reg%2 == 1) { + // lower (right) + current -= current & 0b0000000011111111; + current += std::stoi(dest); + } else { + // upper (left) + current -= current & 0b1111111100000000; + current += std::stoi(dest) << 8; + } + registers[reg/2].second = current; + cb = current; + printf("\t; 0x%.2X->0x%.2X", before , current); + } else { + auto before = registers[reg/2].second; + auto current = std::stoi(dest); + // immediate -> register + cycles = 4; + + if(mn == ADD) { + current += before; + } + 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; start in: 0x%.2X, flag: 0x%.2X->0x%.2X", in, pf , registers[8].second); + } + + cb = current; + if(mn != CMP && mn != FN) { + registers[reg/2].second = current; + if(mn == SUB) { + printf("\t\tval: 0x%.2X->0x%.2X", before , current); + } else { + printf("\t; start in: 0x%.2X \t\t\t\tval: 0x%.2X->0x%.2X", in, before , current); + } + } + } + + } 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]); + val1 = (val1 << 8); + uint16_t val = val1 + val2; + auto src = getReg(source); + 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 {"",cycles}; + } + cycles = 2; + auto destVal = valueAt(registers,num); + + auto before = registers[reg/2].second; + auto current = destVal; + + if(mn == ADD) { + cycles = 3; + current += before; + } + + 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; start in: 0x%.2X, flag: 0x%.2X->0x%.2X", in, pf , registers[8].second); + } + + if(mn != CMP && mn != FN) { + registers[reg/2].second = current; + if(mn == SUB) { + printf("\t\tval: 0x%.2X->0x%.2X", before , current); + } else { + printf("\t; start in: 0x%.2X \t\t\t\tval: 0x%.2X->0x%.2X", in, before , current); + } + } + } + } else { + + if(mn == FN) { + return {"", 0}; + } + + // not moving to register (e.g. store) + std::string sourceVS = source.substr(1); + sourceVS = sourceVS.substr(0,sourceVS.size() - 1); + + uint16_t sourceAddr; + + if(getReg(sourceVS.substr(0,2)) != -1) { + sourceAddr = registers[getReg(sourceVS.substr(0,2)) / 2].second; + sourceAddr += std::stoi(sourceVS.substr(5)); + } else { + 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("; start in: 0x%.2X \t\t\tval: 0x%.2X->0x%.2X", memory[sourceAddr] , destV); + memory[sourceAddr] = upper; + memory[sourceAddr+1] = (lower >> 8); + } + return {"",cycles}; +} +int main(int argc, char** argv) { + + std::vector<std::pair<std::string,uint16_t>> registers{}; + registers.push_back({"ax",0}); + registers.push_back({"bx",0}); + registers.push_back({"cx",0}); + registers.push_back({"dx",0}); + registers.push_back({"sp",0}); + registers.push_back({"bp",0}); + registers.push_back({"si",0}); + 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 {}; + + std::string acc = ""; + + for(size_t i = 0; i < ftext.size(); ++i) { + if(ftext[i] == '\n') { + lines.push_back(acc); + acc = ""; + } + else { + acc += ftext[i]; + } + } + if(acc != "") { + lines.push_back(acc); + } + + + std::cout << "INSTRUCTIONS:" << std::endl; + + std::unordered_map<std::string, size_t> knownFunctions{}; + + int cycles = 0; + for(size_t in = 0; in < lines.size() ; ) { + + auto& line = lines[in]; + Mnemonic mne = getMnemonic(line); + + printMnemonic(mne); + if(mne != FN) { + std::cout << " "; + } + + 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(dest[0] == '+') { + dest = dest.substr(dest.find(']') + 3); + } + + if(mne != FN) { + if(mne != JNZ) { + + std::cout << source << ", " << dest; + } else { + std::cout << source; + } + } else { + std::cout << source; + knownFunctions[source] = in; + } + 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 != "") { + in = knownFunctions[source]; + } else { + in += 1; + } + } + + 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/8086-simulator/source/0043.asm b/perf/8086-simulator/source/0043.asm @@ -0,0 +1,8 @@ +mov ax, 1 +mov bx, 2 +mov cx, 3 +mov dx, 4 +mov sp, 5 +mov bp, 6 +mov si, 7 +mov di, 8 diff --git a/perf/8086-simulator/source/0044.asm b/perf/8086-simulator/source/0044.asm @@ -0,0 +1,12 @@ +mov ax, 1 +mov bx, 2 +mov cx, 3 +mov dx, 4 +mov sp, ax +mov bp, bx +mov si, cx +mov di, dx +mov dx, sp +mov cx, bp +mov bx, si +mov ax, di diff --git a/perf/8086-simulator/source/0046.asm b/perf/8086-simulator/source/0046.asm @@ -0,0 +1,8 @@ +mov bx, -4093 +mov cx, 3841 +sub bx, cx +mov sp, 998 +mov bp, 999 +cmp bp, sp +add bp, 1027 +sub bp, 2026 diff --git a/perf/8086-simulator/source/0048.asm b/perf/8086-simulator/source/0048.asm @@ -0,0 +1,5 @@ +mov cx, 200 +mov bx, cx +add cx, 1000 +mov bx, 2000 +sub cx, bx diff --git a/perf/8086-simulator/source/0049.asm b/perf/8086-simulator/source/0049.asm @@ -0,0 +1,6 @@ +mov cx, 3 +mov bx, 1000 +loop_start: +add bx, 10 +sub cx, 1 +jnz loop_start diff --git a/perf/8086-simulator/source/0051.asm b/perf/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/8086-simulator/source/0051a.asm b/perf/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] diff --git a/perf/8086-simulator/source/0056.asm b/perf/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 diff --git a/perf/8086-simulator/source/og/listing_0043_immediate_movs.asm b/perf/8086-simulator/source/og/listing_0043_immediate_movs.asm @@ -0,0 +1,27 @@ +; ======================================================================== +; +; (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 43 +; ======================================================================== + +bits 16 + +mov ax, 1 +mov bx, 2 +mov cx, 3 +mov dx, 4 + +mov sp, 5 +mov bp, 6 +mov si, 7 +mov di, 8 diff --git a/perf/8086-simulator/source/og/listing_0044_register_movs.asm b/perf/8086-simulator/source/og/listing_0044_register_movs.asm @@ -0,0 +1,32 @@ +; ======================================================================== +; +; (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 44 +; ======================================================================== + +bits 16 + +mov ax, 1 +mov bx, 2 +mov cx, 3 +mov dx, 4 + +mov sp, ax +mov bp, bx +mov si, cx +mov di, dx + +mov dx, sp +mov cx, bp +mov bx, si +mov ax, di