Page 1 of 1

Inline assembler: using the data statement

Posted: Tue Feb 24, 2015 7:14 am
by pythoncoder
I've yet to find an elegant way to access data created using the data statement and wondered if anyone knew one. The problem is that you can't assign a label to a register: mov(r1, mylabel) produces an error. The only way I've found is to horse around with the pc thus:

Code: Select all

@micropython.asm_thumb
def getdata(r0):
    push({pc})
    b(START)
    data(4, 0x1234567, 0x987654, 0x333333) # 32 bit data
    label(START)
    pop({r2})
    add(r2, 2)          # skip the b() instruction
    add(r0, r0, r0)
    add(r0, r0, r0)     # Convert array offset to bytes
    add(r2, r2, r0)     # Add to adjusted address
    ldr(r0, [r2, 0])    # get the requested data

print(hex(getdata(1)))  # Returns data[1]: 0x987654
Oddly replacing push({pc}) with mov(r2, pc) (and removing pop) causes the example to output 0x98 which has me baffled. Any thoughts?