Search code examples
assemblyx86masmmasm32irvine32

How do I remove everything after a certain character in a string?


How do I remove everything in the string after the '?' ? The code I have so far searches for the '?'. How do I proceed from there?

This is my code.

INCLUDE Irvine32.inc

.data
source BYTE "Is this a string? Enter y for yes, and n for no",0

.code
main PROC

mov edi, OFFSET source
mov al, '?'                  ; search for ?
mov ecx, LENGTHOF source
cld
repne scasb       ; repeat while not equal
jnz quit
dec edi           ; edi points to ?

end main

Solution

  • You can replace everything after the "?" by zeroes, so all the characters after "?" are been "removed" :

    INCLUDE Irvine32.inc
    
    .data
    source BYTE "Is this a string? Enter y for yes, and n for no",0
    
    .code
    main PROC
    
    mov edi, OFFSET source
    mov al, '?'                  ; search for ?
    mov ecx, LENGTHOF source
    cld
    repne scasb       ; repeat while not equal
    jnz quit
    dec edi           ; edi points to ?
    
    ;REPLACE ALL CHARACTERS BY "AL" (ZERO) STARTING WHERE "EDI" WAS AND
    ;FINISH WHEN "ECX" == 0.
    mov al, 0         ;<=====================================
    repne stosb       ;<=====================================
    
    end main
    

    Notice how we are using the values that EDI and ECX have after searching for "?".