Search code examples
arraysstringassemblymasmirvine32

Change only one letter in string


I have 2 string and one letter.

selectedWords BYTE "BICYCLE"
guessWords BYTE "-------"
inputLetter BYTE 'C'

Base on this answers, I write code who compere if selectedWords have letter C and If this is the case he need to change string guessWords:

guessWords "--C-C--"

But from some strange reason I get all other possibilities, just not correct one. Some suggestions on how to solve this problem.


Solution

  • First, forget the so called string instructions (scas, comps, movs). Second, you need a fixed pointer (dispkacement) with an index, e.g [esi+ebx]. Have you considered that WriteString needs a null-terminated string?

    INCLUDE Irvine32.inc
    
    .DATA
    selectedWords BYTE "BICYCLE"
    guessWords BYTE SIZEOF selectedWords DUP ('-'), 0   ; With null-termination for WriteString
    inputLetter BYTE 'C'
    
    .CODE
    main PROC
    
        mov esi, offset selectedWords       ; Source
        mov edi, offset guessWords          ; Destination
        mov ecx, LENGTHOF selectedWords     ; Number of bytes to check
        mov al, inputLetter                 ; Search for that character
        xor ebx, ebx                        ; Index EBX = 0
    
        ride_hard_loop:
        cmp [esi+ebx], al                   ; Compare memory/register
        jne @F                              ; Skip next line if no match
        mov [edi+ebx], al                   ; Hang 'em lower
        @@:
        inc ebx                             ; Increment pointer
        dec ecx                             ; Decrement counter
        jne ride_hard_loop                  ; Jump if ECX != 0
    
        mov edx, edi
        call WriteString                    ; Irvine32: Write a null-terminated string pointed to by EDX
        exit                                ; Irvine32: ExitProcess
    
    main ENDP
    
    END main