汇编下如何处理字符串?

natasha 2005-07-22 03:35:05
汇编环境下,如何操作字符串的某位?
例子如下:
procedure StringFunc;
var
S: String;
begin
S := '12345';
asm
push eax
push ebx
push ecx
mov eax, S //Pointer(S)
mov ecx, [eax - 4] //Length(S)
@@LOOP:
test ecx, ecx
je @@RET
mov bl, byte ptr [eax + ecx - 1] //S[ecx]
//... //这里对bl进行处理
//mov byte ptr [eax + ecx - 1] //问题出现这里,编译有错误,该内存不能写入
//注释上句掉就没有问题,但是我需要把处理完的数据重新写入,请问如何操作???
dec ecx
jmp @@LOOP
@@RET:
pop ecx
pop ebx
pop eax
end;
end;

顺便帮忙看看还有没有问题,或者还可以怎么改进更简洁更精练.
...全文
239 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝色光芒 2005-07-22
  • 打赏
  • 举报
回复
这么给你说吧,
S := '12345';
在编译时被优化了,也就是说,S被指向了一个Const
如果换成
S := Inttostr(12345);
试试。。。
natasha 2005-07-22
  • 打赏
  • 举报
回复
主要问题是:如何改进 mov byte ptr [eax + ecx - 1], bl使运行不报错.
还是我写得不对?
蓝色光芒 2005-07-22
  • 打赏
  • 举报
回复
要所简洁的话:
push eax
push ebx
push ecx
换成PUSHAD;
pop ecx
pop ebx
pop eax
换成POPAD;

LOOP的那个循环也可以优化,

就你给的程序而言,优化后的代码为:

var
S : String;
begin
S := '12345';
asm
PUSHAD;
mov eax, S //Pointer(S)
mov ecx, [eax - 4] //Length(S)
@@LOOP:
mov bl, byte ptr [eax + ecx - 1] //S[ecx]
//... //这里对bl进行处理
//mov byte ptr [eax + ecx - 1] //问题出现这里,编译有错误,该内存不能写入
//注释上句掉就没有问题,但是我需要把处理完的数据重新写入,请问如何操作???
LOOP @@LOOP
@@RET:
POPAD;
end;
end;
natasha 2005-07-22
  • 打赏
  • 举报
回复
我就想事先汇编下,修改字符串的某位.
mov byte ptr [eax + ecx - 1]这是写漏了,应该是:
mov byte ptr [eax + ecx -1], bl

上面的程序再改一下:
procedure StringFunc;
var
S: String;
begin
S := '12345';
asm
push eax
push ebx
push ecx
mov eax, S //Pointer(S)
je @@RET
mov ecx, [eax - 4] //Length(S)
@@LOOP:
test ecx, ecx
je @@RET
mov bl, byte ptr [eax + ecx - 1] //S[ecx]
//... //这里对bl进行处理
//mov byte ptr [eax + ecx - 1], bl //问题出现这里,编译有错误,该内存不能写入
//注释上句掉就没有问题,但是我需要把处理完的数据重新写入,请问如何操作???
dec ecx
jmp @@LOOP
@@RET:
pop ecx
pop ebx
pop eax
end;
end;
蓝色光芒 2005-07-22
  • 打赏
  • 举报
回复
mov byte ptr [eax + ecx - 1]
单单这句,没有源操作符,不对
蓝色光芒 2005-07-22
  • 打赏
  • 举报
回复
你想要实现什么过程,我帮你写。:)
natasha 2005-07-22
  • 打赏
  • 举报
回复
UP
natasha 2005-07-22
  • 打赏
  • 举报
回复
function IncString(const S: string): string;
begin
Result := S;
asm
pushad
mov eax, Result
test eax, eax
mov eax, [eax]
je @@RET
mov ecx, [eax - 4]
@@LOOP:
inc byte ptr [eax + ecx - 1]
loop @@LOOP
@@RET:
popad
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
begin
S := '12345';
S := IncString(S);
end;

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