Macil / DCPU-16-Assembler

Assembler for Notch's DCPU-16 architecture.
GNU General Public License v3.0
7 stars 0 forks source link

Wrong code generation for [0 + register] #1

Open fhars opened 12 years ago

fhars commented 12 years ago

[0 + register] is assembled as the non-equivalent [register], which completey breaks code like

; copy C words starting at A from  B
;
; If the memory areas overlap, the routine will only work
; if the target address is lower than the source address
;
; input A: target address
; input B: source address
; input C: number of words to copy
:memcpy
        SET PUSH, X
        SET X, SP
        SET SP, A
        ADD A, C

        AND C, 0x0f
        XOR C, 0x0f
        ADD C, 1
        SUB B, C
        SHL C, 1
        ADD PC, C
:_pc_memcpy_loop
        SET POP, [0 + B]
        SET POP, [1 + B]
        SET POP, [2 + B]
        SET POP, [3 + B]
        SET POP, [4 + B]
        SET POP, [5 + B]
        SET POP, [6 + B]
        SET POP, [7 + B]
        SET POP, [8 + B]
        SET POP, [9 + B]
        SET POP, [10 + B]
        SET POP, [11 + B]
        SET POP, [12 + B]
        SET POP, [13 + B]
        SET POP, [14 + B]
        SET POP, [15 + B]

        ADD B, 16
        IFN SP, A
        SET PC, _pc_memcpy_loop

        SET SP, X
        SET X, POP
        SET PC, POP
Macil commented 12 years ago

Making sure I have this correct: this code relies on all of the SET POP, [_ + B] instructions being two words long, right?

I've been planning on adding a symbol that forces a parameter to use the next word if possible, mainly for code that requires the instruction to be that long or for self re-writing code. I'd rather that code that wasn't explicitly marked as such was always written in the shortest form possible (When I eventually add support for macros, then it makes sense that someone might do something like SET X, [somemacro-1+B] where the macro might end up being 1 at compile time for example, and I'd like to compile that down to [B] in that case, etc.). The line in this program would look something like this then: SET POP, @[0 + B].