flat assembler
Message board for the users of flat assembler.

 Index > Main > How can I use parameters with "define"? Goto page 1, 2  Next
Author
lieyan2024

Joined: 21 Mar 2010
Posts: 3
lieyan2024 21 Mar 2010, 14:32
I want to write below code in FASM:

mov eax, multiply(124, 200)

In C language, I can define the macro "multiply" like this:

#define multiply(x, y) ((x) * (y))

But in FASM, how can I do this?

I'm sorry for my english!
21 Mar 2010, 14:32
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20142
revolution 21 Mar 2010, 14:34
You can't. fasm does not support inline macros.
21 Mar 2010, 14:34
Fanael

Joined: 03 Jul 2009
Posts: 168
Fanael 21 Mar 2010, 15:05
Code:
```macro mov first*, second*
{
local done
define done 0

match =multiply =( x =, y =), second
\{
mov first, x * y
define done 1
\}
match =0, done
\{
mov first, second
\}
}

mov eax, <multiply(2, 7)>    ```

I know that it's doesn't and it's not possible to make it work as OP wants to
21 Mar 2010, 15:05
lieyan2024

Joined: 21 Mar 2010
Posts: 3
lieyan2024 21 Mar 2010, 16:15
Thanks revolution, you let me know the limit of FASM.

Thanks Fanael, you gave me a resolution of the example.

But "inline macro" is powerful utility, why does not FASM support it?
21 Mar 2010, 16:15
hopcode

Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 21 Mar 2010, 17:05
revolution wrote:
You can't. fasm does not support inline macros.

Yes...and no: have you ever seen this ?
Code:
```macro @multiply X,Y {@multiply equ X*Y }

@multiply 7,3
display_decimal @multiply
mov eax,@multiply   ;<--------- 15h

@multiply 8,2
display_decimal @multiply
mov eax,@multiply   ;<--------- 10h
```

It seems a PURGE after an EQU. All symbols and directive are equals,
but some of them more "assembling stage" than other symbols.

Cheers,
hopcode

_________________
⠓⠕⠏⠉⠕⠙⠑
21 Mar 2010, 17:05
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20142
revolution 21 Mar 2010, 17:11
hopcode wrote:
It seems a PURGE after an EQU. All symbols and directive are equals,
but some of them more "assembling stage" than other symbols.
No. Purge is to undo macros. Restore is to undo EQUs. Restruc is to undo strucs.

BTW: You might want to consider putting brackets around X*Y
Code:
`macro @multiply X,Y {@multiply equ (X*Y) }    `
Or use an '='
Code:
`macro @multiply X,Y {@multiply = X*Y }    `

Last edited by revolution on 21 Mar 2010, 17:33; edited 1 time in total
21 Mar 2010, 17:11
hopcode

Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 21 Mar 2010, 17:29
revolution wrote:
Restore is to undo EQUs...
Oh, sorry, right qui-pro-quo
But to me it sounds strange every of the above behaviours apart this one, because inside macro it is not possible purging the symbol, also a temporairly EQU before exiting the macro is still ok in the scope.

Code:
```macro @multiply X,Y {
@multiply equ mov eax,(X*Y)
@multiply
}
@multiply 7,3 ;-----ok
@multiply 8,2 ;-----ok
```

_________________
⠓⠕⠏⠉⠕⠙⠑
21 Mar 2010, 17:29
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20142
revolution 21 Mar 2010, 17:32
hopcode wrote:
... because inside macro it is not possible purging the symbol ...
Please show an example of what you are having trouble with.
21 Mar 2010, 17:32
bitshifter

Joined: 04 Dec 2007
Posts: 791
Location: Massachusetts, USA
bitshifter 21 Mar 2010, 17:50
Why make life so difficult?
Code:
`mov eax,124*200    `
21 Mar 2010, 17:50
hopcode

Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 21 Mar 2010, 17:54
I mean that this is strange that such code works
Code:
```macro @multiply X,Y {@multiply = X*Y }
```

...Or i mistake, it is to say, "=" works external ok like purge, but not in the macro scope (as it is by design, during macro definition). The question is:

even if in the scope of it i have redefined it @multiply = Y+X ,
it remains always a macro, and i can use it again ?

... must go now,
hear you later

Cheers,
hopcode

_________________
⠓⠕⠏⠉⠕⠙⠑
21 Mar 2010, 17:54
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20142
revolution 21 Mar 2010, 18:06
The macro address space is separate from the variable address space. You can have this:
Code:
```@m = 4
macro @m v {db v}
@m @m    ```
21 Mar 2010, 18:06
hopcode

Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 21 Mar 2010, 21:46
Apart the fact that bitshifter has the best solution,imho
revolution wrote:
Also, it should be true for the following case too...
Code:
```        ;----but it doesnt work "reserved word used as symbol".
macro @m v {db v} ;<---
@m @m
@m = 90h    ```

But this is ok
Code:
``` macro @m v {db v}  ;--- definition
@m n
n = 90h
```

Now, i'll try to explain it simple. Please,correct the text if needed.
Macro-name definitions (and their internal-implement too, see the
following case) have priority on all other assignments-type: equ,define,=
I have not tested it with fix, because iirc it is somehow "deprecated".
For example the "bold" assignment @m = 90h has low priority (a lower
stack position i imagine) after the definition of a macro under
the same name "@m"; that assignment is, as you know, an assembling-stage
assigment, while macro definition and implementation happens already
in a pre-procedural stage.

