flat assembler
Message board for the users of flat assembler.

Index > Windows > Choosing FASM?

Author
Thread Post new topic Reply to topic
Calendos



Joined: 20 Jan 2021
Posts: 10
Location: France
Calendos 01 May 2024, 13:53
Hi,
I am a programmer using MASM 32 and MASM SDK since the first Microsoft Macro Assembler. I would like to be able to program on the 64-bit Window platform and abandon MASM. I use the GUI interface a lot and I would like to know if the flat assembler allows you to program resources. Is it a separate resource compiler such as LINK or GoRC or integrated with FASM?. This, to program the API: CreateWindow(Ex), DialogBoxParam, DialogBoxIndirectParam, CreateDialogParam and CreateDialogIndirectParam?
Thanks in advance.
Robert

PS: is the invoke directive useable in 64_bit, despite the FASTCALL ?
Post 01 May 2024, 13:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20629
Location: In your JS exploiting you and your system
revolution 01 May 2024, 16:00
Resources and invoke are not part of the fasm core. They are not directives. But you can find macros in the download to add support for them.

You can either manually encode resources and import etc. into your output exe, or use a linker the same way MASM does, or use macros to make do the manual entries for you.

If you open fasmw.asm in the IDE download package, you can see this:
Code:
;...

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          advapi,'ADVAPI32.DLL',\
          user,'USER32.DLL',\
          gdi,'GDI32.DLL',\
          comctl,'COMCTL32.DLL',\
          comdlg,'COMDLG32.DLL',\
          shell,'SHELL32.DLL'

  import kernel,\
         GetModuleHandle,'GetModuleHandleA',\
         LoadLibrary,'LoadLibraryA',\
         GetProcAddress,'GetProcAddress',\
         GetCommandLine,'GetCommandLineA',\

;...

section '.rsrc' resource data readable

  directory RT_MENU,menus,\
            RT_ACCELERATOR,accelerators,\
            RT_DIALOG,dialogs,\
            RT_GROUP_ICON,group_icons,\
            RT_ICON,icons,\
            RT_BITMAP,bitmaps,\
            RT_VERSION,versions

  resource menus,\
           IDM_MAIN,LANG_ENGLISH+SUBLANG_DEFAULT,main_menu,\
           IDM_TAB,LANG_ENGLISH+SUBLANG_DEFAULT,popup_menu

  resource accelerators,\
           IDA_MAIN,LANG_ENGLISH+SUBLANG_DEFAULT,main_keys

  resource dialogs,\
           IDD_POSITION,LANG_ENGLISH+SUBLANG_DEFAULT,position_dialog,\
           IDD_FIND,LANG_ENGLISH+SUBLANG_DEFAULT,find_dialog,\

;...    
Post 01 May 2024, 16:00
View user's profile Send private message Visit poster's website Reply with quote
Calendos



Joined: 20 Jan 2021
Posts: 10
Location: France
Calendos 01 May 2024, 19:10
Thank you for your prompt response. I need to think a bit to understand what you wrote about resources and directives for FASM. MASM looks very different from this point of view.
I would explain my project: until now I used Power Basic, an excellent 32-bit BASIC providing an inline assembler. However Power Basic no longer evolves and I decided to turn to Pure Basic 64 bits. Pure Basic uses FASM for x86-x64 assembly.
So I decided to take an interest in FASM with the aim of also using it as a standalone assembler. Hence my questions.
Thank you again for your help
Post 01 May 2024, 19:10
View user's profile Send private message Reply with quote
Calendos



Joined: 20 Jan 2021
Posts: 10
Location: France
Calendos 02 May 2024, 14:21
I have not completely understood the articulation between resources and the main program in FASM because I am undoubtedly a prisoner of my long practice of MASM.
To clarify things, here is a classic program developed under MASM which uses the DialogBoxParam API function and a resource file to facilitate programming of the GUI interface and its menu. These scripts were developed under MASM 6.1 (32-bit) with the help of the Microsoft RC.exe resource compiler. and the linker LINK.exe. This is one of famous Iczelion’s tutorials.
Header files and most macros belong to the MASM32 SDK.

