~luxferre/nrj-oisc

24eaaab70de07ebb79650892c11534563ff350f9 — Luxferre 5 months ago d712730
Optimized lookup table placement and trailing zero words stripping
2 files changed, 15 insertions(+), 6 deletions(-)

M example.nrjasm
M nrjasm.py
M example.nrjasm => example.nrjasm +5 -5
@@ 12,9 12,9 @@

.inc stdlib.nrjasm

.var x 12EF ; .var defines a label for a particular memory location
.var y 12F0 ; define another variable at 0x12F0
.set @x 33EE ; .set sets a memory location to a particular hex constant at the build time, @ dereferences a label into the address
.var x 2EF ; .var defines a label for a particular memory location
.var y 2F0 ; define another variable at 0x2F0
.set @x 'm ; .set sets a memory location to a particular hex constant at the build time, @ dereferences a label into the address
.set @y 'M ; ' dereferences a character into a whole word with its ASCII code

; we CANNOT use dereferencing operators with .var, only with .set or directly


@@ 28,13 28,13 @@

; for the lookup table and CUR/NXT macros to work correctly, the code must start at an address divisible by 3
; note that FREE doesn't intelligently detect the available cells, it only takes the next one after the maximum address used
; so in our case, the first FREE instance will be substituted with 12F1, the next with 12F2 and so on

; so in our case, the first FREE instance will be substituted with 2F1, the next with 2F2 and so on

; now, lets output a character by transferring the y value to the output cell 1
; in an endless loop

.lbl myloop
MOV 1 @x 
MOV 1 @y @myloop ; output the character and jump to the beginning
; we don't have to explicitly zero out the cell 1 as it is done by the I/O logic


M nrjasm.py => nrjasm.py +10 -1
@@ 5,6 5,7 @@
import sys
import array
from os.path import realpath
from numpy import trim_zeros

# some constants to redefine more easily in case of major breaking changes



@@ 154,7 155,11 @@ def start_assembly(srcfname, dstfname): # main assembly method
    # the NXT macro will be replaced with a cell in the lookup table that points to the next instruction
    # and the lookup table will also take some memory in the machine

    ltoffset = memsize >> 1 # in the worst case scenario, the code will take half of all memory and lookup table will take the other half
    # now, try to detect the optimal offset for our lookup table
    if maxvar > 0: # we have some variables defined, so place the lookup table after them
        ltoffset = maxvar + 1
    else: # in the worst case scenario, the code will take half of all memory and lookup table will take the other half
        ltoffset = memsize >> 1 
    targetmem[ltoffset] = haltaddr # the first lookup table entry is always the halting address
    codepos = 0
    ltpos = 1 


@@ 199,6 204,10 @@ def start_assembly(srcfname, dstfname): # main assembly method
            for v in line:
                codepos += 1

    # looking for a more optimal solution than to import numpy just for this:

    targetmem = trim_zeros(targetmem, 'b') # only strip trailing zero values

    # now, we have assembled our target memory snapshot, let's write the output file

    outf = open(dstfname, "wb")