Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-09

Convert assembler to Pascal

12 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

Hi, I'm trying to convert Rijndael encryption method from TPLockBox to Lazarus/FPC.

While converting the code, I found this routine in Assembler. I'm wondering if somebody can help me convert this to Pascal?.

Here's the routine:

procedure XorMemPrim(var Mem1; const Mem2; Count : Cardinal); register;
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;

Thanks in advance,

Reply by email

Alexsander Rosa
to Leonardo M. Ramé

push esi // move register esi to the top of the stack pop esi // get the top of the stack and put into esi mov esi, eax // esi := eax
shr ecx, 2 // ecx shr 2 (shift right)
the same for and, xor, etc
jz // GOTO = jump if zero (if the result of last command is zero) jnz // GOTO = jump if not zero
add esi, 4 // esi := esi + 4
dec ecx // Dec(ecx)

Atenciosamente, Alexsander da Rosa http://rednaxel.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110913/8b7f9c82/attachment.html>

Reply by email

Ludo Brands
to Leonardo M. Ramé: Hi, I'm trying to convert Rijndael encryption method from TPLockBox to Lazarus/FPC. While…
Procedure XorMemPrim(var Mem1;  const Mem2;  Count : Cardinal);
Var i:integer;
  p1,p2:pointer;
Begin
;
;
for i:=1 to count div 4 do
  begin
  pdword(p1)^:=pdword(p1)^ xor pdword(p2)^;
  p1:=p1+4;
  p2:=p2+4;
  end;
For i:=1 to count mod 4 do
  begin
  pbyte(p1)^:=pbyte(p1)^ xor pbyte(p2)^;
  p1:=p1+1;
  p2:=p2+1;
  end;
End;

Ludo

Show quoted text 50 lines
procedure XorMemPrim(var Mem1; const Mem2; Count : Cardinal); register; 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; Thanks in advance, -- Leonardo M. Ram? http://leonardorame.blogspot.com -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Leonardo M. Ramé
to Ludo Brands
Ludo Brands wrote 21 lines
Procedure XorMemPrim(var Mem1; const Mem2; Count : Cardinal); Var i:integer; p1,p2:pointer; Begin ; ; for i:=1 to count div 4 do begin pdword(p1)^:=pdword(p1)^ xor pdword(p2)^; p1:=p1+4; p2:=p2+4; end; For i:=1 to count mod 4 do begin pbyte(p1)^:=pbyte(p1)^ xor pbyte(p2)^; p1:=p1+1; p2:=p2+1; end; End; Ludo

Wow!, that was fast. Thanks Ludo, I'll try it.

Reply by email

José Mejuto
to Leonardo M. Ramé: Hi, I'm trying to convert Rijndael encryption method from TPLockBox to Lazarus/FPC. While…

Hello Lazarus-List,

Tuesday, September 13, 2011, 2:42:25 PM, you wrote:

LMR> While converting the code, I found this routine in Assembler. I'm LMR> wondering if somebody can help me convert this to Pascal?. LMR> Here's the routine:

Basically:

LMR> procedure XorMemPrim(var Mem1;  const Mem2;  Count : Cardinal);
procedure XorMemPrim(var Mem1;  const Mem2;  Count : Cardinal);
var
  c: PtrUint;
begin
  for c:=0 to Count-1 do
  begin
    PBYTE(Mem1)[c]^:=PBYTE(Mem1)[c]^ xor PBYTE(Mem2)[c]^;
  end;
end;

Syntax not checked.

Best regards, Jos?

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé: Wow!, that was fast. Thanks Ludo, I'll try it. Leonardo M. Ram?…
Leonardo M. Ram? wrote 25 lines
On 2011-09-13 15:40:23 +0200, Ludo Brands wrote: > Procedure XorMemPrim(var Mem1; const Mem2; Count : Cardinal); > Var i:integer; > p1,p2:pointer; > Begin > ; > ; > for i:=1 to count div 4 do > begin > pdword(p1)^:=pdword(p1)^ xor pdword(p2)^; > p1:=p1+4; > p2:=p2+4; > end; > For i:=1 to count mod 4 do > begin > pbyte(p1)^:=pbyte(p1)^ xor pbyte(p2)^; > p1:=p1+1; > p2:=p2+1; > end; > End; > > Ludo Wow!, that was fast. Thanks Ludo, I'll try it.

Ludo, I did a quick comparison of the results against the Delphi 7 counterpart and the doesn't match.

Also, I tried to compile this in Delphi and I got "Ordinal type
required" on "p1 := P1 + 4". How can avoid this issue?.

Reply by email

Ludo Brands
to Leonardo M. Ramé
Show quoted text 6 lines
Ludo, I did a quick comparison of the results against the Delphi 7 counterpart and the doesn't match. Also, I tried to compile this in Delphi and I got "Ordinal type required" on "p1 := P1 + 4". How can avoid this issue?.

Should have told it was for delphi also:

Procedure XorMemPrim(var Mem1;  const Mem2;  Count : Cardinal); Var
i:integer;
  p1,p2:pointer;
Begin
;
;
for i:=1 to count div 4 do
  begin
  pdword(p1)^:=pdword(p1)^ xor pdword(p2)^;
  p1:=pointer(cardinal(p1)+4);
  p2:=pointer(cardinal(p2)+4);
  end;
For i:=1 to count mod 4 do
  begin
  pbyte(p1)^:=pbyte(p1)^ xor pbyte(p2)^;
  p1:=pointer(cardinal(p1)+1);
  p2:=pointer(cardinal(p2)+1);
  end;
End;

I compared the original and above in D6 and the result is the same. The simplistic test program used:

procedure TForm1.FormCreate(Sender: TObject);
var s,s2,s3:string;
i:integer;
begin
  s:='abcdefghij';
  s2:='bcdefghijk';
  XorMemPrim2(s[1],s2[1],length(s2));
  s3:='';
  for i:=1 to length(s2) do
    s3:=s3+inttohex(ord(s[i]),2);
  label1.Caption:=s3;
  s:='abcdefghij';
  s2:='bcdefghijk';
  XorMemPrim(s[1],s2[1],length(s2));
  s3:='';
  for i:=1 to length(s2) do
    s3:=s3+inttohex(ord(s[i]),2);
  label2.Caption:=s3;
end;

Reply by email

Ludo Brands
Show quoted text 22 lines
Should have told it was for delphi also: Procedure XorMemPrim(var Mem1; const Mem2; Count : Cardinal); Var i:integer; p1,p2:pointer; Begin ; ; for i:=1 to count div 4 do begin pdword(p1)^:=pdword(p1)^ xor pdword(p2)^; p1:=pointer(cardinal(p1)+4); p2:=pointer(cardinal(p2)+4); end; For i:=1 to count mod 4 do begin pbyte(p1)^:=pbyte(p1)^ xor pbyte(p2)^; p1:=pointer(cardinal(p1)+1); p2:=pointer(cardinal(p2)+1); end; End;

BTW. Use PtrUint instead of cardinal if you want to port this to 64 bits. But then it won't compile on Delphi anymore.

Ludo

Reply by email

Leonardo M. Ramé
to Ludo Brands: Should have told it was for delphi also: Procedure XorMemPrim(var Mem1; const Mem2; …
Ludo Brands wrote 9 lines
> Ludo, I did a quick comparison of the results against the > Delphi 7 counterpart and the doesn't match. > > Also, I tried to compile this in Delphi and I got "Ordinal > type required" on "p1 := P1 + 4". How can avoid this issue?. > Should have told it was for delphi also:

Well, no, it won't work on Delphi. I just wanted to compare the results between Lazarus and Delphi.

Reply by email

Ludo Brands
to Leonardo M. Ramé
Show quoted text 5 lines
> Should have told it was for delphi also: > Well, no, it won't work on Delphi. I just wanted to compare the results between Lazarus and Delphi.

You are forgiven ;)

You could have compared also with fpc on i386. Change

	procedure XorMemPrim(var Mem1;  const Mem2;  Count : Cardinal);
register;

into

	{$asmmode intel}
	procedure XorMemPrim(var Mem1;  const Mem2;  Count : Cardinal);
register;assembler;

and the assembler routine works also in fpc.

Ludo

Reply by email

Leonardo M. Ramé
to Ludo Brands
Ludo Brands wrote 21 lines
> > Should have told it was for delphi also: > > > Well, no, it won't work on Delphi. I just wanted to compare > the results between Lazarus and Delphi. > You are forgiven ;) You could have compared also with fpc on i386. Change procedure XorMemPrim(var Mem1; const Mem2; Count : Cardinal); register; into {$asmmode intel} procedure XorMemPrim(var Mem1; const Mem2; Count : Cardinal); register;assembler; and the assembler routine works also in fpc. Ludo

Ludo, thank you very much!. Now everything is working wonderfully on Lazarus.

Reply by email