A recent Delphi project i've inherited has a procedure in ASM. I'm a complete ASM newbie, so i dont understand it. I've read up on the various ASM instructions to try and decipher the procedures flow, but i still dont get it.
Could someone with ASM experience assist my understanding and translate the following procedure to English (then i can translate back to Delphi so the code is easier to read in the future!!!)
The declaration of Mem1 is an array [0..15] of Byte;. And Mem2 is a LongInt.
Here's the procedure:
procedure TForm1.XorMem(var Mem1; const Mem2; Count : Cardinal); register;
begin
asm
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
end;
end;
edit: Thanks to Roman R i've converted the ASM back to Delphi
procedure TForm1.XorMem2(var Mem1; const Mem2 :LongInt; Count : Cardinal);
var
Key : array [0..3] of byte absolute Mem1;
Num : array [0..3] of byte absolute Mem2;
idx : byte;
begin
for Idx := 0 to Count -1 do Key[idx] := Key[idx] Xor Num[idx];
end;
Mem2: Longint
is not good in your code. Only if Count is <=4. And if you compile with range checking ON, this method will fail with Count>4. – VoroshilovskMem1
receives the result of xoringMem2
with something. We can't yet be certain that the other operand isMem1
, but it's a reasonable guess, and that gives us something to watch for when reading the code. – Sherrylsherurd