DDE inline assembler in C
André Timmermans (100) 655 posts |
It seems like the DDE C compiler includes an inline assembler but where is the documentation? |
Paolo Fabio Zaino (28) 1882 posts |
Hey André, If you need an example of use that is very easy to read, please have a look at my uCLib sources here: https://github.com/RISC-OS-Community/uCLib/blob/develop/src/c/uCLib_base Also if you fancy, feel free to join me on that project. It’s an alternative “micro C library” to be used with transient executable builds with both DDE and GCC. Hope this helps |
André Timmermans (100) 655 posts |
I was actually looking at inlining fixed point multiplications like “(a*b) >> 23” and stuff like that but all the examples I have seen use fixed registers. In GCC it seems you can tell it to use the register corresponding to a variable. |
James Peacock (318) 129 posts |
IIRC there was some notes on using it in either the changes file or maybe in the c99 docs files. These were in the DDE archive, not the PDFs. |
Chris Mahoney (1684) 2165 posts |
There are some notes in the “C implementation details” chapter of the C/C++ manual, under “Inline assembler” (page 122 in my copy). I’m not sure whether or not you saw that; it’s not hugely detailed but is hopefully enough to get on with. |
Paolo Fabio Zaino (28) 1882 posts |
André, #include <stdio.h> int mul(int x, int y) { int rval; __asm { MUL rval,x,y } return rval; } int main(void) { printf("result: %i", mul(5,7)); return 0; } I used integers and a simple operation just to keep the code short. However the example is “real-world useful”, in the sense that I used this approach to initially implement my Forth compiler for RISC OS and then replaced it with ObjAsm so that the primitives are all writen in optimised ARM ASM. So, each dictionary word become just an address (typical forth hybrid between interpreter and compiler). Hope this helps P.S. DDE’s inline ASM is not as complete as GCC’s one and also seems to be kinda “controlled” by the compiler anyway. It’s definitely nothing like the BBC BASIC assembler (just to avoid confusions). |
Chris Mahoney (1684) 2165 posts |
The manual says to avoid using registers whenever possible (“For best practice, BL and SWI are the only reason you should use physical registers”). Can those three assembly instructions be consolidated down to “MUL rval,x,y” or doesn’t it work like that? I’m not an assembly guru :) |
Paolo Fabio Zaino (28) 1882 posts |
@ Chris
Indeed they can be converted to a single line `MUL rval,x,y`, but it’s just “syntax sugar”, it will be (obviously) converted into the MOVs and MUL. Anyway updated the code on my previous post if that’s what André is seeking for. |
André Timmermans (100) 655 posts |
Thanks Paolo, much simplier than the GCC stuff. I have now converted all the stuff. I just have a problem with the following macro: #define arm_mul(x, y, SCALEBITS) \ which gives the cryptic error “operand of # not macro formal parameter”. |
Rick Murray (539) 13840 posts |
Have you tried it without the #? It’s a compiler that understands some assembler, it isn’t an assembler, so the syntax will be slightly different. |
Simon Willcocks (1499) 513 posts |
cpp will try to make #SCALEBITS into a string. Maybe try a space after the # Or compile with -S, and look at the .s file. |
James Peacock (318) 129 posts |
Did it compile outside the macro? If so could try using an inline function rather than a macro. |
André Timmermans (100) 655 posts |
I forgot the # had some effect inside macros. |
Jean-Michel BRUCK (3009) 359 posts |
Bonjour André, I don’t know if this keeps up with the latest updates, but the syntax remains the same. |
Chris Mahoney (1684) 2165 posts |
That’s almost word-for-word identical to the text in the C/C++ manual. |
Paolo Fabio Zaino (28) 1882 posts |
Good choice, you can always ifdef the function if you need replacements and define it as always inline if you want to reduce function call overhead. |