Hi,

I'm switching from gas to fasm. My first impression is fasm just creates a binary from the source which acts as a template for PE file format. Seems little odd coming from traditional assembler + linker model. Anyway, I'm trying to port all my assembly sources from gas format to fasm. But I'm not sure how to go about dividing the source into multiple files in FASM. Appreciate any pointers.

Thanks
Posted on 2010-01-18 05:15:26 by uglyhunK
uglyhunK,

"When in doubt, RTFM." ;-)
format directive selects output format (default is plain binary). FASM supports MZ and PE exe, COFF (32- and 64-bit), ELF and whatever format you invent/need (if you're crazy enough to assemble it bytewise).
Posted on 2010-01-18 07:03:31 by baldr
Well, I have read the manual and even saw the example of MS COFF. What I'm looking for is how others are doing this. Anyway, after going through some more examples, I also realized that all the fasm sources are crammed into single file ...just not to use any linker. Does not look good at all.....Wonder how MenuetOS is created using fasm (gonna look at the source now).

Posted on 2010-01-18 08:47:48 by uglyhunK
uglyhunK,

Compile to COFF and use your favorite linker to build executable (or librarian to add object file to library, or whatever).

NASM, too, by default (in the distribution version) compiles to binary. Older MASM versions (at least 6.15) scoff upon unaware user by compiling to OMF object file (if run without /coff option, lowercase).
Posted on 2010-01-18 17:03:23 by baldr
When using linker, it is quite a pain to list the all the windows API functions in the source, prefixed with extrn followed by actual exported function name. Now it is clear as to why everybody is happy to dump all the code in single file. Moreover, one needs import libraries when using a linker to produce an executable which obviously do not come with fasm. On a seperate but related point, MenuetOS kernel source (written in fasm) is a single file with 8000 lines, difficult to fathom how much time one spends moving the scroll bar up n down just to reach the point of interest in the code.
Posted on 2010-01-18 22:13:14 by uglyhunK
Have you asked this question on the FASM message board? (flatassembler.net)

FASM's "include" directive will add separate source files to your main file. Just as it does in many of the examples (although the examples only show it including .inc files, but .asm files are fine as well).
Posted on 2010-01-19 13:52:22 by r22
Yes, include directive just places all the contents in the place where the directive is used. Only problem I see is, the included source should not include anything other than instructions. If it is just a procedure or a set of instructions it should be perfectly fine.
Posted on 2010-01-19 21:09:47 by uglyhunK
uglyhunK: just assemble to coff and use the linker of your choice. You don't have to list included APIs manually - did you even look at the contents of the fasm distribution zipfile? hint: INCLUDE/WIN*.INC files :)
Posted on 2010-01-20 06:18:37 by f0dder
fodder, following is an example to make ms coff object file. This will not be assembled unless each API function is defined using extrn.


format MS COFF
include 'win32a.inc'
extrn '__imp__MessageBoxA@16' as MessageBox:dword

section '.text' code readable executable
public demo
demo:
        invoke  MessageBox, NULL, msg, title, MB_OK
        ret

section '.data' data readable writeable
        title db 'Title', 0
        msg db 'Message', 0
Posted on 2010-01-20 09:03:53 by uglyhunK
Ouch, sorry, my bad - I was pretty sure that this used to work for me, but apparently not. I suggest posting about the issue at the fasm board, the import macros ought to be fixed up so they work with both PE and COFF outputs. It would seem silly to have to maintain duplicate include files for the API lists...
Posted on 2010-01-20 11:01:53 by f0dder
uglyhunK,

Just as you use the "include" directive to include .inc files, you can use the "include" directive to include .asm files.  I usually use one "main" file which includes the directives to define the nature of the final file: .exe , .dll , coff , etc.  and in that file include the .inc files necessary and then any files which contains .asm content required.  Also in the main file are the import sections.  I also usually have a separate file for data declarations.

The advantage to this technique is that you can use the fasm's ability to create an executable directly from the "one" file.  The disadvantage is that this technique creates one big source file that the fasm assembles and builds each time.  But with the fasm's speed, this is not a problem; at least for me!  You can still use this technique to create on big .obj file or .bin file which you can then link with the linker of your choice.

Another advantage is that if the other file that you include has a procedure that is never called by your program code, the extra code is never included in the final executable.  So if you had a file that contained all of your "standard utility procs" if you include this file and only one proc is called, the final program code only contains the code for that one proc.  The fasm acts like a smart linker/librarian!

The fasm is very forgiving as to where you include the .asm source files within the main file.

Come by the http://board.flatassembler.net/ and you will get excellent advice and other discussions.

hth,

farrier
Posted on 2010-01-20 12:56:12 by farrier
Another advantage is that if the other file that you include has a procedure that is never called by your program code, the extra code is never included in the final executable.  So if you had a file that contained all of your "standard utility procs" if you include this file and only one proc is called, the final program code only contains the code for that one proc.  The fasm acts like a smart linker/librarian!
Hm, the fasm proc macro only emits code for referenced procs? I can see how that's useful for exe-output mode, but normally I'd expect WYCIWYG from an assembler :P (doesn't mean I'm against macros though - this isn't the 80'es ;)).

Come by the http://board.flatassembler.net/ and you will get excellent advice and other discussions.
Yeah, good suggestion - it's a better place to ask about FASM-specific stuff, and there's some nice guys there as well. Hope you'll hang around in both communities :)
Posted on 2010-01-20 18:16:23 by f0dder
farrier, thanks for the info.

fodder, this board is too good to leave unless I'm kicked out....(but I'm not hutch so don't see that happening  ;) )
Posted on 2010-01-20 21:20:38 by uglyhunK
Just in case someone is interested, this is the continuation thread for this. http://board.flatassembler.net/topic.php?p=108538#108538
Posted on 2010-01-20 21:55:20 by uglyhunK