只改变字符串中的一个字母

Change only one letter in string

我有 2 个字符串和一个字母。

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

基于 ,如果 selectedWords 有字母 C,我将编写代码,如果是这种情况,他需要更改字符串 guessWords:

猜词“--C-C--”

但出于某种奇怪的原因,我得到了所有其他可能性,只是不正确。关于如何解决这个问题的一些建议。

首先,忘记所谓的字符串指令(scas、comps、movs)。其次,您需要一个带有索引的固定指针 (dispkacement),例如 [esi+ebx]。您是否考虑过 WriteString 需要一个以 null 结尾的字符串?

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