Assembler

No more hand assembling. I created a simple assembler using flex and bison. It is another piece of software I wrote from scratch for the sake of building the computer. The hobby hardware project so far has been only around software. It’s been several months now since I started all this, and I’ve been coding, and coding, and… coding some more. But it is just the way it has to be now. I need a decent software stack to design everything carefully and get the hardware up and running smoothly. Going for construction now would be a dangerous shortcut and would lead me to multiple hours wasted on tracking bugs or flaws that should never reach construction phase. So, I created the microcode assembler, the simulator, and now the assembler (so that I can assemble and run a test suite in the simulator). After the machine’s construction phase, I will return to coding with things like linker, OS and demo software. So, there is more coding to come.

I wrote the assembler from scratch. Porting readily available software seemed an overkill to me. I would definitely spend to much time and energy trying to analyze someone else’s code of a full blown assembler with functionalities I don’t need at the moment.

My assembler is as simple as it may be. It generates an absolute binary machine language dump which I intend to use for testing purposes only. It generates code for one segment, namely the code segment. It has no support for code relocation, is not able to export or import symbols, nor generate object files which could be later linked into executables understood by the future OS’s loader (currently I have no linker anyway). It supports local symbols and simple expressions consisting of symbols and immediates (decimal and hex). Most importantly, however, it works and may be easily modified whenever I add or remove instructions from the instruction set (there is a separate production for every instruction in my bison definition). In this phase of the project, it is crucial. The next step I am going to take is to create a test suite covering every singe instruction I have. Here is a sample test case for memory load and store (which is also an example of an assembly source supported by the assembler):

; test sample
; load and store

_start:
        ld a, 0x0100
        mov dp, a
        ld a, 0x1234
        st (dp:0), a    ; store value from a
        xor a, a        ; zero a
        ld a, (dp:0)    ; restore value in a
        cmp a, 0x1234
        je pass
        jmp error
pass:   halt
error:  ld a, 0xdead
        halt

The following is the assembler’s response to the above input assembly listing:

$ ./as.exe listing3.as out3.bin
Building symbol table.
Assembling.
Output is 28 bytes.
Symbols:
 0x0018:  error
 0x0017:  pass
 0x0000:  _start

Here is a list of future developments I am planning on the assembler:

  • create object files to be used by the linker instead of absolute binary (a.out, ELF, COFF or something custom)
  • provide support for code and data segments (by dedicated .code and .data keywords)
  • allow for importing external symbols and exporting own symbols (extern and export keywords)
  • allow for code relocation by the linker/loader (by means of relocation tables in object files)
  • provide support relaxation fixups (to shorten 16-bit instructions to their 8-bit versions once all symbol references are resolved by the linker/loader)

I will add things as I go, whenever I decide a certain feature becomes necessary. The current source code is available in the download section, or under this link. It is flex and bison source, and should work in every environment that has lex/flex or yacc/bison.

Leave a Reply

  

  

  

Time limit is exhausted. Please reload the CAPTCHA.