我寫的彙編函數在Delphi 7,但其轉換我的代碼到別的東西:Delphi標籤和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的錯誤報告。
請報告此作爲http://qc.embarcadero.com/wc/qcmain.aspx – 2010-03-08 00:02:57
@Jeroen一定的錯誤。沒有問題... – Egon 2010-03-08 07:55:16
您在這裏問了幾個「爲什麼」的問題,但沒有人回答您接受的答覆。看起來你真的只是想知道如何跳到Delphi彙編器中的新指令,而不考慮你自己嘗試失敗的原因。這是否準確? – 2010-03-08 09:01:08