Page 2 of 2

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Posted: Wed Mar 02, 2016 12:54 pm
by ropod7
pythoncoder,
I will try it soon! Thank you.

I have an other solution:
[youtube]thBrAjPJL4c[/youtube]
https://youtu.be/thBrAjPJL4c

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Posted: Wed Mar 02, 2016 5:19 pm
by SpotlightKid
@ropod7: pythoncoder's byteswap function is buggy. Roberthh's reverse function works.

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Posted: Wed Mar 02, 2016 6:47 pm
by ropod7
[youtube]m7FUieXfCRw[/youtube]

Roberthh's reverse function works!

Little update.

https://www.youtube.com/watch?v=m7FUieXfCRw

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Posted: Thu Mar 03, 2016 1:32 pm
by pythoncoder
Roberthh wrote:Hi @pythoncoder, besides the fact, that I should have called my function byteswap, like you did, it seems to me that your code is wrong. You increment the data pointers by two in each loop, but divide the buffer length value by 4, which means that only half of the buffer is processed.
Robert (alias Statler)
Oh dear, you've got me bang to rights ;) This fixes it

Code: Select all

@micropython.asm_thumb
def byteswap(r0, r1):               # bytearray, len(bytearray)
    mov(r3, 1)
    lsr(r1, r3) # divide len by 2
    mov(r4, r0)
    add(r4, 1) # dest address
    label(LOOP)
    ldrb(r5, [r0, 0])
    ldrb(r6, [r4, 0])
    strb(r6, [r0, 0])
    strb(r5, [r4, 0])
    add(r0, 2)
    add(r4, 2)
    sub(r1, 1)
    bpl(LOOP)

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Posted: Thu Mar 03, 2016 2:23 pm
by Roberthh
Almost, sorry. Please check the loop iterations vs. len value, e.g. for len = 0 or 2

Change endian-ness of a bytearray of half words

Posted: Fri Mar 04, 2016 6:27 am
by pythoncoder
Yes, it did one iteration too many. Here is a version which validates input, returning 1 on success or 0 on failure (len() of 0 or 1).

I prefer to validate in Python before calling assembler routines. It's easier to read and maintain, simplifies the asm, eases doing something meaningful if validation fails and saves the overhead of calling a function which will do nothing.

Code: Select all

@micropython.asm_thumb
def byteswap(r0, r1):               # bytearray, len(bytearray)
    cmp(r1, 1)
    ble(FAIL)
    mov(r3, 1)
    lsr(r1, r3) # divide len by 2
    mov(r4, r0)
    add(r4, 1) # dest address
    label(LOOP)
    ldrb(r5, [r0, 0])
    ldrb(r6, [r4, 0])
    strb(r6, [r0, 0])
    strb(r5, [r4, 0])
    add(r0, 2)
    add(r4, 2)
    sub(r1, 1)
    bne(LOOP)
    mov(r0, 1)
    b(END)
    label(FAIL)
    mov(r0, 0)
    label(END)

Change endian-ness of a bytearray of half words

Posted: Fri Mar 04, 2016 7:46 am
by Roberthh
Hello @pythoncoder, this is a good example how nasty simple seeming functions can be. My variant, simpler looking, has a fault too. it counts right, and does not execute at a value of 0, BUT fails badly if the counter is odd, by trying to access a byte beyond the buffer limit. Your variant, due to the division, would simply ignore the last byte. Robert

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Posted: Tue Mar 08, 2016 12:55 am
by SpotlightKid
So how about this?

Code: Select all

@micropython.asm_thumb
def byteswap(r0, r1):
    label(loopstart)
    cmp(r1, 1)
    ble(loopend)
    ldrb(r2, [r0, 0])
    ldrb(r3, [r0, 1])
    strb(r3, [r0, 0])
    strb(r2, [r0, 1])
    add(r0, 2)
    sub(r1, 2)
    b(loopstart)
    label(loopend)

def byteswapped(b):
    res = bytearray(b)
    byteswap(res, len(res))
    return res

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Posted: Thu Mar 25, 2021 6:52 am
by ichtar
I know it is a really old post but I was struggling with that too and found that

>>> str="abc"
>>> print(''.join(reversed(str)))
cba


works pretty well for me

Re: Revers strings in micropython (NotImplementedError with slices as [::-1])

Posted: Fri Jan 07, 2022 10:53 pm
by leosok
+1 to @ichtar. Your solution works. :idea: