title: avr-gcc woes: ignores permanent variable/register binding. Why?
body: <p>Still struggling with AVR assembly. This time <code>avr-gcc</code> seems to completely ignore my directive to <a href="http://www.atmel.com/webdoc/AVRLibcReferenceManual/FAQ_1faq_regbind.html" rel="nofollow noreferrer">permanently bind a local variable to a register</a>. Here's an example — this is of course just an illustration, not the final code:</p>
  
  <pre><code>// C code:
  ISR(USART1_RX_vect)
  {
      register uint8_t c asm("r3") = UDR1;
      tty1::buffer[tty1::ptr.head] = c;
  }
  
  // Generated assembly:
  000000d8 &lt;__vector_20&gt;:
    d8:   1f 92           push    r1
    da:   0f 92           push    r0
    dc:   0f b6           in      r0, SREG        ; 63
    de:   0f 92           push    r0
    e0:   11 24           eor     r1, r1
    e2:   8f 93           push    r24
    e4:   ef 93           push    r30
    e6:   ff 93           push    r31
    e8:   80 91 73 00     lds     r24, UDR1       ; 0x800073 &lt;__EEPROM_REGION_LENGTH__+0x7f0073&gt;
    ec:   e0 91 01 01     lds     r30, 0x0101     ; 0x800101 &lt;tty&lt;drv::uart1&gt;::ptr&gt;
    f0:   e2 95           swap    r30
    f2:   ef 70           andi    r30, 0x0F       ; 15
    f4:   f0 e0           ldi     r31, 0x00       ; 0
    f6:   ee 5f           subi    r30, 0xFE       ; 254
    f8:   fe 4f           sbci    r31, 0xFE       ; 254
    fa:   80 83           st      Z, r24
    fc:   ff 91           pop     r31
    fe:   ef 91           pop     r30
   100:   8f 91           pop     r24
   102:   0f 90           pop     r0
   104:   0f be           out     SREG, r0        ; 63
   106:   0f 90           pop     r0
   108:   1f 90           pop     r1
   10a:   18 95           reti
  </code></pre>
  
  <p>- Give me <code>r3</code>, please.</p>
  
  <p>- Sure thing! here's <code>r24</code>. </p>
  
  <p>I can ask any register between <code>r2</code> and <code>r7</code> that compiler just takes whatever it wants! And it has nothing to do with <code>UDR1</code>, it does that with whatever I assign <code>c</code>. What's the point of that directive if it does nothing at all? How am I supposed to control the register the compiler selects?</p>
  
  <p>To the question «<em>Why the heck am I wanting to assign variable to a register?</em>» I reply «<em>Because the generated code is sub-optimal for an interrupt and I want a fine control over the generated assembly.</em>» So far it's been a trouble for me.</p>
  
  <p>Still using <code>avr-gcc</code> version 7.1.0...</p>