Delphi 7 で asm 関数を作成しましたが、コードが別のものに変換されます。
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 <> 0 then
begin
c := bsf(x); // bitscanforward
x := 1 shl c;
Result := x or (x shl 1)
end
else
Result := 0;
end;
push ebx
とが生成されるのはなぜpop ebx
ですか? そして、それはなぜmov eax, ebx
ですか?
のせいで部分的なスタックフレームが生成されるようですmov eax, ebx
。
この単純なテストはmov eax, edx
、そのスタック フレームを生成しますが、生成しません。
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
と何か関係があるようですlabel err
。それを削除すると、その部分が得られませんmov eax, *
。
なぜこれが起こるのですか?
Quality Centralでバグ レポートを作成しました。