Skip to content

Assembler

varkenvarken edited this page Feb 26, 2020 · 12 revisions

A small assembler is provided for the Robin SoC as a python program.

Because of the limited number of instructions and address modes the assembler is small. It supports expressions as well as user defined macros. Its output is a flat file of bytes or an Intel .hex file.

To give an idea of its current capabilities, a sample is shown below. Note that return and call are not actual cpu instructions but macros. A sample macro file is shown at the end. The assembler also comes with a bunch of built in aliases for registers (like tmp and link) as defined in this spreadsheet

start: 0x200

readline:   ; read characters until newline (10) into buffer pointer to by r2
    push r5
    move r4,r2,0
    move tmp,0,0
    move r5,0,0
    load r5,#10 ; newline
getcharloop:
    call #getchar
    pop link
    cmp tmp2,tmp,r5
    beq readlinedone
    stor tmp,r4,0
    move r4,r4,1
    bra getcharloop
readlinedone:
    move tmp,0,0
    stor tmp,r4,0
    pop r5
    return

Sample macros file (lib.S)

#define jump x
	load link,${x}
	jal link,link,0
#end

#define call x
	push link
	jump ${x}
#end

#define return
	jal 0,link,0
#end

calling the assembler

python3 assembler.py [options] files

-l  --labels   print list of labels to stderr
               shows the addresses corresponding to the labels

-u  --usage    show allowed syntax and exit
               a list of all available instructions and their meaning

-d  --debug    dump internal code representation
               addresses and bytecode along with the original source

-i  --ihex     produce output in Intel HEX format

-v  --hex      produce output in Verilog HEX format
               useful for generating firmware to be loaded into brams

Without any files stdin is read.

A typical call would look like

python assembler.py --ihex lib.S myprog.s > myprog.bin

The default output format is binary file of bytes where the first byte represents data at address 0x0000 and the last byte is the last byte of the assembled program. This is straight forward but also very wasteful. If your program starts at address 0x400 for example there will be 1024 leading zero bytes. The intel hex format might be a better choice then.

Clone this wiki locally