I might try doing this as a learning project. Can you offer any advice as to what those abstractions are? I'd appreciate some advice to avoid the "going back" part.
Re:An x86 Assembler in 256 LOC (2017)
by chrisseaton on Monday May 27, 2019 @08:06PM
(#20026164)
See this real-world assembler for example
https://github.com/oracle/graal/tree/master/compiler/src/org...
Then take a random instruction
https://github.com/oracle/graal/blob/master/compiler/src/org...
See how it has common methods used by other instructions simdPrefix and emitModRM? Those are the abstractions.
What are the proper abstractions necessary?
Re:An x86 Assembler in 256 LOC (2017)
by chrisseaton on Monday May 27, 2019 @08:04PM
(#20026152)
Most simple cases of instructions seem straightforward to just go ahead and emit the bytes for. Then when you start to use more addressing modes you realise it gets to be a lot of code and it turns out there's a common pattern for everything.
Here's a concrete example from an industrial assembler.
Almost every simple instruction boils down this helper method which is parameterised by a bunch of flags and can then deal with all of them.
https://github.com/oracle/graal/blob/f3a3576493f87abdea1045d...
Even that looks complicated! But writing this per instruction (the simple case) would be insanely complicated.
Re:An x86 Assembler in 256 LOC (2017)
by userbinator on Monday May 27, 2019 @08:41PM
(#20026290)
Ideally, the common fields and patterns noted by the manufacturer in the ISA description would be implemented, and then the remainder of the logic can be (relatively) simple table-lookup.
So people doing something trivial thinks they're easy (like this example), people trying to do a bit more think they're really hard, and people doing an entire assembler think they're easy again.