Here are some display issued by this program:
Image

It is from this example that I would like to know how FASM deals with this type of program. Could you help me?

Main program script (MASM syntax)
Code:
include \masm32\include\masm32rt.inc
DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD
.data
DlgName db "MyDialog",0
AppName db "Our Second Dialog Box",0
TestString      db "Wow! I'm in an edit box now",0
.data?
hInstance       HINSTANCE ?
CommandLine     LPSTR ?
buffer  db 512 dup(?)
.const
IDC_EDIT        equ 3000
IDC_BUTTON      equ 3001
IDC_EXIT        equ 3002
IDM_GETTEXT     equ 32000
IDM_CLEAR       equ 32001
IDM_EXIT        equ 32002
.code
start:
        mov    hInstance,rv(GetModuleHandle, NULL)
        invoke DialogBoxParam, hInstance, ADDR DlgName,NULL, addr DlgProc, NULL
        invoke ExitProcess,eax

DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
        .IF uMsg==WM_INITDIALOG
                invoke GetDlgItem, hWnd,IDC_EDIT
                invoke SetFocus,eax
        .ELSEIF uMsg==WM_CLOSE
                invoke SendMessage,hWnd,WM_COMMAND,IDM_EXIT,0
        .ELSEIF uMsg==WM_COMMAND
                mov  eax,wParam
                ;--- Menu items handling
                .IF lParam==0
                        .IF ax==IDM_GETTEXT
                                invoke GetDlgItemText,hWnd,IDC_EDIT,ADDR buffer,512
                                invoke MessageBox,NULL,ADDR buffer,ADDR AppName,MB_OK
                        .ELSEIF ax==IDM_CLEAR
                                invoke SetDlgItemText,hWnd,IDC_EDIT,NULL
                        .ELSEIF ax==IDM_EXIT
                                invoke EndDialog, hWnd,NULL
                        .ENDIF
                .ELSE
                ;--- Controls handling
                                mov edx,wParam
                                shr edx,16
                        .IF dx==BN_CLICKED
                                .IF ax==IDC_BUTTON
                                        invoke SetDlgItemText,hWnd,IDC_EDIT,ADDR TestString
                                .ELSEIF ax==IDC_EXIT
                                        invoke SendMessage,hWnd,WM_COMMAND,IDM_EXIT,0
                                .ENDIF
                        .ENDIF
                .ENDIF
        .ELSE
                mov  eax,FALSE
                ret
        .ENDIF
                mov  eax,TRUE
        ret
DlgProc endp
end start
    


Resources script to compile by RC.exe(MASM Syntax)

Code:
#include "resource.h"
#define IDC_EDIT        3000
#define IDC_BUTTON      3001
#define IDC_EXIT        3002
#define IDR_MENU1       3003
#define IDM_GETTEXT     32000
#define IDM_CLEAR       32001
#define IDM_EXIT        32003

MyDialog DIALOG 10, 10, 205, 60
STYLE 0x0004 | DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "Our Second Dialog Box"
MENU IDR_MENU1
BEGIN
        EDITTEXT         IDC_EDIT,   15,17,111,13, ES_AUTOHSCROLL | ES_LEFT
        DEFPUSHBUTTON   "Say Hello", IDC_BUTTON,    141,10,52,13
        PUSHBUTTON      "E&xit", IDC_EXIT,  141,26,52,13
END

IDR_MENU1  MENU
BEGIN
        POPUP "Test Controls"
        BEGIN
                MENUITEM "Get Text", IDM_GETTEXT
                MENUITEM "Clear Text", IDM_CLEAR
                MENUITEM "", , 0x0800 /*MFT_SEPARATOR*/
                MENUITEM "E&xit", IDM_EXIT
        END
END
    
Post 02 May 2024, 14:21
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 02 May 2024, 19:20
There are a number of problems with the MASM syntax. The least of which is the hiding of indirect access verses immediate/address. It is a great disservice to anyone trying to learn x86, imho.

Disclaimers aside ...

The RC file can be compiled with any number of resources editors to produce a RES file.
https://github.com/katahiromz/RisohEditor

