Android ARM inline assembly

If you happen to want to write some inline assembly in Android, its just about as easy as you’d expect! See the code snippet below for a simple example of how to add two numbers in assembly on ARM.

Download code with Android makefile

[code lang=”c”]
include "stdio.h"

static inline int add(int a, int b) {
int sum;
__asm__ __volatile ("add %[c][/c], %[a], %[b]"
: [c][/c] "=r" (sum) /* one output */
: [a] "r" (a), [b] "r" (b) /* two inputs */
: /*code does not clobber*/ );
return sum;
}

int main(int argc, char ** argv) {
printf("sum is %in", add(4,222));
return 0;
}
[/code]
If you run this on your android device (say, a pandaboard), you’ll see:
[code lang=”bash”]sum is 226[/code]

Now, this is just a simple example, I’m sure you will want to do something more complex. You should read up and make sure you know what you’re doing before you start tinkering with assembly, or you might be left with head scratching and your code broken… Some recommended reading

Happy hacking!

This entry was posted in Coding, Hardware, Random. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *