简体   繁体   English

如何删除从字符串末尾开始计数的字符?

[英]How to remove characters counting from the end of a string?

I have to remove a certain number of characters(let's say 3) from the end of a string. 我必须从字符串末尾删除一定数量的字符(比方说3个字符)。 For this particular string it works when I find 'Z', and then make it point to W by sub edi, 3, and then storing the rest of the string with 0's. 对于这个特定的字符串,当我找到“ Z”时,它起作用,然后用sub edi 3使其指向W,然后将其余的字符串存储为0。

INCLUDE Irvine32.inc

.data
source BYTE "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0

.code
main PROC

mov edi, OFFSET source
mov al, 'Z'                  ; search for Z
mov ecx, LENGTHOF source
cld
repne scasb       ; repeat while not equal
sub edi, 3         ; now points to W

mov al, 0         
rep stosb        ; stores all characters after W with 0.

mov edx, OFFSET source
call WriteString
call CrlF

exit
main ENDP
end main

However, I want to make this code work with different null terminated strings. 但是,我想使此代码与其他以null终止的字符串一起使用。 For that purpose I tried finding 0(the end of the string), as my code shows below. 为此,我尝试查找0(字符串的末尾),如下面的代码所示。 But it doesn't work, It just outputs the whole string. 但是它不起作用,它只输出整个字符串。

What should I do to ensure that the code still works even if the string is changed without having to change the search for the end of the string every time? 我应该怎么做以确保即使更改了字符串而不必每次都更改对字符串结尾的搜索,代码仍然可以正常工作?

INCLUDE Irvine32.inc

.data
source BYTE "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0

.code
main PROC

mov edi, OFFSET source
mov al, '0'                  ; search for end of string
mov ecx, LENGTHOF source
cld
repne scasb       ; repeat while not equal
sub edi, 3         ; now points to W ??

mov al, 0         
rep stosb        ; stores all characters after W with 0.

mov edx, OFFSET source
call WriteString
call CrlF

exit
main ENDP
end main

The error is that you don't reset ECX with any value. 错误是您没有将ECX重置为任何值。

You set ECX to the length of the data, in this case 27. Then you find Z, which is the 26th value, so in the end ECX will have value 1 and your rep stosb will write one 0 in the string. 您可以设置ECX的数据的长度,在这种情况下,27。然后你发现Z,这是26号值,所以最后ECX将有值1 ,你的rep stosb会写一个0中的字符串中。 Then you print it and that first nul will stop printing. 然后打印它,第一个nul将停止打印。

In the second one you're looking for 0 which is the last byte. 在第二个中,您正在寻找0 ,这是最后一个字节。 Exiting the repne scasb ECX is zero, so rep stosb doesn't write anything. 退出repne scasb ECX为零,因此rep stosb不写任何内容。

To fix this you can use 要解决此问题,您可以使用

mov edi, OFFSET source
mov al, 0                  ; search for end of string
mov ecx, LENGTHOF source
cld
repne scasb       ; repeat while not equal
sub edi, 3         ; now points to W ??

mov byte ptr [edi], 0     ; store a single nul since that's enough

Do note that in the first one "stores all characters after W with 0" is not true. 请注意,在第一个中“将W后面的所有字符存储为0”是不正确的。 You would have to do ADD ECX, 3 after SUB EDI, 3 to do that. 您必须在SUB EDI, 3之后执行ADD ECX, 3操作。

Do also note that '0' is the character 0, not the value 0. 还要注意, '0'是字符0,而不是值0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM