The MASM Forum

General => The Campus => Topic started by: MCpiroman on September 30, 2017, 10:04:53 PM

Title: Printing out array
Post by: MCpiroman on September 30, 2017, 10:04:53 PM
Given code:

    .486                                   
    .model flat, stdcall                   
    option casemap :none                   

    include \masm32\include\windows.inc     
    include \masm32\macros\macros.asm       
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
PrintBoard PROTO :DWORD

.data

initBoard  dw 0,0,0, 7,0,0, 0,0,0
            dw 1,0,0, 0,0,0, 0,0,0     
            dw 0,0,0, 4,3,0, 2,0,0             
            dw 0,0,0, 0,0,0, 0,0,6     
            dw 0,0,0, 5,0,9, 0,0,0     
            dw 0,0,0, 0,0,0, 4,1,8
            dw 0,0,0, 0,8,1, 0,0,0
            dw 0,0,2, 0,0,0, 0,5,0
            dw 0,4,0, 0,0,0, 3,0,0
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .code 
                     
start:
    call FindSolutions
    exit

FindSolutions proc

    invoke PrintBoard, initBoard
   
    ret                         
FindSolutions endp

PrintBoard proc iBoard:DWORD

    push ebx
    push ecx

    lea ebx, iBoard
    xor edi, edi
    @@:
        mov edi, [ebx+ecx]
        print str$(edi)
        inc ecx
        cmp ecx, 80
        jne @B

    pop ecx
    pop ebx
   
PrintBoard endp

end start


prints the first number (0) and then crashes. Although if I remove 'print' segment it works fine (at least doesn't crash). Also, when in PrintBoard I add
LOCAL someVar :DWORD
(so it looks:)

..some code

    PrintBoard proc iBoard:DWORD
    LOCAL someVar :DWORD
    push ebx

..some code

it crashes too, even without print instruction.
Any ideas, how to fix this?
Title: Re: Printing out array
Post by: jj2007 on September 30, 2017, 10:37:03 PM
Maybe a ret before the endp would help?
Title: Re: Printing out array
Post by: aw27 on September 30, 2017, 10:49:19 PM
There are many errors in your program other than the absence of ret.
ecx becomes corrupted in the loop, you pass a parameter by value and expect to get the original address from it on the called function, initBoard is an array of words but you want to print an array of bytes. Well, may be you should scrap everything and start over.   ::)
Title: Re: Printing out array
Post by: mabdelouahab on October 01, 2017, 12:57:07 AM
Quote
FindSolutions proc
    invoke PrintBoard,addr initBoard
    ret                         
FindSolutions endp

PrintBoard proc iBoard:DWORD
    push esi
    push edi   
    mov esi, iBoard
    xor edi, edi
    @@:
        movsx ecx,word ptr  [esi+edi]
        print str$(ecx)
        add edi,2
        cmp edi, 80*2
        jne @B

    pop edi
    pop esi
    ret
PrintBoard endp