Close

Simple Assembly Tool

A project log for Suite-16

Suite-16 is a 16-bit cpu built entirely from TTL. It is a personal exploration of how hardware and software interact.

monsonitemonsonite 10/18/2019 at 19:082 Comments

Now that I have got a basic simulator running in C, I can work towards proving the Instruction Set.

However hand assembly of 16-bit instruction words gets a bit tedious - even though the bytecodes are trivial.

I decided that I would hack together a barebones tool that would help me assemble instructions from either text typed at a terminal, or sent from a text file using a terminal program such as TeraTerm.

The first thing to do was to establish a few rules:

To make the job of writing the assembler easier, I really only wanted to parse single characters - rather than the more conventional mnemonics that are usually 3 of 4 character strings - like ADD and CALL.

All of the mnemonics would be assigned a single character - which for arithmetic and logic operations is all very familiar

ADD becomes +

SUB becomes -

AND becomes &

OR    becomes |

XOR becomes ^

INV   becomes ~

So that's the arithmetical and logical instructions mostly taken care of apart from INC and DEC.

The next task was to allocate single character pseudonyms to my register set R0 to R15. 

These would each be designated a capital letter starting at A (a logical choice for the Accumulator) and ending at P, which happens to be the Program Counter.   Anyone familiar  with 8080 or Z80 code will be quite happy using B, C, D, E, F, H, L, M etc. All I have done is add a few more register names such as G, I, J, K, N and O. 

Personally, I find it much easier to remember alphabetical names rather than numbers - J and K are good choices for loop counters and M as a memory referencing register like on the 8080.  They can always be changed later depending on the application,  I might want to call my Stack Pointer S and my Return Stack pointer R at some later date. 

The assembly language syntax will be minimal - just enough to get the job done.  A register is refered to by it's name followed by some operator.  As most of the ALU operators involve a register plus the accumulator as the destination we can omit the A.

B+     ADD B to A.

B}      Move B into A   direct register addressing

B{      Move A into B

B!      Store A at the address given by B   - these are indirect register addressing using fetch @ and store ! symbols borrowed from Forth

B@   Load A from the address given by B

Numeric constants (immediate addressing) use the #  - which is borrowed from the LIT word in Forth

1234#B    Load B with 1234 which follows in the next memory location

234#B     Load B with the short constant, which is found in the payload byte

We now have to dig a little deeper for meaningful symbols

B'             INC B

B,             DEC B

B\             POP an item off the stack into A, where B is used as the stack pointer

B/             PUSH an item onto the stack, using B as the stack pointer

$              Identify the following number as hexadecimal

127=         Branch if A=0 to zeropage address 127

126<         Branch if A<0 to zeropage address 126

125>         Branch if A>0 to zeropage address 125

I have managed to implement this in less that 200 lines of C code.  This is enough to prove the concept and show that the correct instructions can be assembled from the limited syntax.  More features such as labels, origins etc can be added when required.

Discussions

monsonite wrote 10/19/2019 at 10:04 point

Hi Roelh,  Thanks for the tip. I have looked at Marco's editor-assembler, and it looks like it might prove useful. I can see the attraction of labels and near-standard mnemonics, and the visual simulation of the cpu in operation. I'm not sure how easy it would be to modify his code to simulate a 16-bit machine with more registers - that might be a whole sub-project in it's own right, as I have never written any javascript. My programming skills have not evolved much since I did some Z80 assembly in the 1980s, and a bit of trivial coding on embedded devices in C.  My approach was just to hack something together quickly in C, for simulation and assembly - that I can understand and adapt to my needs. I will however look around to see if there are alternatives.

  Are you sure? yes | no

roelh wrote 10/19/2019 at 07:46 point

Hi Monsonite, I would rather assemble by hand (as I did a long time ago for the Z80) then use this Brain****-like language. I think you will find it very hard to use. After one day of use you will want to jump to labels. Why not adapt the Javascript editor-assembler from Marco Schweighauser (https://www.mschweighauser.com/make-your-own-assembler-simulator-in-javascript-part1/ ). I'm using it now for the Kobold, and successfully used it for the relay computer and the 1-square-inch cpu. And I'm sure there are many alternatives.

  Are you sure? yes | no