flat assembler
Message board for the users of flat assembler.

Index > Main > Questions about Preprocessor directives

Author
Thread Post new topic Reply to topic
SPTH



Joined: 24 Jul 2004
Posts: 91
SPTH
Hello,
I've just read the manual, but did not get an answere to my question.



----
1.) Code+Data by Macro
It would make coding so much easier if i had a macro, that could create code and data seperatly.

First example:
Code:
.data
  db 0x0
DataHere:

.code
MakeCodeAndData 2 15
    


should create

Code:
.data
    db 0x0
DataHere:
    db 15

.code
    mov al, 2
    


Or second example:

Code:
.data
  db 0x0
DataHere:

.code
MakeCodeAndData 2 15
MakeCodeAndData 12 11
MakeCodeAndData 17 12
    


should create

Code:
.data
    db 0x0
DataHere:
    db 15
    db 11
    db 12

.code
    mov al, 2
    mov al, 12
    mov al, 17
    





If that does not work, it would also help:
Code:
.data
  db 0x0
DataHere:
  db 0x0
  db 0x0
  db 0x0

.code
MakeCodeAndData 2 15
MakeCodeAndData 12 11
MakeCodeAndData 17 12
    


should create

Code:
.data
    db 0x0
DataHere:
    db 15
    db 11
    db 12

.code
    mov al, 2
    mov al, 12
    mov al, 17
    



Do you have any idea how I can use this? I would also use very nasty tricks :) Without this, i have to write code+data seperatly for several 100s instructions - that would be crazy!


Is that possible?


Thanks alot for your help!

Have a nice evening,
SPTH

[edit]
Found solution to Problem 2 and 3, so removed that.
[/edit]
Post 10 Dec 2010, 23:07
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Well, in order to create code on the place where macro is invoked and data in some other place, you should separate data definition and create inside your macro, new macro (or define symbolic constant) with data definition, that to be invoked in the place where you want to put data.
For example, download Fresh and see the file "include/macro/globals.inc".
Post 11 Dec 2010, 07:07
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
I not sure what you're trying to do exactly but here's one way.
Code:
        format binary as 'out.asm'

macro MakeCodeAndData [d,c]
{
common
        db '.data',0x0D,0x0A
        db '    db 0x00',0x0D,0x0A
forward
        db '    db '#`d,0x0D,0x0A
common
        db '.code',0x0D,0x0A
forward
        db '    mov al,'#`c,0x0D,0x0A
}

MakeCodeAndData 2,15,\
                12,11,\
                17,12
    

This will compile a new source file with added data, code that you can then also compile to executable. If you have a lot of other data or code, you can use the file 'some/file.name' in place of the macro db's to include a data and code source files instead of defining the source in the string.
Post 11 Dec 2010, 17:47
View user's profile Send private message Reply with quote
SPTH



Joined: 24 Jul 2004
Posts: 91
SPTH
Thank you JohnFound and cod3b453!

cod3b453 - I've tried your way first, it worked, but just if I can call the macro one single time. I dont know if I can do that in the further progress of the code, so I can not use it.
However, it is a very nice solution, I may use that information for other projects Thank you!

JohnFound:
I have checked the source you suggested and read more about macros in macros etc. Very interesting topic. I nearly succeeded using a multi-macro solution (the problem was, that i wanted to use macro InSideMacro#COUNTER arg,val { ... }. I could define that, but could not call InsideMacro0, InsideMacro1, ... I dont know why.

But when I read further about FASM macros, and using the idea with separation of creation of code + data, i succedded with a simple list solution:

Code:
include '..\FASM\INCLUDE\win32ax.inc'

list equ

macro MakeCodeAndData arg, val
{
    mov al, arg

    match any, list \{ list equ list,val \}
    match , list \{ list equ val  \}
}

.data
    db 0x0

.code
start:
    MakeCodeAndData 2,15
    MakeCodeAndData 12,11
    MakeCodeAndData 17,12 

    DataHere:
        db list

.end start    


The only thing i was not able to produce is the list in the data-section - even if i put the data-section below to the code section. But that is not important to my project, so everything is good :)

