制作 --- 的字符串,末尾为 0

Making string of --- with 0 on the end

我不能使用这个声明,因为 selectedWords 可以是任何字符串。

.DATA
guessWords BYTE SIZEOF selectedWords DUP ('-'), 0  

所以我尝试这样做:

;Wordls what we select by rundom code
selectedWords BYTE ?
lengthSelectedWorld DWORD ?

;Letter what we guess, input from keyboard
guessLetter BYTE ?
guessWords BYTE ? 
;Letter what are unknows, change with - 
letterUnknown BYTE "-", 0

而且我已经写了这个函数

make_array1 PROC     

    mov  edx,OFFSET selectedWords
    call StrLength
    mov  lengthSelectedWorld,eax
    mov  lengthSelectedWorld1 ,eax
    inc  lengthSelectedWorld

    loop_add_more:
        cmp lengthSelectedWorld, 1 
        je done
        dec lengthSelectedWorld       
        mov eax, '-'
        mov ecx, lengthSelectedWorld1
        mov edi, offset guessWords
        rep stosw

        mov edx, offset guessWords
        call WriteString
        call Crlf ;stampamo enter novi red

        jmp loop_add_more    

    done:
        mov eax, '0'
        mov ecx, lengthSelectedWorld1
        mov edi, offset guessWords
        rep stosw

        mov edx, offset guessWords
        call WriteString
        call Crlf ;stampamo enter novi red

        ret

make_array1 ENDP  

但是在这个函数之后我得到了 guessWords what is string of ------- 并且在 and 上没有 0。那么如何让字符串guessWords=--------0呢?

由于代码中的一些其他比较,字符串末尾有 0 对我来说很重要。

selectedWords BYTE ? 只为 selectedWords 保留一个字节。 guessWords BYTE ? 同样的问题。不要像新手一样玩动态分配的内存。而是保留在任何情况下都足够的 space:guessWords BYTE 50 DUP (?)。问号意味着MASM可以决定将其视为未初始化的内存(不存储在.exe文件中,而是在程序启动时分配)。

STOSW 存储一个 WORD(= 两个字符)。然而 Irvine 的 StrLength returns 字符串的字节数。请改用 STOSBSTOSB之后,EDI指向最后存储的AL之后的字符。您可以在那里存储空值。想看的话,暂时把0改成'0'吧。

INCLUDE Irvine32.inc

.DATA

;Wordls what we select by rundom code
selectedWords BYTE "WEIGHTLIFTING", 0
lengthSelectedWord DWORD ?

;Letter what we guess, input from keyboard
guessLetter BYTE ?
guessWords BYTE 50 DUP (?)

;Letter what are unknows, change with -
letterUnknown BYTE "-", 0

.CODE

make_array1 PROC

    mov  edx,OFFSET selectedWords
    call StrLength                      ; Irvine32: Length of a null-terminated string pointed to by EDX
    mov  lengthSelectedWord,eax

    loop_add_more:
        mov al, '-'                     ; Default charcter for guessWords
        mov ecx, lengthSelectedWord     ; REP counter
        mov edi, offset guessWords      ; Destination
        rep stosb                       ; Build guessWords
        mov BYTE PTR [edi], 0           ; Store the null termination

        mov edx, offset guessWords
        call WriteString                ; Irvine32: write a string pointed to by EDX
        call Crlf                       ; Irvine32: new line

        ret

make_array1 ENDP

main PROC

    call make_array1
    exit                                ; Irvine32: ExitProcess

main ENDP

END main