如何在Irvine汇编语言中搜索字符串并将其替换为子字符串? [英] How do you search a string in Irvine Assembly Language and replace it with a substring?
问题描述
我正在编写一个程序,该程序读取一个字符串,然后搜索某些关键字,例如"cat",然后将其替换为"dog".我只是不确定如何启动它.我必须使用什么代码?
I am writing a program that reads a string then searches for certain keywords, like "cat", and replaces it with "dog". I am just unsure as how to start it. What code will I have to use?
推荐答案
对于8位字符,大致上是这样的,有很多实现方法:
For 8-bit characters it's broadly like this, there are many ways to implement it:
-
设置
si
指向字符串的第一个字符.
Set
si
to point to the first character of the string.
mov al,[si]
repnz scasb
查找第一个字符的第一个匹配项.
repnz scasb
to find the first match of the first character.
将地址存储在某个地方.
Store the address somewhere.
设置di
指向替换字符串的第一个字符(在这种情况下为'dog'
).
Set di
to point to the first character of the replacement string ('dog'
in this case).
将cx/ecx/rcx
设置为字符串长度.
repz cmpsb
检查cx/ecx/rcx
为零并且最后一个字符匹配.
Check that cx/ecx/rcx
is zero and last characters match.
如果是,这是匹配项,因此将'dog'
复制到使用rep movsb
存储的地址(首先设置指针si
和di
).请注意,仅当替换字符串不超过原始字符串时,此方法才有效.如果更长,则可能需要保留一个新的内存块,以避免缓冲区溢出.如果不匹配,则将si
回溯到存储的地址,将si
递增1(对于16位字符,递增2),然后跳至2.(mov al,[si]
).到达字符串末尾时,还需要在此处检查.
If yes, it's a match, so copy 'dog'
to the address stored with rep movsb
(set pointers si
and di
first). Do note that this approach only works if the replace string is no longer than the original string. If it's longer, you may need to reserve a new block of memory to avoid a buffer overflow. If it's not a match, backtrack si
to the stored address, increment si
by 1 (by 2 for 16-bit characters), and jump to 2. (mov al,[si]
). You need to also check here when you have reached the end of the string.
准备好了.或者,如果您要替换所有内容,如sed s/cat/dog/g
中一样,从1开始循环,请首先设置指针(si
)(取决于您希望正则表达式引擎的工作方式).
Ready. Or, if you want to replace all, as in sed s/cat/dog/g
, loop from 1, set pointer (si
) first (depending on how you want your regex engine to work).
对于UTF-8(16位字符),替换以下内容:scasb
-> scasw
,cmpsb
-> cmpsw
,movsb
-> movsw
,al
-> ax
.
For UTF-8 (16-bit characters) replace the following: scasb
-> scasw
, cmpsb
-> cmpsw
, movsb
-> movsw
, al
-> ax
.
对于32位代码,将所有对si
的引用替换为esi
,并将对所有di
的引用替换为edi
.
For 32-bit code, replace all references to si
with esi
and all references to di
with edi
.
对于64位代码,将所有对si
的引用替换为rsi
,并将对所有di
的引用替换为rdi
.
For 64-bit code, replace all references to si
with rsi
and all references to di
with rdi
.
这篇关于如何在Irvine汇编语言中搜索字符串并将其替换为子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!