-3

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
kc9552
  • 25
  • 3
  • 10

1 Answers1

0

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.

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
Safford96
  • 23
  • 1
  • 7
  • I want to delete the characters from a string without copying it to another. Thanks for your answer though. Appreciate your effort. :) – kc9552 May 05 '16 at 02:23