Assembler, 32 bit, 64
-
There's an assembler code that I use in 32 battles of Delphi.
function RotateRightBits(const Value:LongWord; const Bits:Byte):LongWord;register;assembler; asm MOV CL,DL ROR EAX,CL end;
I've been rewriting the program in the 64-battered FreePascal, and I've encountered the difficulty of translating this code. Because he doesn't return the number of 32 battles.
Delphi's code analog works ten times longer, which makes me uncomfortable.
function RotateRightBits(const Value:LongWord;const Bits:Byte):LongWord; var I,B:integer; begin Result := Value; if Bits >= 32 then B := Bits mod 32 else B := Bits; for I := 1 to B do if Result and 1 = 0 then Result := Result shr 1 else Result := (Result shr 1) or $80000000; end;
It's hard to rewrite the 32-bit assembler code under 64-bit registers so he can work identically?
-
In x32, the parameters in the function are transmitted through eax, edx, ecx registers and the return is transmitted through the eax register.
In x64, the parameters in the function are transmitted through registers rcx, rdx, r8, r9, and the return is transmitted through the rax register.
So the function needs to be written like this.
function RotateRightBits(Value: LongWord; Bits: Byte): LongWord; register; assembler; asm {$IFDEF CPUx64} mov rax, rcx {$ENDIF} mov cl, dl ror eax, cl end;
If a 64-battered function is to work with 64 battles, it should be written.
// Тип NativeUInt имеет различный размер в зависимости от платформы function RotateRightBits(Value: NativeUInt; Bits: Byte): NativeUInt; register; assembler; asm {$IFDEF CPUx32} mov cl, dl ror eax, cl {$ELSE} mov rax, rcx mov cl, dl ror rax, cl {$ENDIF} end;