我正在使用这个组件http://sourceforge.net/projects/tponguard/,现在我需要在64位编译。我被困在这里了。
它是这样的:
push esi
push edi
mov esi, eax //esi = Mem1
mov edi, edx //edi = Mem2
push ecx //save byte count
shr ecx, 2 //convert to dwords
jz @Continue
cld
@Loop1: //xor dwords at a time
mov eax, [edi]
xor [esi], eax
add esi, 4
add edi, 4
dec ecx
jnz @Loop1
@Continue: //handle remaining bytes (3 or less)
pop ecx
and ecx, 3
jz @Done
@Loop2: //xor remaining bytes
mov al, [edi]
xor [esi], al
inc esi
inc edi
dec ecx
jnz @Loop2
@Done:
pop edi
pop esi然后我改成了这个:
push rsi
push rdi
mov rsi, rax //esi = Mem1
mov rdi, rdx //edi = Mem2
push rcx //save byte count
shr rcx, 2 //convert to dwords
jz @Continue
cld
@Loop1: //xor dwords at a time
mov rax, [rdi]
xor [rsi], rax
add rsi, 4
add rdi, 4
dec rcx
jnz @Loop1
@Continue: //handle remaining bytes (3 or less)
pop rcx
and rcx, 3
jz @Done
@Loop2: //xor remaining bytes
mov al, [rdi]
xor [rsi], al
inc rsi
inc rdi
dec rcx
jnz @Loop2
@Done:
pop rdi
pop rsi但是现在我在xor rsi,rax中遇到访问冲突。
发布于 2013-06-10 21:14:00
您正在查看的函数是
procedure XorMem(var Mem1; const Mem2; Count : Cardinal); register;从ogutil单位。
就我个人而言,我不会费心将其转换为x64汇编程序。为了做到这一点,您需要正确地处理一些棘手的细节。对我来说,移植到Pascal并让编译器处理细节更有意义。最简单、最幼稚的翻译如下所示:
procedure XorMem(var Mem1; const Mem2; Count: Cardinal);
var
p1, p2: PByte;
begin
p1 := PByte(@Mem1);
p2 := PByte(@Mem2);
while Count>0 do
begin
p1^ := p1^ xor p2^;
inc(p1);
inc(p2);
dec(Count);
end;
end;如果这是性能关键型的,那么您可能希望稍微展开循环以对大操作数进行操作。比如x86上的32位操作数和x64上的64位操作数。
对32位操作数进行操作的版本可能如下所示:
procedure XorMem(var Mem1; const Mem2; Count: Cardinal);
var
p1, p2: PByte;
begin
p1 := PByte(@Mem1);
p2 := PByte(@Mem2);
while Count>3 do
begin
PCardinal(p1)^ := PCardinal(p1)^ xor PCardinal(p2)^;
inc(p1, 4);
inc(p2, 4);
dec(Count, 4);
end;
while Count>0 do
begin
p1^ := p1^ xor p2^;
inc(p1);
inc(p2);
dec(Count);
end;
end;实际上,您可以很容易地编写一个由编译目标确定的自动使用32或64位操作数的版本。诀窍是使用NativeUInt类型,它是机器字大小。
procedure XorMem(var Mem1; const Mem2; Count: Cardinal);
var
p1, p2: PByte;
begin
p1 := PByte(@Mem1);
p2 := PByte(@Mem2);
while Count>SizeOf(NativeUInt)-1 do
begin
PNativeUInt(p1)^ := PNativeUInt(p1)^ xor PNativeUInt(p2)^;
inc(p1, SizeOf(NativeUInt));
inc(p2, SizeOf(NativeUInt));
dec(Count, SizeOf(NativeUInt));
end;
while Count>0 do
begin
p1^ := p1^ xor p2^;
inc(p1);
inc(p2);
dec(Count);
end;
end;这个最终版本在启用优化的情况下编译时效率非常高。我不会把目光投向最终的Pascal版本。
https://stackoverflow.com/questions/17023838
复制相似问题