← index #1142Issue #15248
Related · high · value 0.486
QUERY · ISSUE

Inline assembler data statement: suggested enhancement

openby peterhinchopened 2015-03-01updated 2024-09-29
enhancement

The data statement enables data to be included with code. Unfortunately it doesn't seem possible to access it without explicitly manipulating the program counter. Attempts to assign labels to registers produce an error. For example it would be good to be able to write something like this (to access an element from a data array):

d1 = const(12)
d2 = const(13)

@micropython.asm_thumb
def bar(r0):
    b(START)

    label(MYDATA)
    data(4, d1, d2)

    label(START)
    movwt(r1, MYDATA)
    add(r0, r0, r0) # Convert index to byte offset
    add(r0, r0, r0)
    add(r1, r1, r0) # add offset to data address
    ldr(r0, [r1, 0])

But this fails with "unsupported Thumb instruction 'movwt' with 2 arguments". The code can be made to work as follows, but it seems inelegant compared to using a label.

d0 = const(1234567)
d1 = const(987654)
d2 = const(3*d1)

@micropython.asm_thumb
def getdata(r0):
    push({pc})
    b(START)
    data(4, d0, d1, d2)
    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
CANDIDATE · ISSUE

asm: inline assembler: directive "data" for all the micros

closedby massimosalaopened 2024-06-10updated 2024-06-11
enhancement

Description

Form the thumb2 micros, in the inline assembler it is available the directive data
https://docs.micropython.org/en/latest/reference/asm_thumb2_directives.html

A clever usage of data is to make assembler instructions available to python.
Examples:

@micropython.asm_thumb
def rbit(r0):
    data(2, 0xfa90, 0xf0a0)

@micropython.asm_thumb
def clz(r0):
    data(2, 0xfab0, 0xf080)

Note: these are only examples, I am NOT talking about to implement bit reversal and counting leading zeroes.

I think it is nice to have the data directive available for all the supported architectures: armv6m, xtensa, xtensawin, riscv
also where inline assembler isn't (still) implemented.

Code Size

I don't know

Implementation

I would like to sponsor development of this feature

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied