制作---的字符串---结尾为0

时间:2017-06-04 11:50:41

标签: arrays string assembly masm irvine32

我无法使用此声明,因为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  

但是在这个函数之后,我得到了猜词是什么是-------并且在和上没有0。那么如何制作字符串guessWords = ------- 0?

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

enter image description here

1 个答案:

答案 0 :(得分:1)

selectedWords BYTE ?为selectedWords保留一个字节。与guessWords BYTE ?相同的问题。不要像新手那样使用动态分配的内存。而是保留足够的空间:guessWords BYTE 50 DUP (?)。问号意味着MASM可以决定将其视为未初始化的内存(不存储在.exe文件中,但在程序启动时分配)。

STOSW存储一个WORD(=两个字符)。但是,Irvine的StrLength返回字符串的字节数。请改用STOSB。在STOSB之后,EDI指向最后一次存储AL之后的字符。你可以在那里存储null。如果要查看,请暂时将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