Inline assembler: using the data statement

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Inline assembler: using the data statement

Post by pythoncoder » Tue Feb 24, 2015 7:14 am

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?
Peter Hinch
Index to my micropython libraries.

Post Reply