Inline assembler advice requested

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 advice requested

Post by pythoncoder » Tue Feb 10, 2015 5:57 pm

This follows on from the issue I raised on Github.
https://github.com/micropython/micropython/issues/1111

It seems that registers above r7 aren't safe to use so the question is how to proceed when you run out of registers. One solution is to push registers onto the stack; alas the STFMFD and LDFMFD instructions aren't implemented. One suggestion is that you can implement arbitrary instructions using the data command thus data(bytesize, data, ...); e.g.

Code: Select all

 data(2, 0x1234, 0x4567) 
While this is very useful to know, it isn't the most readable solution. Is there any reason not to use the stack as follows? r13 is the stack pointer, it grows downwards and you pre-decrement it for a push and post increment for a pop (at least from my reading of the well written tutorial on http://www.davespace.co.uk/arm/introduc ... stack.html) and assuming MicroPython uses the Full Descending stack - the tutorial suggests anything else is "rare". Thus

Code: Select all

sub(r13, 4)
str(r0, [r13, 0])
...
ldr(r0, [r13, 0])
add(r13, 4)
As far as I can see this should be safe even if the code is interrupted, but I'm a total noob regarding low level ARM stuff so any comments would be appreciated.
Peter Hinch
Index to my micropython libraries.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Inline assembler advice requested

Post by Damien » Wed Feb 11, 2015 12:30 am

You could try using the 32-bit push and pop instructions:

Code: Select all

data(2, 0xe92d, 0x0f00) # push r8,r9,r10,r11
data(2, 0xe8bd, 0x0f00) # pop r8,r9,r10,r11
The 2 means the following numbers are 2-bytes in size each. The 0x0f00 specifies the registers to push/pop, as a bitmask. You need to push/pop an even number of registers.

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

Re: Inline assembler advice requested

Post by pythoncoder » Wed Feb 11, 2015 9:41 am

Thanks, Damien. Doubtless a safer fix than pigging about with r13. I've now found the doc that explains the coding and have coded some other unimplemented instructions, including SDIV and UDIV. For anyone following this, search for "armv7-m architecture reference manual".

Machine code. Takes me back to the 70's ;)
Peter Hinch
Index to my micropython libraries.

Post Reply