Thanks again for the help!
Post 13 Dec 2010, 00:15
View user's profile Send private message Reply with quote
SPTH



Joined: 24 Jul 2004
Posts: 91
SPTH
I have another question that is related to that topic:

This works:
Code:
include 'E:\Programme\FASM\INCLUDE\win32ax.inc'

macro M c,arg
{
        cmp eax, arg
        je  Label#c
        inc al

        Label#c:
}

.data
        db 0x0

.code
start:
        M 1,23
        M 2,23
        M 3,23
        M 4,23
        ret
.end start    


but that does not work:

Code:
include 'E:\Programme\FASM\INCLUDE\win32ax.inc'

macro M c,arg
{
        cmp eax, arg
        je  Label#c
        inc al

        Label#c:

        COUNT=COUNT+1
}

.data
        db 0x0

.code
start:
        COUNT=1
        M COUNT,23
        M COUNT,23
        M COUNT,23
        M COUNT,23
        ret
.end start     


Why cant I use a preprocessor-variable in connection with Labels?
Post 14 Dec 2010, 01:26
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17639
Location: In your JS exploiting you and your system
revolution
COUNT is not a preprocessor-variable. It is an assembler variable.

But for macros just us the local directive:
Code:
macro M arg {
 local   .lab
        cmp     eax,arg
     je      .lab
        inc     al
    .lab:
}    
Post 14 Dec 2010, 01:30
View user's profile Send private message Visit poster's website Reply with quote
SPTH



Joined: 24 Jul 2004
Posts: 91
SPTH
Hey revolution,

thanks for the answere. But imagine I want to call that label at another place of the source, like for multiple SEHs in my code:

Code:
include 'E:\Programme\FASM\INCLUDE\win32ax.inc'

macro SEH_TRY c
{
        push    dword[fs:0x0]
        mov     eax,dword[fs:0x0]
        pop     dword[ExcReg_prev]      ; Save old Exception Handler (whatever it is)

        push    SEH_Handler#c
        push    dword[fs:0x0]
        mov     dword[fs:0x0], esp   ; this works!
}

macro SEH_EXCEPTION c
{
        jmp     SEH_NoException#c

     SEH_Handler#c:

        mov     eax, dword[ExcReg_prev]
        mov     dword[fs:0x0], eax
}

macro SEH_END c
{
        jmp     SEH_Finish#c

     SEH_NoException#c:
        push    dword[ExcReg_prev]
        pop     dword[fs:0x0]
        add     esp, 8
     SEH_Finish#c:
}

.data
        ExcReg_prev dd 0x0

.code
start:


           SEH_TRY 2
                invoke  MessageBox, 0x0, "try2", "try2", 0x0
;                mov     dword[0x0], eax
           SEH_EXCEPTION 2
                invoke  MessageBox, 0x0, "EX2", "EX2", 0x0
           SEH_END 2
                invoke  MessageBox, 0x0, "fin2", "fin2", 0x0

           SEH_TRY 1
                invoke  MessageBox, 0x0, "try1", "try1", 0x0
                mov     dword[0x0], eax
           SEH_EXCEPTION 1
                invoke  MessageBox, 0x0, "EX1", "EX1", 0x0
           SEH_END 1
                invoke  MessageBox, 0x0, "fin1", "fin1", 0x0

        invoke ExitProcess, 0x0

.end start    


I need an external variable then, would be easier to have a counter.

What is the difference of preprocessor-variable and assembler-variable?
Post 14 Dec 2010, 15:40
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
SPTH,

Symbolic constant (that's what you're calling “preprocessor-variable”) is preprocessor's feature (i.e. it works when macroinstructions are processed). There is rept 1 var:expr { … } trick that allows some numeric computations during this compilation phase. Preprocessor is single-pass, thus some restrictions are implied (e.g. no forward reference).

In your case, local macro-body-specific directive is enough to generate unique labels for each macroinstruction's invocation (along with some symbolic constant to pass generated name between associated macroinstructions). You may look at structuring macroinstructions from MACRO\IF.INC for example of such communication.
Post 14 Dec 2010, 18:08
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.