Page 1 of 1

Inline assembler advice requested

Posted: Tue Feb 10, 2015 5:57 pm
by pythoncoder
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.

Re: Inline assembler advice requested

Posted: Wed Feb 11, 2015 12:30 am
by Damien
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.

Re: Inline assembler advice requested

Posted: Wed Feb 11, 2015 9:41 am
by pythoncoder
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 ;)