Over the weekend I wrote some ARM! Specifically a program to read two numbers from argv
, parse,
add, convert back to a string, and write back to stdout with a syscall.
I thought it would be peaceful in zen sort of way (it was not) and maybe be a little educational (it was).
This turned out to be a good exercise, going over reading and writing arrays to the stack, looping, function calls and syscalls.
Quick takeaways:
- A program is really just a giant mutable array of bytes in memory and some mutable registers. A debugger is pretty useful here, and separately, it’s interesting to code is just data being executed
- Compiling and running ARM on x86 (with qemu) demystified some questions I had about emulation and VMs
- I overwrote all my stack frames trying to write an array because I got the stack direction wrong. Not recommended
- But the stack seems stupid cheap (just add a register and copy, though not sure how expensive the copy is), vs running a whole bunch of allocator code + maybe a syscall
Full gist (quite brittle at points)