We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I was reading The Definitive Guide to ARMCortexM0/M0+ by Joseph Yiu
In Chapter 21, under section 21.4.2 GNU Compiler Collection, there is a example function with inline assembly operation:
int my_mul_10(int DataIn) {
int DataOut; __asm(" movs r0, %0\n"
" movs r3, #10\n" " mul r0, r3\n" " movs %1, r0\n" :"=r" (DataOut) : "r" (DataIn) : "cc", "r0", "r3");
return DataOut; }
In the code example, %0 is the first input parameter and %1 is the first output parameter. Since the operand order is output _operands, input_operands, and clobbered_operands, “DataOut” is assigned to %0, and “DataIn” is assigned to %1. Since the code changes register R3, it needs to be added to the clobbered operand list.
Shouldn't "DataOut" is assigned to %1 and "DataIn" to %0?
Or am I missing something here?
Actually the first sentence is the one that need fixing. The first operand (here the output operand DataOut) goes in %0, second operand (here input operand DataIn) in %1, etc.Best regards,Thomas
I am bit confused now. From the code, what I understand is that %0 is the input (DataIn) and %1 is the return value (DataOut).
Sorry for the error in the text, and a bit unclear in the descriptions.
In the code:
1st parameter (%0) : "=r" (DataOut)
2nd parameter (%1): "r" (DataIn)
3rd to 5th parameters: "cc", "r0", "r3"
Thomas's is correct - the first sentence is wrong, and might be clearer to write it as:
"In the code example, %0 is the output parameter and %1 is the input parameter."
Please note that this is an old style inline assembler format. Recent gcc support symbolic names - instead of using %0, %1..., you can use %[ _name_ ] which is much easier to read. Unfortunately I wasn't aware of this when I wrote the book.
regards,
Joseph
Ah yes indeed, it should be %1 at the top and %0 later. Sadly typos happen.
Thanks a lot Thomas.
Thanks a lot, Joseph. Learnt a lot from your book :)
Thanks for your feedback :-)