For this reason you have (all thorough the code)
working code like the following
Code:
```macro @multiply X,Y {
define @multiply mov eax,(X*Y)
@multiply
define @multiply (X*Y)
}

@multiply 7,3
mov ecx,@multiply

@multiply 8,2
mov ecx,@multiply

restore @multiply
restore @multiply

mov edx,@multiply

;00403000  B8 15000000  MOV EAX,15h
;00403005  B9 15000000  MOV ECX,15h
;0040300A  B8 10000000  MOV EAX,10h
;0040300F  B9 10000000  MOV ECX,10h
;00403014  BA 15000000  MOV EDX,15h
```
Anyway, note that as in previous versions of fasm (i have seen 3 or 4) you cannot purge the macro during its internal implementation,
without using an external way to do it. Internally, (under the scope of the macro) fasm let you define/do/"execute" a name even if this name appears to be the macro-name definition
Code:
``` define @multiply mov eax,(X*Y)
@multiply
```

Now,... will this wonderful feature be changed in the future ?

Cheers,
hopcode

_________________
⠓⠕⠏⠉⠕⠙⠑
21 Mar 2010, 21:46
edfed

Joined: 20 Feb 2006
Posts: 4325
Location: Now
edfed 21 Mar 2010, 21:51
lieyan2024 wrote:
I want to write below code in FASM:

mov eax, multiply(124, 200)

In C language, I can define the macro "multiply" like this:

#define multiply(x, y) ((x) * (y))

maybe asm and C are not the same.
c is abstract, asm is real.

when coding in asm, you should forget averything about C, and think around a machine, named CPU. a pure mechanical machine that does things around binary datas in a memory that is at real locations.

then, you should know what a CPU is and does for real.
21 Mar 2010, 21:51
lieyan2024

Joined: 21 Mar 2010
Posts: 3
lieyan2024 22 Mar 2010, 02:40
Thanks edfed, I know FASM is a very simple assembler, we must make more effort than in high-level programming languages.

I like this! So I want to implement "rva" by myself.

"rva" in FASM is an operator, I can use it like below:

mov eax, rva STRING_TABLE

Look, "rva" performs like a "inline macro".

I think other guys maybe want to do the same thing, this is why I submitted this subject.
22 Mar 2010, 02:40
bitshifter

Joined: 04 Dec 2007
Posts: 791
Location: Massachusetts, USA
bitshifter 22 Mar 2010, 04:52
The C language can handle your macro two ways:

#define multiply(x,y) ((x)*(y))

At compile time, if the x and y are resolvable
the occurance is replaced with the product of x * y.

At run time, if product cannot be resolved, the compiler
will insert the appropriate opcodes to get the job done.
22 Mar 2010, 04:52
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Mar 2010, 09:48
Quote:
The C language can handle your macro two ways

Well, the macro is always handled the same way - string substitution. It's just later that compiler will optimize constant expressions to their result.
22 Mar 2010, 09:48
hopcode

Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 26 Mar 2010, 12:49
continuing on the subject that does not persuade me,
sorry if off-topic, but this compile ok
Code:
``` if defined @m
display "@m defined as constant"
end if
@m = 40h
```

no antinomy here (btw: in the manual 2.2.6 Multiple passes the typo
antinomy instead of antynomy -have we a [st] strikethru on board ?)

now, macro are similiar to sym-constant, 2.3.3 Macroinstructions
Quote:
So if you define macroinstruction and symbolic constant of the same name,...

i do it... ... in two steps...
Code:
``` macro @m v {db v}
if defined @m
display "@m defined as constant" ;<---- this never gets displayed
end if
```

but this way dosnt compile
Code:
``` macro @m v {db v}       ;error reserved word used as symbol
if defined @m
display "@m defined as constant"
end if
@m = 40h
```

also: if defined and used excludes macroname symbols
what the purpouse there in going down to pre-processing stage and raise
error ?

Or, even if not so useful, defined and used could be enhanced to check
for macro-definition. This would be good from external to "shot"
an "awareness" of what/if macro were defined in preexisting code.
This could correspond to an exist operator
Code:
``` if exist macroname
display "macroname"
end if
```

Cheears,
hopcode

_________________
⠓⠕⠏⠉⠕⠙⠑
26 Mar 2010, 12:49
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 20142
revolution 26 Mar 2010, 12:59
hopcode wrote:
Or, even if not so useful, defined and used could be enhanced to check for macro-definition.
It can't be set to do that because the name spaces are different. You would have no way to distinguish between a variable and a macro.
Code:
```@m = 4
macro @m v {db v}
if defined @m ;what does it check for? The macro or the variable
;...    ```
And the other suggestion of "if exist ..." is also problematic because if is only precessed at the assembler stage, and the assembler stage does not have macros. You would need to define a different keyword that is processed at the preprocessor stage.
Code:
```SomeNewKeyWord @m {
;do something if @m is defined as a macro (or struc also?)
}    ```

Last edited by revolution on 26 Mar 2010, 13:54; edited 1 time in total
26 Mar 2010, 12:59
hopcode

Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode 26 Mar 2010, 13:11
revolution wrote:
...if is only precessed at the assembler stage,
On this , yes, i agree completely, but this
Quote:
You would have no way to distinguish between a variable and a macro.
Code:
```@m = 4
macro @m v {db v}
if defined @m ;what does it check for? The macro or the variable
;...    ```

But you should have the way, already, by design, just because defined and used are for "variables".
Or

_________________
⠓⠕⠏⠉⠕⠙⠑
26 Mar 2010, 13:11
baldr

Joined: 19 Mar 2008
Posts: 1651
baldr 26 Mar 2010, 19:47
hopcode,

@m is recognized as a macro name only if it is the first token on the line (not counting optional label_name: label definition). Almost the same for struc macro (in this case macro name should be the second token, besides label again).
26 Mar 2010, 19:47
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page 1, 2  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum