flat assembler
Message board for the users of flat assembler.

flat assembler > Compiler Internals > fasm as DLL

Goto page Previous  1, 2, 3, 4
Author
Thread Post new topic Reply to topic
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6981
Location: Kraków, Poland
sesey wrote:
How can I compile this source code as x64.dll?
This is a bit complex, it requires the same macros as Linux x64 version and rewrite of OS abstraction layer, I may take a look at this in the future.
Post 21 Aug 2018, 20:15
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6981
Location: Kraków, Poland
ProMiNick wrote:
But internal variable __FILE__ without value can produce bugs in virtual blocks (when they are as additional files).
Do not worry, the engine has it all figured out, you just need to be careful to use its functions properly (I did my best to well-document their interfaces).

As a quick demonstration I made a simplest DLL version of fasmg. It just exposes the main interfaces of engine (and uses hard-coded values for most of the settings). It operates on local variables, so should be thread-safe.

The attached files have to be in the source/windows/dll directory in fasmg source tree to assemble correctly. It can only be self-hosted with fasmg. Note that selfhost.inc has additional macros to convert MOV to LEA when EBP-relative addresses need to be put into a register.

The main function is:
fasmg_Assemble(source_string, source_path, reserved, output_path, stdout, stderr)
All arguments are optional, every one of them is allowed to be NULL.
  • source_string is the text to be assembled directly from memory. Normal versions of fasmg use this for the instructions added from command line with -I switch, but this can just as well be used to assemble complete source not stored in any file.
  • source_path is the path to source file. If both in-memory source and source file are provided, their concatenation is assembled.
  • reserved for a parameter that might be used to receive output in memory. Currently not implemented.
  • output_path is the path to file for the output. When not provided, the output path is generated from the input path (just like the command-line fasmg does it). When there is neither path provided, output is not written anywhere.
  • stdout should be a handle to file or pipe that receives the messages generated with DISPLAY command.
  • stderr should be a handle to file or pipe that receives the error messages.

Function returns the number of errors encountered in the assembly, so 0 indicates success. When there were errors, details have been written into stderr (if provided).
Negative values returned indicate fatal errors: -1 when assembler ran out of memory, -2 when maximum number of passes has been reached without a resolved assembly, -3 when writing output file has failed for some reason.

I kept this version as simple as possible, mainly to demonstrate how the bare engine can be used. If you wanted a pure memory-to-memory assembly, you'd need to add some special handling. You'd have to allocate a big enough memory block (get_output_length function can give the required size) and then perhaps use read_from_output to copy data.

EDIT: Attachment removed - see below.


Last edited by Tomasz Grysztar on 13 Sep 2018, 07:57; edited 3 times in total
Post 21 Aug 2018, 20:51
View user's profile Send private message Visit poster's website Reply with quote
sesey



Joined: 21 Aug 2018
Posts: 3
Tomasz Grysztar wrote:
sesey wrote:
How can I compile this source code as x64.dll?
This is a bit complex, it requires the same macros as Linux x64 version and rewrite of OS abstraction layer, I may take a look at this in the future.


I thank you for your help. I will wait for your future work.
Post 22 Aug 2018, 09:17
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6981
Location: Kraków, Poland
I have added a simple modification to the above FASMG.DLL to allow a pure memory-to-memory assembly.

The reserved argument became output_region which should either be a NULL (when no in-memory output is needed) or a pointer to this simple structure:
Code:
struct MEMORY_REGION
        address dd ?
        size dd ?
ends    
It should be initially zeroed or may be filled with information about a block of memory pre-allocated with VirtualAlloc. When the assembly is successful, the structure is filled with information about the size of generated output and the address where it is stored. If the structure provided a memory region but it was too small to hold the output, a region is re-allocated with VirtualAlloc, releasing the previous one with VirtualFree. This way you can keep calling fasmg_Assemble and have it re-use the same region of memory for output. You can also initialize the address field to point to a region prepared with VirtualAlloc with MEM_RESERVE flag and set the initial size to zero, fasmg_Assemble should then be able to keep using the reserved address, committing as much memory as needed for the generated output.

For pure memory-to-memory assembly both source_path and output_path should be NULL.

EDIT: Attachment removed - see below.


Last edited by Tomasz Grysztar on 13 Sep 2018, 07:57; edited 1 time in total
Post 23 Aug 2018, 17:25
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6981
Location: Kraków, Poland
I have now included the sources of this simple DLL in the main repository of fasmg: https://flatassembler.net/fossil/repo/fasmg/info/3d9126369323f2c7

EDIT: the sources are not also included in the basic package. The added files are small and I like them enough to show them in the official distribution as a demonstration how fasmg core can be used in multi-threaded environment.
Post 31 Aug 2018, 15:30
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6981
Location: Kraków, Poland
Tomasz Grysztar wrote:
sesey wrote:
How can I compile this source code as x64.dll?
This is a bit complex, it requires the same macros as Linux x64 version and rewrite of OS abstraction layer, I may take a look at this in the future.
I have made an experimental FASMX64.DLL that uses the same macros as Linux x64 version. It seems to work, but keep in mind that the limitation of this hack is that it is only going to work when all the memory blocks fit in the low 4G addressing space. When you allocate memory for fasm you need to make sure that allocation is in the low memory, also when you provide source text in memory, you need to ensure that is fits in low 4G.

At the same time I also added a mutex in FASM.DLL. I think I have been making a bad example by not using something like that when the core is obviously and dangerously not thread-safe.

I believe it might also be possible to make FASMX64.DLL not require mutex and just use a per-thread memory block for all variables by addressing it with a base register like R14 (similarly to how FASMG.DLL addresses everything relative to EBP). However this would require some careful setup, to allocate locals block in low 4G space for every function call. For now I used a mutex there, too.
Post 13 Sep 2018, 09:34
View user's profile Send private message Visit poster's website Reply with quote
bazizmix



Joined: 15 Jan 2016
Posts: 39
So. I made a 64-bit fasm. Exe and dll. Tested only for PE and PE64.
It can be compiled as 32-bit from 64-bit and versa.


Description: Source and example
Download
Filename: fasmdll64.7z
Filesize: 230.33 KB
Downloaded: 24 Time(s)

Post 02 Nov 2018, 14:45
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3, 4

< 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-2018, Tomasz Grysztar.

Powered by rwasa.