Get the latest tech news
A Proper x86 Assembler in Haskell Using the Escardó-Oliva Functional
Writing an assembler turns out to be an interesting example: one needs to calculate distances between jumps and their target labels, and the target may appear after the label. It turns out that one can write a one-pass assembler using the tardis monad or in Curry, using logic programming.
In our case the problem can be thought of as a single-player game; the outcome is the number of bytes required to assemble the code, which we would like to minimize. The optimal play (here, the assignment of jumps to encodings leading to the smallest code) is provided by The next part is fairly standard---we collect offsets of labels---with the subtlety that we can assume all jumps are short if they have not been specified yet; invalid assignments will be pruned later.
Or read this on Hacker News