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?
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