简体   繁体   English

如何从字符串的末尾删除一定数量的字符?

[英]How to remove a set number of 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 strings ie if the source is changed by the user to say some thing different(Egsource byte "This is a string", 0).但是,我想让这段代码适用于不同的字符串,即如果用户更改了源以说出不同的内容(例如源字节“这是一个字符串”,0)。 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

I think you need to use repeat move string byte.我认为您需要使用重复移动字符串字节。 I got this from another website.这是我从另一个网站上得到的。 In your case you would move the length of your string - 10 into cx.在您的情况下,您会将字符串的长度 - 10 移动到 cx 中。

mov     ax,@data 
mov     ds,ax     ;initialize ds 
mov     es,ax     ;and es 
lea     si,[str1] ;si points to source string 
lea     di,[str2] ;di points to dest string 
cld               ;set df=0 (increasing) 
mov     cx,5      ;# of chars in string1 
rep movsb         ;copy the string

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

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