User Tools

Site Tools


en:pfw:assemblers_rp2040-assembler

Assembler for RP2040

The idea

A compact Forth-style macro assembler with an efficient literal pool mechanism for the RP2040.
A Forth-style assembler uses all the capabilities of the Forth interpreter and assembles opcodes in between.
An example:

code SQUARED    ( +n -- +n^2 )
    tos tos muls,  next,
end-code

Implementation

The example code asumes a 32-bit machine that has 16-bit & 32-bit compile actions. It is also asumed that the code runs in RAM as noForth T on the RP2040 does. Because the Thumb-2 opcodes of the ARMv6-M use a RISC like architecture, macros are added to make assembly a bit more easy. An implementation example for CODE and END-CODE is showed too. Missing words in Generic Forth are:

(*          ( "ccc" -- )        - Skip all text until *) is found
+TO         ( n "name" -- )     - Add n to the contents of the value "name"
VOCABULARY  ( "name" -- )       - Define a named wordlist

All Thumb2 instructions

✦ Two low register opcodes
    ands,       eors,       adcs,       sbcs,       rors,       tst,
    neg,        cmn,        orrs,       muls,       bics,       mvns,
    sxth,       sxtb,       uxth,       uxtb,       rev,        rev16,
    revsh,      lsls,       lsrs,       asrs,       movs,       cmp,
 
✦  Two all register opcodes
    add,        mov,        cmp,
 
✦ Three low register opcodes
    adds,       subs,       str,        strb,       strh,       ldrb,
    ldrh,       ldr,        ldrsb,      ldrsh,
 
✦ Two low register & 3-bits immediate
    adds,       subs,       rsbs,
 
✦ Two low register & 5-bits immediate
    lsls,       lsrs,       asrs,       str,        strb,       strh,   
    ldr,        ldrb,       ldrh,
 
✦ One low register & 7-bits immediate (RP-specific)
    add,        subs,       sub,
 
✦ One low register & 8-bits immediate
    movs,       cmp,        str, (rp)   ldr, (rp)   ldr,        add, (rp)
    push,       pop,        bkpt,       udf,        svc,        adr,
    stm,        ldm,        adds,       subs,
 
✦ Various 16-bit opcodes
    noop,       nop,        yield,      wfe,        wfi,        sev,
    cpsie,      cpsid,      bx,         blx,
 
✦ Fourteen jump instructions
    =?          cs?         neg?        vs?         u>?         <?
    >?          no
 
✦ Control structures
    if,         else,       then,       ahead,
    begin,      while,      repeat,     again,      until,
 
✦ Literal pool additions
    ALIGN,      CODE>       POOL,       APOOL,      ##
 
✦ Assembler macros
    ALIGN,      ##          POOL,       APOOL,      )+
    -)          NEXT,

Two usage examples

code LSHIFT ( x1 +n -- x2 )
    day tos movs,       \ 1 - +n to DAY
    tos  sp )+ ldr,     \ 2 - pop X1 to TOS
    tos day lsls,       \ 1 - Shift TOS DAY positions left
    next,               \ 6 
end-code

code KEY?)  ( -- f )
    40034018 ,          \ UART0 UARTFR 
code>
    sun  w ) ldr,       \ Read UARTFR to SUN
    tos  sp -) str,     \ Push stack
    tos  sun ) ldr,     \ Read flags
    tos 1B # lsls,      \ Build flag out of bit-4
    tos 1F # asrs,      \ Extend sign
    tos tos mvns,       \ Invert flag
    next,
end-code

File name Purpose in Dropbox (external link)
Thumb assembler doc v2.pdf Documentation Assembler documentation
RP2040-asm uni.f RP2040 assembler noForth RP2040 assembler
RP2040-asm-extensions uni.f RP2040 ass. extensions noForth assembler ext.f

Contributions

Alternative Implementations

Back to PFW page

en/pfw/assemblers_rp2040-assembler.txt · Last modified: 2023-10-02 17:04 by willem