代码3:function IntRol(v: LongWord): LongWord; var bits: Integer; tv: LongWord; begin tv := $80000000; bits := 31; while (bits >= 0) and (v and tv = 0) do begin Dec(bits); tv := tv shr 1; end; if bits > 0 then Result := ((v and not tv) shl 1) or (v shr bits) else Result := v; end;
前面代码1处理v=1时cl超范围,可改为:function IntRol(v: LongWord): LongWord; asm cmp eax, 1 jbe @@1 bsr ecx, eax xor ecx, 31 xor edx, edx inc ecx shld edx, eax, cl shl eax, cl dec ecx shr eax, cl or eax, edx @@1: end;代码3也可做些简化:function IntRol(v: LongWord): LongWord; var bits: Integer; tv: LongWord; begin if v > 1 then begin tv := $80000000; bits := 31; while v and tv = 0 do begin Dec(bits); tv := tv shr 1; end; Result := ((v and not tv) shl 1) or 1; end else Result := v; end;
procedure MoveLeft(var AStr: string); var c: char; ALen: integer; begin //循环左移字符串 ALen := Length(AStr); c := AStr[1]; Move(AStr[2], AStr[1], ALen - 1); AStr[ALen] := c; end;procedure MoveRight(var AStr: string); var c: char; ALen: integer; begin //循环右移字符串 ALen := Length(AStr); c := AStr[ALen]; move(AStr[1], AStr[2], ALen - 1); AStr[ALen] := c; end;
1、得看你是移动的单位,是一次移动一个字节和还是一次只移一个bit
2、移动数据的数据类型是什么呢?
asm
bsr ecx, eax
xor ecx, 31
xor edx, edx
inc ecx
shld edx, eax, cl
shl eax, cl
dec cl
shr eax, cl
or eax, edx
end;代码2:function IntRol(v: LongWord): LongWord;
asm
push eax
bsr ecx, eax
mov edx, 1
shl edx, cl
not edx
and eax, edx
pop edx
shl eax, 1
shr edx, cl
or eax, edx
end;
var
bits: Integer;
tv: LongWord;
begin
tv := $80000000;
bits := 31;
while (bits >= 0) and (v and tv = 0) do
begin
Dec(bits);
tv := tv shr 1;
end;
if bits > 0 then
Result := ((v and not tv) shl 1) or (v shr bits)
else
Result := v;
end;
asm
cmp eax, 1
jbe @@1
bsr ecx, eax
xor ecx, 31
xor edx, edx
inc ecx
shld edx, eax, cl
shl eax, cl
dec ecx
shr eax, cl
or eax, edx
@@1:
end;代码3也可做些简化:function IntRol(v: LongWord): LongWord;
var
bits: Integer;
tv: LongWord;
begin
if v > 1 then
begin
tv := $80000000;
bits := 31;
while v and tv = 0 do
begin
Dec(bits);
tv := tv shr 1;
end;
Result := ((v and not tv) shl 1) or 1;
end
else
Result := v;
end;
var
c: char;
ALen: integer;
begin
//循环左移字符串
ALen := Length(AStr);
c := AStr[1];
Move(AStr[2], AStr[1], ALen - 1);
AStr[ALen] := c;
end;procedure MoveRight(var AStr: string);
var
c: char;
ALen: integer;
begin
//循环右移字符串
ALen := Length(AStr);
c := AStr[ALen];
move(AStr[1], AStr[2], ALen - 1);
AStr[ALen] := c;
end;