delphi中如何编程实现pos,copy函数

2025-03-07 16:08:33
推荐回答(3个)
回答1:

function MyPos(u: string; s: string) : integer;
var
i,j: integer;
begin
Result := 0;
for i := 1 to Length(s) do
begin
if s[i] = u[1] then
begin
j := 1;
while j <= length(u) do
begin
if u[j] = s[i+j-1] then
begin
j := j + 1;
continue;
end
else
break;
end;//while
if j > Length(u) then
Result := i
else Result := 0;
end; //if
end; //for
end;
这个函数是我自己写的花了5分钟,在Delhpi7下调试通过!
今晚没时间写COPY了,如果你真的需要就给我发消息吧。

回答2:

用下列汇编实现得。多给点分。
PUSH EBX
PUSH ESI
PUSH EDI

MOV ESI,EAX { Point ESI to substr }
MOV EDI,EDX { Point EDI to s }

XOR ECX,ECX { ECX = Length(s) }
MOV CL,[EDI]
INC EDI { Point EDI to first char of s }

PUSH EDI { remember s position to calculate index }

XOR EDX,EDX { EDX = Length(substr) }
MOV DL,[ESI]
INC ESI { Point ESI to first char of substr }

DEC EDX { EDX = Length(substr) - 1 }
JS @@fail { < 0 ? return 0 }
MOV AL,[ESI] { AL = first char of substr }
INC ESI { Point ESI to 2'nd char of substr }

SUB ECX,EDX { #positions in s to look at }
{ = Length(s) - Length(substr) + 1 }
JLE @@fail
@@loop:
REPNE SCASB
JNE @@fail
MOV EBX,ECX { save outer loop counter }
PUSH ESI { save outer loop substr pointer }
PUSH EDI { save outer loop s pointer }

MOV ECX,EDX
REPE CMPSB
POP EDI { restore outer loop s pointer }
POP ESI { restore outer loop substr pointer }
JE @@found
MOV ECX,EBX { restore outer loop counter }
JMP @@loop

@@fail:
POP EDX { get rid of saved s pointer }
XOR EAX,EAX
JMP @@exit

@@found:
POP EDX { restore pointer to first char of s }
MOV EAX,EDI { EDI points of char after match }
SUB EAX,EDX { the difference is the correct index }
@@exit:
POP EDI
POP ESI
POP EBX

回答3:

不太明白你的意思,这些函数在delphi里可以直接使用的?你是不知道怎么用?还是想重新重载使用?汇编不会 不好意思