How do you search a string in Irvine Assembly Language and replace it with a substring?

落爺英雄遲暮 提交于 2020-01-17 03:34:07

问题


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?


回答1:


For 8-bit characters it's broadly like this, there are many ways to implement it:

  1. Set si to point to the first character of the string.

  2. mov al,[si]

  3. repnz scasb to find the first match of the first character.

  4. Store the address somewhere.

  5. Set di to point to the first character of the replacement string ('dog' in this case).

  6. Set cx/ecx/rcx to string length.

  7. repz cmpsb

  8. Check that cx/ecx/rcx is zero and last characters match.

  9. 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.

  10. 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).

For UTF-8 (16-bit characters) replace the following: scasb -> scasw, cmpsb -> cmpsw, movsb -> movsw, al -> ax.

For 32-bit code, replace all references to si with esi and all references to di with edi.

For 64-bit code, replace all references to si with rsi and all references to di with rdi.



来源:https://stackoverflow.com/questions/13616384/how-do-you-search-a-string-in-irvine-assembly-language-and-replace-it-with-a-sub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!