Search code examples
arraysstringassemblymasmirvine32

Making string of --- with 0 on the end


I can not use this declaration, because selectedWords can be any string.

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

So I try to do this:

;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

And I have write this function

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  

But after this funcion I get guessWords what is string of ------- and dont have 0 on the and. So how to make string guessWords=-------0?

Its important for me to have 0 on the end of string because of some other comparation in code..

enter image description here


Solution

  • selectedWords BYTE ? reserves just one byte for selectedWords. The same issue with guessWords BYTE ?. Don't play with dynamically allocated memory as newbie. Rather reserve space which is sufficient in any case: guessWords BYTE 50 DUP (?). The question mark means that MASM can decide to treat it as uninitialized memory (not stored in the .exe file, but allocated at program start).

    STOSW stores a WORD (= two characters). However Irvine's StrLength returns the number of bytes of the string. Use STOSB instead. After STOSB, EDI points to the character after the last stored AL. You can store a null there. If you want to see it, temporarily change 0 to '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