flat assembler
Message board for the users of flat assembler.

Index > Main > How to combine PE sections in FASM?

Author
Thread Post new topic Reply to topic
Foamplast



Joined: 07 May 2004
Posts: 36
Location: Saratov, Russia
Foamplast 20 Jul 2004, 22:34
Hi all!

I am quite new to FASM (was using NASM before), that's why have a question:

How to combine code, data and bss PE sections in FASM?

I often write projects of 100-500 Kbytes of source code, thus dividing them into several files. For my convenience, I like to define variables in the file where they are used. When using NASM I was doing something like this:

main file:
include 'file1'
include 'file2'
...

file1:
section 'code'
...
section 'bss'

file2:
section 'code'
...
section 'bss'

and NASM was combining all code sections together, all data sections together and so on. But today I have found that my *.exe file assembled with FASM has 7 code sections, 7 data sections, 7 bss sections. I think it is a waste of space. I suppose, the solution can be achieved by using macroses. Maybe somebody already knows the cure.
Post 20 Jul 2004, 22:34
View user's profile Send private message Visit poster's website Reply with quote
VitalOne



Joined: 29 Jul 2003
Posts: 54
Location: USA
VitalOne 20 Jul 2004, 23:22
I'm not sure, but couldn't you split it up into different procedures, and have one code section, bss section, etc.. and just do something like call code_1, call code_2, etc...?
Post 20 Jul 2004, 23:22
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1150
Location: Russian Federation
comrade 21 Jul 2004, 00:43
I have had a similar concern and came up with this:

Code:
;################################
main.asm:

macro includemods [mods] {
    reverse
        include mods
} 
macro imod command,[mods] {
    forward
        command
        purge command
} 

modules fix "module01.asm","module02.asm","module03.asm"

section ".code" code readable executable
imod code,modules ; include code from all modules
section ".data" code readable writeable
imod data,modules       ; include data from all modules
;################################
module01.asm:
macro code {
      ; code for module01 here
}
macro data {
     ; data for module01 here
}
;################################
module02.asm:
macro code {
   ; code for module02 here
}
macro data {
     ; data for module02 here
}
;################################
module03.asm:
macro code {
   ; code for module03 here
}
macro data {
     ; data for module03 here
}    


Alternatively you can use COFF objects and a linker (the sane way). This is a macro hack.

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 21 Jul 2004, 00:43
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
zubi



Joined: 27 Apr 2006
Posts: 25
Location: Turkey
zubi 01 May 2006, 08:17
For a more recent discussion on the issue, check this: http://board.flatassembler.net/topic.php?t=5176
Post 01 May 2006, 08:17
View user's profile Send private message MSN Messenger Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 28 Mar 2008, 00:12
Hallo to all, here is my partial solution, about
grouping dwords vars in only a data section.
...
see the post following this, attached files
long_myvar2.zip
Hope this helps!!


Last edited by hopcode on 29 Mar 2008, 00:46; edited 4 times in total
Post 28 Mar 2008, 00:12
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: 20430
Location: In your JS exploiting you and your system
revolution 28 Mar 2008, 00:23
I think you misunderstand the "if used ..." operation. It does not detect a duplicate parameter name.

Also the line "argname equ argname#offs" is overloading the argname parameter, it is NOT reusing the same variable. The "LONG hClass" is in fact making two different variables. Check your output code to see what it generates.

Another point, with allowing redeclaration of variables (if you manage to get it working properly) it is quite easy to make a mistake by reusing a name improperly and create a headache for you when you need to debug the problem.
Post 28 Mar 2008, 00:23
View user's profile Send private message Visit poster's website Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 28 Mar 2008, 01:30
Yes,Yes,Yes
I must revisit it, because of the boundary (un)check of the
data seg too; it can overwrite other sections causing
eventually a GPF.
Thanx a lot.
..
I have it done for the creation of variables. Now the big problem is only
with the boundary check.

BTW, it is an exceptional case that someone is worryng about
someones's headache Crying or Very sad but in the case that you have not
all the problems you are speaking about, tell us the solution
of this headache. We are all awaiting... Question


Last edited by hopcode on 28 Mar 2008, 02:13; edited 2 times in total
Post 28 Mar 2008, 01:30
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: 20430
Location: In your JS exploiting you and your system
revolution 28 Mar 2008, 01:49
hopcode wrote:
Yes,Yes,Yes
I must revisit it, because of the boundary (un)check of the
data seg too; over 4k it can overwrite other sections causing
eventually a GPF.
Thanx a lot.
There are more problems with your code than just the GPF. Try to look at the output with a debugger and make sure you are getting what you want.
Post 28 Mar 2008, 01:49
View user's profile Send private message Visit poster's website Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 28 Mar 2008, 23:51
Here my definitive solution Razz
for grouping dwords that, declared
in OTHER modules, are stored
in the MAIN (and the only) DATA SEG.

Comments and suggestions appreciated.
Code:
format PE CONSOLE 4.0
entry start
include '%fasminc%\win32a.inc'

OFFS_INCR = 0
NUM_DWORDS = 0

macro LONG argname
 {
    virtual at DWORDS_DATA_START + OFFS_INCR
            argname:
        end virtual

     NUM_DWORDS = NUM_DWORDS + 1
 OFFS_INCR = OFFS_INCR + 4
}

     section '.code' code readable executable
          include "module1.asm"
             include "module2.asm"
     
    start:
          LONG myVar1
         LONG myVar2
         LONG myVar3
         
            mov     dword [myVar1],1
            mov     dword [myVar2],2
            mov     dword [myVar3],3
            
            cinvoke printf,szFormat,szMyVar,1,myVar1,[myVar1]
           cinvoke printf,szFormat,szMyVar,2,myVar2,[myVar2]
           cinvoke printf,szFormat,szMyVar,3,myVar3,[myVar3]
                   cinvoke printf,szLine
               
            ;///////CALL PROC biberkopf_eins FROM module1.asm
           call biberkopf_eins
                 cinvoke printf,szLine
               
            ;///////CALL PROC biberkopf_eins FROM module1.asm
           call biberkopf_zwei
                 cinvoke printf,szLine
               cinvoke system,szPause
      ret
 
    section '.data' data readable writeable 
          dummy dd        90909090h
           szPause         db      "pause",0
         szFormat        db      "offset %s%ld=%ld, value=%ld ",13,10,0
            szMyVar         db      "myVar",0
         szMyGVar1       db      "mod1_glob_var",0
         szMyGVar2       db      "mod2_glob_var",0
         szLine          db      09h,"----------------",13,10,0
            
            ;....other data
             ;....
               ;FROM THIS OFFSET DWORDS DATA
               ALIGN 4
             DWORDS_DATA_START:
              dd NUM_DWORDS dup (0)   

                
    section '.idata' import data readable writeable

       library msvcrt,'msvcrt.dll'
                       
            import msvcrt,\
                            system,'system',\
                                printf,"printf"
    

in the zip files
module1.asm and
module2.asm
attached.
see following post for update
Hope this helps


Last edited by hopcode on 30 Mar 2008, 01:12; edited 1 time in total
Post 28 Mar 2008, 23:51
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: 20430
Location: In your JS exploiting you and your system
revolution 29 Mar 2008, 03:02
That looks much cleaner. Thanks for sharing.
Post 29 Mar 2008, 03:02
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.