flat assembler
Message board for the users of flat assembler.

Index > Windows > error: else import32.inc with find files issue FASM 1.73.32

Author
Thread Post new topic Reply to topic
catafest



Joined: 05 Aug 2010
Posts: 130
catafest 04 Mar 2025, 13:45
I try to find files with fasm version 1.73.32 , the source code I used:

Code:
format PE GUI 4.0
entry start

include '\INCLUDE\win32a.inc'

section '.data' data readable writeable
    class_name db 'FileListClass',0
    window_name db 'Files on D: Drive',0
    file_list rb 1024*1024
    file_name rb 260
    find_data WIN32_FIND_DATA <0>
    hwnd dd ?
    msg MSG <0>
    wc WNDCLASS <0>
    ps PAINTSTRUCT <0>
    rect RECT <0>

section '.code' code readable executable
start:
    invoke GetModuleHandle,0
    mov [wc.hInstance],eax
    mov [wc.hIcon],eax
    mov [wc.hCursor],eax
    mov [wc.hbrBackground],COLOR_BTNFACE+1
    mov [wc.lpszClassName],class_name
    mov [wc.lpfnWndProc],WndProc
    invoke RegisterClass,wc
    invoke CreateWindowEx,0,class_name,window_name,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,0,0,[wc.hInstance],0
    mov [hwnd],eax
    invoke ShowWindow,[hwnd],SW_SHOW
    invoke UpdateWindow,[hwnd]

message_loop:
    invoke GetMessage,msg,0,0,0
    test eax,eax
    jz exit_program
    invoke TranslateMessage,msg
    invoke DispatchMessage,msg
    jmp message_loop

exit_program:
    invoke ExitProcess,[msg.wParam]

proc WndProc hWnd, uMsg, wParam, lParam
    cmp [uMsg],WM_PAINT
    je .wm_paint
    cmp [uMsg],WM_DESTROY
    je .wm_destroy
    invoke DefWindowProc,[hWnd],[uMsg],[wParam],[lParam]
    ret

.wm_paint:
    invoke BeginPaint,[hWnd],ps
    mov [hDC],eax
    invoke GetClientRect,[hWnd],rect
    invoke GetFileList,'D:\\',file_list
    invoke SetTextColor,[hDC],COLOR_WINDOWTEXT
    invoke SetBkColor,[hDC],COLOR_WINDOW
    invoke DrawText,[hDC],file_list,-1,rect,DT_LEFT+DT_TOP+DT_WORDBREAK
    invoke EndPaint,[hWnd],ps
    ret

.wm_destroy:
    invoke PostQuitMessage,0
    ret
endp

proc GetFileList, directory, buffer
    local hFind:HANDLE
    local file_name[260]:byte
    local search_pattern[260]:byte
    local file_list_ptr:DWORD
    local file_size:DWORD

    invoke lstrcpy,search_pattern,[directory]
    invoke lstrcat,search_pattern,'*'
    invoke FindFirstFile,search_pattern,find_data
    mov [hFind],eax
    cmp eax,INVALID_HANDLE_VALUE
    je .end

.next_file:
    invoke lstrcpy,file_name,find_data.cFileName
    invoke lstrcat,[buffer],file_name
    invoke lstrcat,[buffer],'\n'
    invoke FindNextFile,[hFind],find_data
    test eax,eax
    jnz .next_file

.end:
    invoke FindClose,[hFind]
    ret
endp

section '.idata' import data readable writeable
    library kernel32,'KERNEL32.DLL',\
            user32,'USER32.DLL',\
            gdi32,'GDI32.DLL'
    import kernel32,\
           GetModuleHandle,'GetModuleHandleA',\
           ExitProcess,'ExitProcess',\
           FindFirstFile,'FindFirstFileA',\
           FindNextFile,'FindNextFileA',\
           FindClose,'FindClose'
    import user32,\
           RegisterClass,'RegisterClassA',\
           CreateWindowEx,'CreateWindowExA',\
           ShowWindow,'ShowWindow',\
           UpdateWindow,'UpdateWindow',\
           GetMessage,'GetMessageA',\
           TranslateMessage,'TranslateMessage',\
           DispatchMessage,'DispatchMessageA',\
           DefWindowProc,'DefWindowProcA',\
           PostQuitMessage,'PostQuitMessage',\
           BeginPaint,'BeginPaint',\
           EndPaint,'EndPaint',\
           GetClientRect,'GetClientRect',\
           DrawText,'DrawTextA'
    import gdi32,\
           SetTextColor,'SetTextColor',\
           SetBkColor,'SetBkColor's    