... or the RC script can be adapted to fasm's macro format.

The code needs more changes [fasm2]:
Code:
include 'win64ax.inc'

.data?
hInstance dq ? ;HINSTANCE ?
CommandLine dq ? ;LPSTR ?
buffer db 512 dup(?)

; assemble-time constants
IDC_EDIT        equ 3000
IDC_BUTTON      equ 3001
IDC_EXIT        equ 3002
IDM_GETTEXT     equ 32000
IDM_CLEAR       equ 32001
IDM_EXIT        equ 32002

.code

start:
        invoke GetModuleHandle,NULL
        mov    [hInstance],rax
        invoke DialogBoxParam, [hInstance],"MyDialog",NULL,addr DlgProc, NULL
        invoke ExitProcess,eax

proc DlgProc, hWnd,uMsg,wParam,lParam
        mov [hWnd], rcx
        mov [uMsg], rdx
        mov [wParam], r8
        mov [lParam], r9

        .IF [uMsg] = WM_INITDIALOG
                invoke GetDlgItem,[hWnd],IDC_EDIT
                invoke SetFocus,eax
        .ELSEIF [uMsg] = WM_CLOSE
                invoke SendMessage,[hWnd],WM_COMMAND,IDM_EXIT,0
        .ELSEIF [uMsg] = WM_COMMAND
                mov eax, dword [wParam]
                ;--- Menu items handling
                .IF [lParam] = 0
                        .IF ax = IDM_GETTEXT
                                invoke GetDlgItemText,[hWnd],IDC_EDIT,ADDR buffer,512
                                invoke MessageBox,NULL,ADDR buffer,"Our Second Dialog Box",MB_OK
                        .ELSEIF ax = IDM_CLEAR
                                invoke SetDlgItemText,[hWnd],IDC_EDIT,NULL
                        .ELSEIF ax = IDM_EXIT
                                invoke EndDialog,[hWnd],NULL
                        .ENDIF
                .ELSE
                ;--- Controls handling
                                mov edx, dword [wParam]
                                shr edx, 16
                        .IF dx = BN_CLICKED
                                .IF ax = IDC_BUTTON
                                        invoke SetDlgItemText,[hWnd],IDC_EDIT,\
                                                "Wow! I'm in an edit box now"
                                .ELSEIF ax = IDC_EXIT
                                        invoke SendMessage,[hWnd],WM_COMMAND,IDM_EXIT,0
                                .ENDIF
                        .ENDIF
                .ENDIF
        .ELSE
                mov  eax,FALSE
                ret
        .ENDIF
                mov  eax,TRUE
        ret
endp

.end start

section '.rsrc' data readable resource from 'main.res'    
Although, I've tested this modification and it seems to work fine; I personally do not use fasm[g] in this manner.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 02 May 2024, 19:20
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 02 May 2024, 19:57
Here is another example, showing fasm resource expectations:
https://github.com/bitRAKE/fasmg_playground/blob/master/win64/langplay/tokens.asm

Note the use of LOCALS/ENDL for the PROC/ENDP; and the need to save the fastcall registers if they are needed. The macros also support saving volatile registers (USES parm):
Code:
proc MainDlgProc uses rbx rsi, hDlg,uMsg,wP,lP
locals
        hCtl    rq 1
endl
        mov [hDlg],rcx
        mov [uMsg],rdx
        mov [wP],r8
        mov [lP],r9

        ret
endp    
... searching the board for LOCALS ENDL will get some useful hits as well.
Post 02 May 2024, 19:57
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 03 May 2024, 06:07
BTW, two different values are being used for IDM_EXIT - 32002/3. That is why the exit menu doesn't work.

By using fasm macros the resource constants can be defined in one place. Another solution is to write a macro that processes the C-language #defines.

With fasm2 "#define NAME VALUE" is valid syntax. So, it's sufficient to just split the defines into a separate file:
Code:
#define IDC_EDIT        3000
#define IDC_BUTTON      3001
#define IDC_EXIT        3002
#define IDR_MENU1       3003
#define IDM_GETTEXT     32000
#define IDM_CLEAR       32001
#define IDM_EXIT        32003    
... and include this in both the ASM and RC files.


