mu/subx/Readme.md

95 lines
3.8 KiB
Markdown
Raw Normal View History

2018-07-15 22:00:38 +00:00
## What is this?
2017-10-09 08:19:33 +00:00
2018-07-15 22:00:38 +00:00
A suite of tools for directly programming in (32-bit x86) machine code without
a compiler. The generated ELF binaries require just a Unix-like kernel to run.
(It isn't self-hosted yet, so generating the binaries requires a C compiler
and libc.)
## Why in the world?
1. It seems wrong-headed that our computers look polished but are plagued by
foundational problems of security and reliability. I'd like to learn to
2018-07-19 19:18:37 +00:00
walk before I try to run. The plan: start out using the computer only to
check my program for errors rather than to hide low-level details. Force
myself to think about security by living with raw machine code for a while.
2018-07-19 20:05:48 +00:00
Reintroduce high level languages (HLLs) only after confidence is regained
in the foundations (and when the foundations are ergonomic enough to
support developing a compiler in them). Delegate only when I can verify
with confidence.
2018-07-15 22:00:38 +00:00
2. The software in our computers has grown incomprehensible. Nobody
understands it all, not even experts. Even simple programs written by a
single author require lots of time for others to comprehend. Compilers are
a prime example, growing so complex that programmers have to choose to
2018-07-19 20:05:48 +00:00
either program them or use them. I think they may also contribute to the
incomprehensibility of the stack above them. I'd like to explore how much
of a HLL I can build without a monolithic optimizing compiler, and see if
deconstructing the work of the compiler can make the stack as a whole more
comprehensible to others.
2018-07-15 22:00:38 +00:00
3. I want to learn about the internals of the infrastructure we all rely on in
our lives.
## Running
```
$ git clone https://github.com/akkartik/mu
$ cd mu/subx
$ ./subx
```
Running `subx` will transparently compile it as necessary.
## Usage
`subx` currently has the following sub-commands:
* `subx test`: runs all automated tests.
* `subx translate <input file> <output ELF binary>`: translates a text file
containing hex bytes and macros into an executable ELF binary.
* `subx run <ELF binary>`: simulates running the ELF binaries emitted by `subx
translate`. Useful for debugging, and also enables more thorough testing of
`translate`.
2018-07-16 14:55:07 +00:00
Putting them together, build and run one of the example programs:
<img alt='ex1.1.subx' src='html/ex1.png'>
```
$ ./subx translate ex1.1.subx ex1
$ ./subx run ex1
```
If you're running on Linux, `ex1` will also be runnable directly:
```
$ chmod +x ex1
$ ./ex1
```
2018-07-15 22:00:38 +00:00
I'm not building general infrastructure here for all of the x86 ISA and ELF
format. SubX is about programming with a small, regular subset of 32-bit x86:
2017-07-12 04:41:15 +00:00
2018-07-07 16:36:24 +00:00
* Only instructions that operate on the 32-bit E\*X registers. (No
2017-10-16 20:52:54 +00:00
floating-point yet.)
2017-10-16 10:22:30 +00:00
* Only instructions that assume a flat address space; no instructions that use
segment registers.
* No instructions that check the carry or parity flags; arithmetic operations
2017-10-16 20:52:54 +00:00
always operate on signed integers (while bitwise operations always operate
on unsigned integers)
2017-10-16 10:22:30 +00:00
* Only relative jump instructions (with 8-bit or 16-bit offsets).
2017-10-16 05:55:25 +00:00
2018-07-15 22:00:38 +00:00
## Resources
2017-10-11 06:35:41 +00:00
2018-07-15 22:21:51 +00:00
* [Single-page cheatsheet for the x86 ISA](https://net.cs.uni-bonn.de/fileadmin/user_upload/plohmann/x86_opcode_structure_and_instruction_overview.pdf)
(pdf; [cached local copy](https://github.com/akkartik/mu/blob/master/subx/cheatsheet.pdf))
2018-07-15 22:00:38 +00:00
* [Concise reference for the x86 ISA](https://c9x.me/x86)
2017-10-11 06:35:41 +00:00
* [Intel programming manual](http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf) (pdf)
2018-07-15 22:09:48 +00:00
## Inspirations
2018-07-15 22:00:38 +00:00
* [&ldquo;Creating tiny ELF executables&rdquo;](https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html)
* [&ldquo;Bootstrapping a compiler from nothing&rdquo;](http://web.archive.org/web/20061108010907/http://www.rano.org/bcompiler.html)
2018-07-15 22:09:48 +00:00
* Forth implementations like [StoneKnifeForth](https://github.com/kragen/stoneknifeforth)