Calling built-in functions from Assembler

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
workless
Posts: 13
Joined: Sat Mar 24, 2018 7:03 pm
Location: Vancouver, BC

Calling built-in functions from Assembler

Post by workless » Mon Mar 26, 2018 10:28 pm

Is there a way to call built-in functions such as those in the math module from a user-defined function written in assembler?

I started writing some functions (https://github.com/billtubbs/array_funcs) to vectorize basic algebra operations for Micropython arrays.

Example:

Code: Select all

@micropython.asm_thumb
def float_array_square(r0, r1):
    label(LOOP)
    vldr(s0, [r0, 0])
    vmul(s0, s0, s0)
    vstr(s0, [r0, 0])
    add(r0, 4)
    sub(r1, 1)
    bgt(LOOP)
But how would I implement other math operations such as pow, sin, cos, tan, exp, log, ...etc?

In other words how do I call the underlying routines in math.pow from assembler to make a vectorized version of the following:

Code: Select all

def float_array_power(x, n, y):
    for i in range(n):
        x[i] = math.pow(x[i], y)

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

Re: Calling built-in functions from Assembler

Post by pythoncoder » Tue Mar 27, 2018 6:37 am

Doing that would be quite tricky. The bigger question is whether it makes sense to do so. The reason for using assembler for maths is to achieve high speed. Calling maths functions written in C rather begs a question. Why not implement the whole module in C?

When I've needed fast maths in assembler I've used algorithms which trade accuracy for performance but I guess this wouldn't be appropriate for your application.

Figuring out how to call the existing maths functions would require some research. I'd start by passing an array of function pointers to the assembler function (array type 'P'). But you'd also need to work out how to pass the arguments and retrieve the result. It would be an interesting challenge, but I'm unconvinced that it makes a lot of sense.
Peter Hinch
Index to my micropython libraries.

User avatar
workless
Posts: 13
Joined: Sat Mar 24, 2018 7:03 pm
Location: Vancouver, BC

Re: Calling built-in functions from Assembler

Post by workless » Tue Mar 27, 2018 5:31 pm

pythoncoder wrote:
Tue Mar 27, 2018 6:37 am
Calling maths functions written in C rather begs a question. Why not implement the whole module in C?
Thanks. I didn't realize that the math module was written in C. I guess when I heard that Micropython 'runs on the bare metal' I thought it meant it was implemented directly in assembly!

So there is a c-compiler included in the Micropython OS? Or do I have to rebuild Micropython itself with my module included? Maybe this project is a bit beyond my skill level...

Now I see some documentation on how to do write modules in c:
http://micropython-dev-docs.readthedocs ... odule.html

Any other good guides/tutorials/references if I were to attempt this?

User avatar
workless
Posts: 13
Joined: Sat Mar 24, 2018 7:03 pm
Location: Vancouver, BC

Re: Calling built-in functions from Assembler

Post by workless » Tue Mar 27, 2018 5:35 pm

workless wrote:
Tue Mar 27, 2018 5:31 pm
pythoncoder wrote:
Tue Mar 27, 2018 6:37 am
Calling maths functions written in C rather begs a question. Why not implement the whole module in C?
So there is a c-compiler included in the Micropython OS? Or do I have to rebuild Micropython itself with my module included?
I think this answers my question:
You might want to use one of the existing libraries for your platform, get access to some peripherals or other features of the hardware that is not exposed to Python by default, or even just do something faster and with smaller memory overhead. To do that, you will need to extend the existing firmware with your own C code...

Post Reply