Description:
Download
Filename: main-fasm2.zip
Filesize: 2.1 KB
Downloaded: 165 Time(s)


_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 03 May 2024, 06:07
View user's profile Send private message Visit poster's website Reply with quote
Calendos



Joined: 20 Jan 2021
Posts: 10
Location: France
Calendos 04 May 2024, 06:08
Thank you, bitRAKE, for your tremendous work in helping me understand. Regarding the naming of variables under MASM, I know there has been much debate about whether (or not) variables should be enclosed in square brackets. The use of these brackets responds, in fact, to some logic. However, in my opinion, the use of these characters is not practical. Each variable requires you to type 2 extra characters, but that's not the most important. On a QWERTY keyboard, you have direct access to the square brackets with a single key, while on an AZERTY keyboard (in France, therefore), you must also press the Alt GR key. It's quite painful in the long run. That said, Microsoft easily accommodates its choice and it does not seem being a problem.

Regarding the source script constants, you noted a critical error due to a poorly defined constant and I thank you for that.

The fact remains that I haven't fully understood how to compile a program with a main script and a resource script with FASM. However, the syntax differences between FASM and MASM are small if we ignore the characterization of the variables.

Okay, I'm going to look again at all the material you sent me and then I'll get back to you if you want.

thanks again
Robert
Post 04 May 2024, 06:08
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 04 May 2024, 06:59
Calendos wrote:
The fact remains that I haven't fully understood how to compile a program with a main script and a resource script with FASM.
If you have a MS build tools prompt and fasm2 is in the path then it's just:
Code:
rc.exe main.rc
fasm2 main.asm    
Alternately, if you are building the RES file with another visual tool and programming with the fasmgw editor then just select compile/run from the menu, after compiling the resource file.

I like NMAKE tool from microsoft because it's terse. I've included a makefile for that - just "nmake" from the dev prompt keeps everything up to date.

There are many knowledgeable people here that can provide perspectives regardless of the challenges.

p.s. Have you seen asmc?
(Very impressive active project, imho.)

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 04 May 2024, 06:59
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20629
Location: In your JS exploiting you and your system
revolution 04 May 2024, 09:59
Let's not get crossed wires here:

bitRAKE appears to be discussing and posting fasm2/fasmg based code.

The OP is asking about fasm code.

fasm != fasm2, they are very different, and not compatible with each other.

For example:
Code:
~ echo 'nop' > test.asm

~ fasmg test.asm
flat assembler  version g.igd4j
test.asm [1]:
        nop
Processed: nop
Error: illegal instruction.

~ fasm test.asm
flat assembler  version 1.73.31  (16384 kilobytes memory)
1 passes, 1 bytes.

~     
Post 04 May 2024, 09:59
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 04 May 2024, 10:26
revolution wrote:
fasm != fasm2, they are very different, and not compatible with each other.
OP is familiar with MASM. So, when we talk about compatibility, perhaps we should approach from a much higher level of abstraction. Your test is rather disingenuous of fasm2's support.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 04 May 2024, 10:26
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 04 May 2024, 18:13
Just to drive the point home, revolution, here is the fasm version of the code I posted. It is more fragile and less flexible/MASM compatible.


Description: Minor changes for fasm use.
Download
Filename: main-fasm.asm
Filesize: 2.25 KB
Downloaded: 205 Time(s)


_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 04 May 2024, 18:13
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20629
Location: In your JS exploiting you and your system
revolution 05 May 2024, 07:40
bitRAKE wrote:
Your test is rather disingenuous of fasm2's support.
My test was to address the fact that it is different. I made no mention of support.
bitRAKE wrote:
Just to drive the point home, revolution, here is the fasm version of the code I posted. It is more fragile and less flexible/MASM compatible.
I wasn't commenting on fragility or flexibility. I was pointing out that it is different. I think that care needs to be taken to ensure the reader understands that v2 isn't simply the next version, but is in fact an entirely different paradigm.
Post 05 May 2024, 07:40
View user's profile Send private message Visit poster's website 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.