The error comes with : Unexpected instruction 'else' at line 99.
Any idea why this not work ?
See this :

Code:
import kernel32,\
           GetModuleHandle,'GetModuleHandleA',\
           ExitProcess,'ExitProcess',\
           FindFirstFile,'FindFirstFileA',\
           FindNextFile,'FindNextFileA',\
           FindClose,'FindClose' 
    

... into import32.inc is this **else** at:

Code:
    macro import name,[label,string]
     { common
        rb (- rva $) and 3
        if defined name#.referred
         name#.lookup:
       forward
         if used label
          if string eqtype ''
           local _label
           dd RVA _label
          else  ...
    enter code here    


Description:
Download
Filename: files_D.ASM
Filesize: 3.38 KB
Downloaded: 27 Time(s)

Post 04 Mar 2025, 13:45
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1694
Location: Toronto, Canada
AsmGuru62 04 Mar 2025, 17:51
There are few issues with this function:
Code:
proc GetFileList, directory, buffer
    local hFind:DWORD
    local file_name[260]:BYTE
    local search_pattern[260]:BYTE
    local file_list_ptr:DWORD
    local file_size:DWORD

    invoke lstrcpy,search_pattern,[directory]
    invoke lstrcat,search_pattern,'*'
    invoke FindFirstFile,search_pattern,find_data
    mov [hFind],eax
    cmp eax,INVALID_HANDLE_VALUE
    je .end

.next_file:
    invoke lstrcpy,file_name,find_data.cFileName
    invoke lstrcat,[buffer],file_name
    invoke lstrcat,[buffer],'\n'
    invoke FindNextFile,[hFind],find_data
    test eax,eax
    jnz .next_file

.end:
    invoke FindClose,[hFind]
    ret
endp
    

1. directory is a reserved keyword (or macro) in FASM, so it cannot be used as parameter.
2. byte should be BYTE. This is how it defined by 'local' macro.
3. Same for HANDLE -- use DWORD instead.
4. file_name,search_pattern -- their address is taken with LEA opcode, cannot use name as with global variable.
5. Cannot use invoke with GetFileList -- must use stdcall.

There are more errors once these are fixed.
Some variable is missing a declaration.
Post 04 Mar 2025, 17:51
View user's profile Send private message Send e-mail Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1068
Location: Russia
macomics 04 Mar 2025, 18:04
Code:
struct FILE_PATH_BUFFER
  buf db 260 dup (?)
ends

proc GetFileList aDirectory, aBuffer ; <-- Extra comma
    local hFind:DWORD
    local file_name:FILE_PATH_BUFFER ; Incorrect declaration of local variables
    local search_pattern:FILE_PATH_BUFFER ; Incorrect declaration of local variables    

To understand that the error is in another place, simply move the import section to the beginning.
Code:
format PE GUI 4.0
entry start

include '\INCLUDE\win32a.inc'

section '.idata' import data readable writeable
    library kernel32,'KERNEL32.DLL',\
            user32,'USER32.DLL',\
            gdi32,'GDI32.DLL'
    import kernel32,\
           GetModuleHandle,'GetModuleHandleA',\
           ExitProcess,'ExitProcess',\
           FindFirstFile,'FindFirstFileA',\
           FindNextFile,'FindNextFileA',\
           FindClose,'FindClose'
    import user32,\
           RegisterClass,'RegisterClassA',\
           CreateWindowEx,'CreateWindowExA',\
           ShowWindow,'ShowWindow',\
           UpdateWindow,'UpdateWindow',\
           GetMessage,'GetMessageA',\
           TranslateMessage,'TranslateMessage',\
           DispatchMessage,'DispatchMessageA',\
           DefWindowProc,'DefWindowProcA',\
           PostQuitMessage,'PostQuitMessage',\
           BeginPaint,'BeginPaint',\
           EndPaint,'EndPaint',\
           GetClientRect,'GetClientRect',\
           DrawText,'DrawTextA'
    import gdi32,\
           SetTextColor,'SetTextColor',\
           SetBkColor,'SetBkColor'

section '.data' data readable writeable
    class_name db 'FileListClass',0
    window_name db 'Files on D: Drive',0
    file_list rb 1024*1024
    file_name rb 260
    find_data WIN32_FIND_DATA <0>
    hwnd dd ?
    msg MSG <0>
    wc WNDCLASS <0>
    ps PAINTSTRUCT <0>
    rect RECT <0>

section '.code' code readable executable
start:
    invoke GetModuleHandle,0
    mov [wc.hInstance],eax
    mov [wc.hIcon],eax
    mov [wc.hCursor],eax
    mov [wc.hbrBackground],COLOR_BTNFACE+1
    mov [wc.lpszClassName],class_name
    mov [wc.lpfnWndProc],WndProc
    invoke RegisterClass,wc
    invoke CreateWindowEx,0,class_name,window_name,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,0,0,[wc.hInstance],0
    mov [hwnd],eax
    invoke ShowWindow,[hwnd],SW_SHOW
    invoke UpdateWindow,[hwnd]

message_loop:
    invoke GetMessage,msg,0,0,0
    test eax,eax
    jz exit_program
    invoke TranslateMessage,msg
    invoke DispatchMessage,msg
    jmp message_loop

exit_program:
    invoke ExitProcess,[msg.wParam]

proc WndProc hWnd, uMsg, wParam, lParam
    cmp [uMsg],WM_PAINT
    je .wm_paint
    cmp [uMsg],WM_DESTROY
    je .wm_destroy
    invoke DefWindowProc,[hWnd],[uMsg],[wParam],[lParam]
    ret

.wm_paint:
    invoke BeginPaint,[hWnd],ps
    mov [hDC],eax
    invoke GetClientRect,[hWnd],rect
    invoke GetFileList,'D:\\',file_list
    invoke SetTextColor,[hDC],COLOR_WINDOWTEXT
    invoke SetBkColor,[hDC],COLOR_WINDOW
    invoke DrawText,[hDC],file_list,-1,rect,DT_LEFT+DT_TOP+DT_WORDBREAK
    invoke EndPaint,[hWnd],ps
    ret

.wm_destroy:
    invoke PostQuitMessage,0
    ret
endp

proc GetFileList, aDirectory, aBuffer
    local hFind:DWORD
    local file_name[260]:BYTE
    local search_pattern[260]:BYTE
    local file_list_ptr:DWORD
    local file_size:DWORD

    invoke lstrcpy,addr [search_pattern],[aDirectory]
    invoke lstrcat,addr [search_pattern],'*'
    invoke FindFirstFile,addr [search_pattern],find_data
    mov [hFind],eax
    cmp eax,INVALID_HANDLE_VALUE
    je .end

.next_file:
    invoke lstrcpy,addr [file_name],find_data.cFileName
    invoke lstrcat,[aBuffer],addr [file_name]
    invoke lstrcat,[aBuffer],'\n'
    invoke FindNextFile,[hFind],find_data
    test eax,eax
    jnz .next_file

.end:
    invoke FindClose,[hFind]
    ret
endp    


AsmGuru62, you got ahead of me.
Post 04 Mar 2025, 18:04
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1694
Location: Toronto, Canada
AsmGuru62 04 Mar 2025, 18:47
macomics, it must be my IDE (I wrote it around 2009) -- just copy/paste code and compile it.
Post 04 Mar 2025, 18:47
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.