title: Delphi label and asm weirdness?
body: <p>I written an asm function in Delphi 7 but it transforms my code to something else:</p>
  
  <pre><code>function f(x: Cardinal): Cardinal; register;
  label err;
  asm
    not eax
    mov edx,eax
    shr edx, 1
    and eax, edx
    bsf ecx, eax
    jz  err
    mov eax, 1
    shl eax, cl
    mov edx, eax
    add edx, edx
    or  eax, edx
    ret
    err:
    xor eax, eax
  end;
  
  // compiled version
  f:
    push ebx       // !!!
    not eax
    mov edx,eax
    shr edx, 1
    and eax, edx
    bsf ecx, eax
    jz  +$0e
    mov eax, 1
    shl eax, cl
    mov edx, eax
    add edx, edx
    or  eax, edx
    ret
    err:
    xor eax, eax
    mov eax, ebx   // !!!
    pop ebx        // !!!
    ret
  
  // the almost equivalent without asm
  function f(x: Cardinal): Cardinal;
  var
    c: Cardinal;
  begin
    x := not x;
    x := x and x shr 1;
    if x &lt;&gt; 0 then
    begin
      c := bsf(x); // bitscanforward
      x := 1 shl c;
      Result := x or (x shl 1)
    end
    else
      Result := 0;
  end;
  </code></pre>
  
  <p>Why does it generate <code>push ebx</code> and <code>pop ebx</code>? And why does it do <code>mov eax, ebx</code>?</p>
  
  <p>It seems that it generates the partial stack frame because of the <code>mov eax, ebx</code>.</p>
  
  <p>This simple test generates <code>mov eax, edx</code> but doesn't generate that stack frame:</p>
  
  <pre><code>function asmtest(x: Cardinal): Cardinal; register;
  label err;
  asm
    not eax
    and eax, 1
    jz  err
    ret
    err:
    xor eax, eax
  end;
  
  // compiled
  asmtest:
    not eax
    and eax, $01
    jz +$01
    ret
    xor eax, eax
    mov eax, edx  // !!!
    ret
  </code></pre>
  
  <p>It seems that it has something to do with the <code>label err</code>. If I remove that I don't get the <code>mov eax, *</code> part.</p>
  
  <p>Why does this happen?</p>
  
  <hr>
  
  <p>Made a bug report on <a href="http://qc.embarcadero.com/wc/qcmain.aspx?d=82774" rel="nofollow noreferrer">Quality Central</a>.</p>