https://github.com/alexfru/regal86 Skip to content Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Project management - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} alexfru / regal86 * Notifications * Star 15 * Fork 1 Register Allocator for 8086 BSD-2-Clause License 15 stars 1 fork Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights master Switch branches/tags [ ] Branches Tags Nothing to show {{ refName }} default View all branches Nothing to show {{ refName }} default View all tags 1 branch 0 tags Code Clone HTTPS GitHub CLI [https://github.com/a] Use Git or checkout with SVN using the web URL. [gh repo clone alexfr] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio If nothing happens, download the GitHub extension for Visual Studio and try again. Go back Latest commit @alexfru alexfru Initial commit ... 8a00b4e May 9, 2021 Initial commit 8a00b4e Git stats * 1 commit Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time LICENSE Initial commit May 9, 2021 check.h Initial commit May 9, 2021 file.asm Initial commit May 9, 2021 lra86.cpp Initial commit May 9, 2021 readme.md Initial commit May 9, 2021 View code Local Register Allocator for 8086 Table of contents What is this? Motivation Scope Basic algorithm Spilling Evaluation order of subsexpressions 2-register instructions 8086-specific approach Shifts (i)mul (i)div Loads Stores Resources readme.md Local Register Allocator for 8086 Table of contents What is this? Motivation Scope Basic algorithm 8086-specific approach Implementation details Resources What is this? This is an implementation of a greedy bottom-up local register allocator for the intel 8086 CPU. It may be used as part of a simple compiler. In particular, it can be used in a C compiler whose int is 16-bit and char is 8-bit. To make things interesting and interactive it comes with a code generator capable of generating 8086 assembly code (compilable into DOS .COM programs) from trees representing expressions with 16-bit integer ALU operations, constants and memory loads and stores. A few extra instructions generated by the code check the results of expression evaluation in the output register and the output memory locations, if any. Thus you can actually execute the generated code and tweak things around to see how your changes affect code generation. Sample assembly output from the register allocator/code generator: ; 7 (vr4) ; mul (vr5) ; 5 (vr3) ; add (vr6) ; 3 (vr1) ; mul (vr2) ; 2 (vr0) ; ---- ; Regs needed (approximately): 3 ; -------- ; ; vr0 mov ax, 2 ; vr0 mov cx, 3 ; vr1 mul cx ; vr2 mov cx, 5 ; vr3 mov dx, 7 ; vr4 xchg ax, cx ; vr5 mul dx ; vr5 add cx, ax ; vr6 ; ; vr6 cmp cx, 41 ; vr6 jne failure ; vr6 N.B. This register allocator will not allocate registers perfectly in all circumstances. It will occasionally generate unnecessary register moves and exchanges. Bear in mind, optimal register allocation is an NP-complete problem. Motivation The intel 8086 architecture has a number of instructions with fixed input and/or output registers and it's not entirely trivial to connect the ins and outs of these instructions. There's obviously a long history of implementing compilers for this and similar architectures, but it's a bit odd that it's hard to find a conceptually simple algorithm like this. The "Dragon" book and many other sources either consider architectures without instructions with such fixed register operands or solve the problem using graph coloring, which for certain reasons may be impractical. The intel 8086 architecture survives to this day in the modern intel Pentium CPUs that are compatible descendants of the old 8086 and with it survive some of the instructions with fixed in/out register operands. So, the problem persists to this day, although there are fewer limitations to deal with when using newer 32-bit and 64-bit x86 instructions and registers today. 8086 registers by special uses in expression evaluation (pardon the mixture of C and assembly operators, instructions and syntax and ASCII art): +-- can be freely used in most ALU instructions as src/dst | +-- can be used as address to access memory | | +-- has individual 8-bit subregisters | | | +-- can be sign-extended with cbw | | | | +-- can be shifted | | | | | +-- can be shift count v v v v v v +-<&|^~ [r] 8bit cbw <