npasm  an assembler for npu   20 mar 2012

structure of source file 

<code>
.end
<data>
.end

<code> 

op arg* nl    a line of instruction
:label  nl    

arg is 
  num, @num, @label

; comment until end of line


<data>
@ads      set starting address of data
num*      data
.end

--------------

Example

; an example of npu assembly

:loop
  ld 0 @512
  ld 1 @513
  lds
  buf
  ldr 2 
  jmp @loop

.end
  
@512    ; data start at 512
1
2
.end

---------------

algorithm

single pass and keep patch list for back patching
in the end of pass.

code

while read token not .end
  if op read token arg* until nl ip++
  if label dolabel

arg*
  if num use value
  if @ doaddress

dolabel
  search and insert symbol table
  if defined then error!
  instantiate value to that symbol

doaddress
  if num use value
  then it is a symbol
  search and insert symbol table
  if defined then use value
  if undef  add symbol to patch list

data
  
read token until .end
  if @  set ip
  else it is num use value

-----------------

pseudo code of the assembler

data structure:  symbol table, patch list, code, data

symbol table: key,value
patch list: ads,key
code: op,ads,r1,r2       array of code output
data: data               array of data (absolute)

main
  docode
  dodata

docode
  tk = tokenise()
  while( tk != tkend )
    if( tk == tkop )
      getarg()
      ip++
    else if( tk == tklabel )
      dolabel()
    else
      error!
    tk = tokenise()

getarg
  code.op[ip] = tkvalue    
  tk = tokenise()


------------------

preprocessing

from source (asm) to strict

strict form:

linenum op arg arg arg <nl>
linenum :label <nl>
linenum .end <nl>
linenum @num <nl>
linenum num <nl>
linenum .end <nl>
<eof>

arg is {num, @num, @label}

algorithm

repeat until .end
  skipblank  (find the first char)
  if :lable  out with nl
  if .end    out with nl
  else is op do to eol
    while not nl  
      read  out
    out nl

---------------
  
        
