I have to remove a certain number of characters(let's say 3) from the end of a string. 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.
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 i.e. if the source is changed by the user to say some thing different(E.g.source byte "This is a string", 0). For that purpose I tried finding 0(the end of the string), as my code shows below. 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