Assembler syntax

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:

Assembler syntax

Post by pythoncoder » Sat Aug 30, 2014 6:42 am

Is there a guide to this? The various resources I've found on the web don't seem to match the requirements of the Micropython environment. I've written some code to do some simple bit twiddling of 32 bit words and have hit the following issues. The bic instruction, according to the various guides I've encountered, takes three arguments. Yet the only way I can get it to work is with two, e.g. bic(r0, r2).
I've yet to find any way to execute the bitwise and instruction. A simple and seems to confuse the Python interpreter, so I thought I'd try andal - yet I've not found a way to get the conditional execution suffixes to work.
I'm new to ARM and evidently there are several variants - perhaps someone could point me to a suitable resource?

Regards, Pete
Peter Hinch
Index to my micropython libraries.

PinkInk
Posts: 65
Joined: Tue Mar 11, 2014 3:42 pm

Re: Assembler syntax

Post by PinkInk » Sat Aug 30, 2014 3:52 pm

and conflicts with python, Damien updated it to _and

I put the sum of the knowledge I gained playing with the inline assembler up on the wiki, hoping someone else would contribute to the bits I couldn't fathom.

There are some bits that are currently a little painful, particularly surrounding interacting with mcu bit fields.

Got the general impression that the developers who might be able to fix this sort of thing aren't likely to put a lot of effort into it until the rest of us start using the facility a little more, other things are more important if this functionality isn't generating a lot of noise ;o). Expect I'd do the same thing under the circumstances.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Assembler syntax

Post by pythoncoder » Sun Aug 31, 2014 6:44 am

Thank you - I'd missed that essential information in the wiki.

Regards, Pete
Peter Hinch
Index to my micropython libraries.

PinkInk
Posts: 65
Joined: Tue Mar 11, 2014 3:42 pm

Re: Assembler syntax

Post by PinkInk » Mon Sep 01, 2014 3:55 am

The source code emitinlinethumb.c (or similar, I linked to it in the wiki) is the most useful documentation at the moment, it's easy to work out what instructions are implemented (but for what they do you largely have to look elsewhere).

Implemented appears just to be a subset of instructions, including very few of (what I felt to be, with very limited assembler knowledge) the more useful 'three register' (rather than statics and register) variants of many instructions.

Not sure why this is, maybe because the intent is for syntax that works across MCU classes, or at this stage it's just a shell for later enhancement.

Also to my understanding someone just implemented another (ARM) inline assembler which outputs a different code set that the MCU in the pyboard understands, haven't had a chance to play with it yet - things appear to be moving very quickly and there's lots of new stuff to grok.

Darn summer holidays ;)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Assembler syntax

Post by pythoncoder » Mon Sep 01, 2014 6:41 am

I think the developers have provided a decent subset and, as you suggest, their efforts are rightly focussed on the Python implementation at present. In my view it's a usable set and the ability to code inline assembler in Python is amazing. I've taken the liberty of editing your Wiki entry to fix a few typos and to add some detail to your information on the various condtion flags.

The one thing that has me foxed is that it seems impossible to pass numbers larger than 0x3fffffff into the assembler. While you can code round this I'd be interested to know the reason. For example consider the trivial function

Code: Select all

@micropython.asm_thumb
def zz(r0):
    and_(r0,r0)
When passed numbers upto and including 0x3fffffff it returns the correct (same) result. But then you get

Code: Select all

>>> hex(test.zz(0x123456))
'0x123456'
>>> hex(test.zz(0x3fffffff))
'0x3fffffff'
>>> hex(test.zz(0x40000000))
'0x20003ab0'
>>> hex(test.zz(0x7fffffff))
'0x20003760'
>>> hex(test.zz(0xffffffff))
'0x20003970'
I notice in the source that movwt clears down the MSB (for reasons I don't understand) but this behaviour, extending to the topmost two bits, is puzzling.

Another issue to anyone coming to this from certain other assemblers is that the logical operations change the condition flags. Not a problem, but something worth noting.

Regards, Pete
Peter Hinch
Index to my micropython libraries.

fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: Assembler syntax

Post by fma » Mon Sep 01, 2014 6:53 am

IS it not related to the fact that micropython implements integers only using 31bits, using the MSB for pointers?
Frédéric

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Assembler syntax

Post by pythoncoder » Mon Sep 01, 2014 4:51 pm

That would explain the results I'm seeing if the problem were restricted to values of 0x80000000 and above (viewed as unsigned integers). However it's the topmost two bits which are at issue, as demonstrated by my result

Code: Select all

>>> hex(test.zz(0x40000000))
'0x20003ab0'
I've worked around this by performing left shifts in assembler: the code itself works as expected. So the problem appears to arise in passing the argument from Python to assembler. I'm unsure whether to report this as a bug, or whether I've missed something in the docs.

Regards, Pete
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Assembler syntax

Post by dhylands » Mon Sep 01, 2014 6:25 pm

Python only knows about signed values. So the small values are stored as 31 bit 2's complement numbers. Which means that as a 32-bit value that the top 2 bits have to be equal.

PinkInk
Posts: 65
Joined: Tue Mar 11, 2014 3:42 pm

Re: Assembler syntax

Post by PinkInk » Tue Sep 02, 2014 1:43 am

Damien did indicate that the 32nd bit issue 'was fixable' in relation to a bug I raised which touched on the subject.

But I closed that bug (main subject of it wasn't a bug, but my miscomprehension) and didn't raise a specific one for this issue.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Assembler syntax

Post by pythoncoder » Tue Sep 02, 2014 8:29 am

Thanks, Dave, all is now clear.

Regards, Pete
Peter Hinch
Index to my micropython libraries.

Post Reply