Arm Inline assembly in gcc?

The problem would seem to be that you specify w0 as an INPUT operand, when it actually should be a read-write OUTPUT operand, like sum . In addition, you need to specify that it clobbers v1 and v2 as you use those registers (otherwise, gcc might put some other var into these regs and expect them to be preserved. ).

Up vote 0 down vote favorite share g+ share fb share tw.

I have some trouble with some inline assembly code. I know what should be done but I miss the "how"! I have this checksum function that is "almost" working : static unsigned long cksum_unroll( unsigned short **w, int *mlen) { int len; unsigned short *w0; unsigned long sum=0; len = *mlen; w0 = *w; while( len >= 8) { asm volatile ( "ldmia %w0!

, {v1, v2}\n\t" "adds %sum, %sum, v1\n\t" "adcs %sum, %sum, v2\n\t" "adcs %sum, %sum, #0" : sum "+r" (sum) : w0 "r" (w0) ); len -= 8; } *mlen = len; *w = w0; return (sum); } My problem, I believe, is on the line ": sum "+r" (sum) : w0 "r" (w0)" On the first assembly line, w0 is processed correctly by ldmia (when the line is executed, data are in r4,r5 and w0 is incremented). But the incremented value of w0 is not saved somewhere and when the code loop, the original value of w0 is loaded again (see assembly code below). My guess is that I should store the value of w0 on the line ": sum "+r" (sum) : w0 "r" (w0)" but I don't know how... Here's the disassembly code of the inline assembly part of the function : Note that : len is stored at r11, #-16 w0 is stored at r11, #-20 sum is stored at r11, #-24 Compiled, the following code : asm volatile ( "ldmia %w0!

, {v1, v2}\n\t" "adds %sum, %sum, v1\n\t" "adcs %sum, %sum, v2\n\t" "adcs %sum, %sum, #0" : sum "+r" (sum) : w0 "r" (w0) ); len -= 8; Generate : 00031910: ldr r3, r11, #-20 00031914: ldr r2, r11, #-24 00031918: mov r4, r2 0003191c: ldm r3! , {r4, r5} 00031920: adds r4, r4, r4 00031924: adcs r4, r4, r5 00031928: adcs r4, r4, #0 0003192c: str r4, r11, #-24 00031930: ldr r3, r11, #-16 00031934: sub r3, r3, #8 00031938: str r3, r11, #-16 As you can see, I would like to add something like "str r3, r11, #-20" between lines 31928 and 3192c because when the program loop to the line 31910, r3 is loaded with the initial value of r3... I think this is an easy one for the inline assembly expert of the stack overflow community! By the way, I'm working on a ARM7TDMI processor (but this may not be relevant for this question...) Thanks in advance!

EDIT: To verify my idea, I tested the following : asm volatile ( "ldmia %w0! , {v1, v2}\n\t" "adds %sum, %sum, v1\n\t" "adcs %sum, %sum, v2\n\t" "adcs %sum, %sum, #0\n\t" "str %w0, r11, #-20" : sum "+r" (sum) : w0 "r" (w0) ); And this works. Maybe this is the solution, but what do I use to replace "r11, #20" that will probably change if I modify the function?

C gcc arm inline-assembly link|improve this question edited Feb 15 at 23:12 asked Feb 15 at 22:36Martin Allard434 64% accept rate.

To verify my idea, I tested the following : asm volatile ( "ldmia %w0! , {v1, v2}\n\t" "adds %sum, %sum, v1\n\t" "adcs %sum, %sum, v2\n\t" "adcs %sum, %sum, #0\n\t" "str %w0, r11, #-20" : sum "+r" (sum) : w0 "r" (w0) ); And this works. Maybe this is the solution, but what do I use to replace "r11, #20" that will probably change if I modify the function?

– Martin Allard Feb 15 at 23:05 GCC's inline assembly makes my head hurt (and I've already got one started because of some carpet gluing going on in a nearby remodel), so I can't give you any direct help... But I can point you to one of the best docs I've read about how to deal with GCC's inline asm, in case you haven't come across it yet: ethernut.de/en/documents/arm-inline-asm.... As a bonus, the doc specifically targets ARM. – Michael Burr Feb 15 at 23:42 Thanks for the link. This web pages was already open when I wrote this question!

– Martin Allard Feb 16 at 13:51.

The problem would seem to be that you specify w0 as an INPUT operand, when it actually should be a read-write OUTPUT operand, like sum. In addition, you need to specify that it clobbers v1 and v2 as you use those registers (otherwise, gcc might put some other var into these regs and expect them to be preserved. ) So you should have: asm volatile ( "ldmia %w0!

, {v1, v2}\n\t" "adds %sum, %sum, v1\n\t" "adcs %sum, %sum, v2\n\t" "adcs %sum, %sum, #0" : sum "+r" (sum) , w0 "+r" (w0) : : "v1", "v2" ); that is, two read-write input/output operands, no exclusively input operands, and two register clobbers.

Wow thanks a lot, it seems to work great. Initially (before I start debugging this part of code) the line was :w0 "+r" (w0), sum "+r" (sum) which is, except for the clobbers part, the answer that you gave, but with sum and w0 in the reverse order... I tried your solution without the clobbers(as you said that I "should", not "must" use it ;-) and it behave like in the beginning. With the clobbers part, it works perfectly as it force the compilator to save the state of w0!

– Martin Allard Feb 16 at 15:45 @Martin: Ok, I strengthened the wording a bit. Leaving out the clobbers is a particularly insidious bug, as it might work just fine when you first try it, and then break later when you change some (apparently) unrelated part of the program which perturbs the register allocator. – Chris Dodd Feb 16 at 17:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions