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
Thread Post new topic Reply to topic
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!
Post 21 Mar 2010, 14:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 21 Mar 2010, 14:34
You can't. fasm does not support inline macros.
Post 21 Mar 2010, 14:34
View user's profile Send private message Visit poster's website Reply with quote
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)>    
Razz

I know that it's doesn't and it's not possible to make it work as OP wants to Wink
Post 21 Mar 2010, 15:05
View user's profile Send private message Reply with quote
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?
Post 21 Mar 2010, 16:15
View user's profile Send private message Reply with quote
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,Very Happy
hopcode

_________________
⠓⠕⠏⠉⠕⠙⠑
Post 21 Mar 2010, 17:05
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: 20445
Location: In your JS exploiting you and your system
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
Post 21 Mar 2010, 17:11
View user's profile Send private message Visit poster's website Reply with quote
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
    

_________________
⠓⠕⠏⠉⠕⠙⠑
Post 21 Mar 2010, 17:29
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: 20445
Location: In your JS exploiting you and your system
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.
Post 21 Mar 2010, 17:32
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 21 Mar 2010, 17:50
Why make life so difficult?
Code:
mov eax,124*200    
Post 21 Mar 2010, 17:50
View user's profile Send private message Reply with quote
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, Very Happy
hopcode

_________________
⠓⠕⠏⠉⠕⠙⠑
Post 21 Mar 2010, 17:54
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: 20445
Location: In your JS exploiting you and your system
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    
Post 21 Mar 2010, 18:06
View user's profile Send private message Visit poster's website Reply with quote
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:
macro address space is separate from the variable address space.
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,Very Happy
hopcode

_________________
⠓⠕⠏⠉⠕⠙⠑
Post 21 Mar 2010, 21:46
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
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.
Post 21 Mar 2010, 21:51
View user's profile Send private message Visit poster's website Reply with quote
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.
Post 22 Mar 2010, 02:40
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
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.
Post 22 Mar 2010, 04:52
View user's profile Send private message Reply with quote
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.
Post 22 Mar 2010, 09:48
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
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... Very Happy... 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, Very Happy
hopcode

_________________
⠓⠕⠏⠉⠕⠙⠑
Post 26 Mar 2010, 12:49
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: 20445
Location: In your JS exploiting you and your system
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
Post 26 Mar 2010, 12:59
View user's profile Send private message Visit poster's website Reply with quote
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 Question

_________________
⠓⠕⠏⠉⠕⠙⠑
Post 26 Mar 2010, 13:11
View user's profile Send private message Visit poster's website Reply with quote
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).
Post 26 Mar 2010, 19:47
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.