flat assembler
Message board for the users of flat assembler.

flat assembler > Main > Section naming question

Author
Thread Post new topic Reply to topic
SokilOff



Joined: 20 Sep 2010
Posts: 15
Hello all.

I started porting one of my projects from tasm to fasm and have a question about %subj%

In tasm sources I had main .asm and few .inc files, each of them had .code and .data sections. It looked like this:

main.asm

include code1.inc

.code
<some code from main.asm>
.data
<some data from main.asm>


code1.inc

.code
<some code from code1.inc>
.data
<some data from code1.inc>

etc.

And at the end compiled exe had the following structure:

.code
<some code from code1.inc>
...
<some code from main.asm>

.data
<some data from code1.inc>
...
<some data from main.asm>

All code parts from .code sections were merged into one code segment during compiling/linking, all data sections were merged together as well. When I do the same in fasm, I get obviously incorrect exe with few .code and few .data sections.

So here's the question: is there a way to tell fasm just to merge sections with the same names from different files ?
Post 30 Jun 2013, 19:24
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2796
Location: dank orb
Short answer is, No.

Long answer is that these sections can be wrapped in a macro, which the main source file instances in the proper section. Okay, it's not that long of an answer because I didn't include an example.
Post 30 Jun 2013, 19:30
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2796
Location: dank orb
All the auxiliary files would have something like:
Code:
macro code1.inc section {
  match =CODE,section \{
    <some code>
  \}
  match =DATA,section \{
    <some data>
  \}
}    
...and the in the main file:
Code:
FILES fix code1.inc,code2.inc,...
irp T,FILES { include `T } Wink

.code
  irp T,FILES { T CODE }

.data
  irp T,FILES { T DATA }    
...I know it looks complicated, but it's extremely flexible.


Last edited by bitRAKE on 02 Jul 2013, 02:02; edited 1 time in total
Post 30 Jun 2013, 19:39
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
"Flat" Assembler Exclamation Wink
Post 30 Jun 2013, 21:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16782
Location: In your JS exploiting you and your system
SokilOff wrote:
So here's the question: is there a way to tell fasm just to merge sections with the same names from different files ?
fasm itself can't do that without macros like bitRAKE suggested. But fasm can do what tasm/masm do by creating a COFF file and then you use a linker to merge the section together. If you want fasm to create the exe directly (without using a linker) then use a macro.
Post 30 Jun 2013, 23:57
View user's profile Send private message Visit poster's website Reply with quote
SokilOff



Joined: 20 Sep 2010
Posts: 15
bitRAKE wrote:
All the auxiliary files would have something like:
...
I know it looks complicated, but it's extremely flexible.


Thank you for your answer ! Atm I'm trying to create simple example using your suggestion. The problem is it cannot see external procedure from .inc
It looks like this:

main.asm
Code:
format PE GUI 4.0
entry start

include 'win32ax.inc'
include 'code1.inc'
IDD_MainDlg = 1

FILES fix code1.inc

.code
  irp T,FILES { T CODE }

  start:

        invoke  GetModuleHandle, 0
        invoke  DialogBoxParam, eax,IDD_MainDlg, HWND_DESKTOP, DialogProc, 0
        invoke  ExitProcess,0

proc    DialogProc hwnddlg, msg, wParam, lParam

        push    ebx  esi  edi
        cmp     [msg], WM_COMMAND
        je      .wmcmd
        cmp     [msg], WM_CLOSE
        je      .wmclose
        xor     eax, eax
        jmp     .finish

  .wmcmd:
        cmp     [wParam], BN_CLICKED shl 16 + IDOK
        jne     @f
        invoke  MessageBox,HWND_DESKTOP, msg_ok, caption_ok, MB_ICONINFORMATION
        jmp     .done
    @@:
        cmp     [wParam], BN_CLICKED shl 16 + IDCANCEL
        jne     .done
        stdcall ExternalProc      ; <--- procedure call from code1.inc
        jmp     .done

  .wmclose:
        invoke  EndDialog,[hwnddlg],0
  .done:
        mov     eax, 1
  .finish:
        pop     edi esi ebx
        ret
endp

.data
  irp T,FILES { T DATA }

  msg_ok      db 'Message from OK button', 0
  caption_ok  db 'Caption from OK button', 0


section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import  kernel,\
          GetModuleHandle,'GetModuleHandleA',\
          ExitProcess,'ExitProcess'

  import user,\
         DialogBoxParam,'DialogBoxParamA',\
         MessageBox,'MessageBoxA',\
         EndDialog,'EndDialog'

section '.rsrc' resource data readable

  directory RT_DIALOG,dialogs

  resource dialogs,\
           IDD_MainDlg,LANG_ENGLISH+SUBLANG_DEFAULT,demo

  dialog demo,'Create message box',70,70,140,35,WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_MODALFRAME
    dialogitem 'BUTTON','OK',IDOK,15,10,45,15,WS_VISIBLE+WS_TABSTOP+BS_DEFPUSHBUTTON
    dialogitem 'BUTTON','C&ancel',IDCANCEL,75,10,45,15,WS_VISIBLE+WS_TABSTOP+BS_PUSHBUTTON
  enddialog
    


code1.inc
Code:
macro code1.inc section {

  match =code,section \{
         proc   ExternalProc
                invoke  MessageBox, HWND_DESKTOP, msg_cancel, caption_cancel, MB_ICONINFORMATION
                ret
         endp
  \}

  match =data,section \{
         msg_cancel      db 'Message from Cancel button', 0
         caption_cancel  db 'Caption from Cancel button', 0
  \}
}    
Post 01 Jul 2013, 15:09
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2796
Location: dank orb
MATCH is case sensitive. "=code" will not work if you use "CODE" in the main file.

_________________
¯\(°_o)/¯ unlicense.org
Post 01 Jul 2013, 16:53
View user's profile Send private message Visit poster's website Reply with quote
SokilOff



Joined: 20 Sep 2010
Posts: 15
bitRAKE wrote:
MATCH is case sensitive.

Indeed, it did the trick. I should have read manual without skipping some parts Smile
Quote:
It's important to note that all macroinstructions, as opposed to internal directives of flat assembler, are case sensitive

Thanks a lot, bitRAKE ! Hopefully it will help to someone else in the future.
Post 01 Jul 2013, 17:40
View user's profile Send private message Reply with quote
SokilOff



Joined: 20 Sep 2010
Posts: 15
Another small question.

If I add any local variable to procedure from auxiliary .inc like this:
Code:
match =code,section \{
         proc   ExternalProc 

                local var_1:DWORD  ; <------

                invoke  MessageBox, HWND_DESKTOP, msg_cancel, caption_cancel, MB_ICONINFORMATION
                ret
         endp
  \}    


I get "Error: extra characters on line". Why ? And how to fix it ?
Post 02 Oct 2013, 11:58
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7367
Location: Kraków, Poland
The "local" inside a macro (and "match" block is also a kind of macro) has a different meaning, it is a directive that defines local symbols inside a macro, and it expects the parameters to be a simple list of comma-separated names - that's why it signalizes an error when it enounters ":".

To avoid "local" being interpreted by macro processor you have to escape it, in this case twice - once for the outer macro, and once for the inner "match":
Code:
match =code,section \{ 
         proc   ExternalProc  

                \\local var_1:DWORD

                invoke  MessageBox, HWND_DESKTOP, msg_cancel, caption_cancel, MB_ICONINFORMATION 
                ret 
         endp 
  \}    


The other solution would be to use the "fix" directive to redefine the "local" keyword, to get rid of the conflict at all.
Post 02 Oct 2013, 13:17
View user's profile Send private message Visit poster's website Reply with quote
SokilOff



Joined: 20 Sep 2010
Posts: 15
Thank you, Tomasz, for detailed explanation. Those nested macro are tricky sometimes.

Do you have any plans for FASM 2.0 to simplify code merging/separation in cases like this ?
Post 02 Oct 2013, 16:30
View user's profile Send private message 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-2019, Tomasz Grysztar.

Powered by rwasa.