title: arm Inline assembly in gcc
body: <p>I have some trouble with some inline assembly code. I know what should be done but I miss the "how" !</p>
  
  <p>I have this checksum function that is "almost" working :</p>
  
  <pre><code>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 &gt;= 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);
  }
  </code></pre>
  
  <p>My problem, I believe, is on the line "<strong>: [sum] "+r" (sum) : [w0] "r" (w0)</strong>"
  On the first assembly line, <strong>w0</strong> is processed correctly by <strong>ldmia</strong> (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...</p>
  
  <p>Here's the disassembly code of the inline assembly part of the function :</p>
  
  <p>Note that :</p>
  
  <pre><code>len is stored at r11, #-16
  w0 is stored at r11, #-20
  sum is stored at r11, #-24
  </code></pre>
  
  <p>Compiled, the following code :</p>
  
  <pre><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;
  </code></pre>
  
  <p>Generate :</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>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... </p>
  
  <p>I think this is an easy one for the inline assembly expert of the stack overflow community!</p>
  
  <p>By the way, I'm working on a ARM7TDMI processor (but this may not be relevant for this question...)</p>
  
  <p>Thanks in advance!</p>
  
  <p>EDIT:</p>
  
  <p>To verify my idea, I tested the following : </p>
  
  <pre><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\n\t" 
  "str %[w0], [r11, #-20]" 
  : [sum] "+r" (sum) : [w0] "r" (w0) 
  ); 
  </code></pre>
  
  <p>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?</p>