Search code examples
arraysstringassemblymasmirvine32

Returning pointer to the letter in string


This is my code to search ih some string consists of one letter:

selectedWords BYTE "BICYCLE"
inputLetter BYTE 'Y'

cld                                 
mov ecx, LENGTHOF selectedWords     
mov edi, offset selectedWords       
mov al, inputLetter                 ;Load character to find
repne scasb                         ;Search
jne notfound

But how to return the pointer to the letter in string?

If I want after to change one leter with some other. Its easy to do if you have pointer to the letter in string.


Solution

  • If repne scasb finds the element, EDI points to the element after the first match. You have to decrement it to get the pointer to the desired element.

    You don't need to clear the direction flag (cld). It's very very unlikelikely that the direction flag is set without any involvement of your part. And if so, you should seit it back to the former status.

    INCLUDE Irvine32.inc
    
    .DATA
    selectedWords BYTE "BICYCLE"
    inputLetter BYTE 'Y'
    
    err_msg BYTE "Not found.", 0
    
    .CODE
    main PROC
    
        mov ecx, LENGTHOF selectedWords
        mov edi, offset selectedWords
        mov al, inputLetter                 ; Load character to find
        repne scasb                         ; Search
        jne notfound
    
        dec edi
        mov al, [edi]
        call WriteChar                      ; Irvine32: Write a character in AL
    
        exit                                ; Irvine32: ExitProcess
    
        notfound:
        lea edx, err_msg
        call WriteString                    ; Irvine32: Write a null-terminated string pointed to by EDX
        exit                                ; Irvine32: ExitProcess
    
    main ENDP
    
    END main
    

    If you don't like repne scasb you can scan the word with a "normal" comparison loop

    INCLUDE Irvine32.inc
    
    .DATA
    selectedWords BYTE "BICYCLE"
    inputLetter BYTE 'Y'
    
    err_msg BYTE "Not found.", 0
    
    .CODE
    main PROC
    
        mov edi, offset selectedWords
        mov ecx, LENGTHOF selectedWords
        mov al, inputLetter
    
        @@:
        cmp [edi], al                       ; Compare memory/immediate value
        je found                            ; JE = jump if equal
        inc edi                             ; Increment pointer
        dec ecx                             ; Decrement counter
        jne @B                              ; Jump back to the last @@, if ECX == 0
    
        jmp notfound
    
        found:
        mov al, [edi]
        call WriteChar                      ; Irvine32: Write a character in AL
    
        exit                                ; Irvine32: ExitProcess
    
        notfound:
        lea edx, err_msg
        call WriteString                    ; Irvine32: Write a null-terminated string pointed to by EDX
        exit                                ; Irvine32: ExitProcess
    
    main ENDP
    
    END main