flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [FASM1] Defining macro and struc for the same thing

Author
Thread Post new topic Reply to topic
DimonSoft



Joined: 03 Mar 2010
Posts: 1042
Location: Belarus
DimonSoft
We all know that there are really two kinds of macros in FASM1: macro and struc. The first thing is used for lexemes that are at the beginning of a line, the second—for the second lexemes in a line, if the first lexeme wasn’t a macro/turned out to be a label without colon :.

I often find myself writing something like
Code:
struc Foo params&
{
  ; All stuff goes here
  ...
}

macro Foo params&
{
  local ..a
  ..a Foo params
}    


The idea is that I sometimes want to have a more readable and flexible way for defining complex data but I definitely don’t want to repeat myself twice, so the “fake” label is a way to go. While the macro is pretty small, it still is boilerplate and gets quite annoying soon.

Besides, the struct macro definition would become smaller if there was some language-level solution to the problem.

I considered a few possible solutions and while some of them would be convenient for me personally, I’m not sure any of them would be a good solution in general.

1) Leave it as it is.
It just works. The macro and struc features are relatively independent from each other and neither is privileged.

2) Special “smacto” (struct + macro) keyword which would do the equivalent of the code in my example.
Adding a new keyword (no matter whether it exists in any vocabulary and how crazy it is) just to remove a pretty small piece of boilerplate doesn’t seem right from design point of view. But that would definitely solve the problem.

3) Shifting privileges to struc’s.
The idea is that since struc invokation is basically an equivalent of using data definition directives, so struc “should” automatically define the equivalent macro unless the same-named macro is present. Looks good at first glance but would definitely break some code if introduced now.

I wonder what other people (and Tomasz especially) think.
Post 07 Dec 2021, 12:06
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1231
Roman
Quote:

Special “smacto”

If smacto.
Do fields in smacto : Data,Initialize,code,procs,destroy
More excellent variant.

Field Data auto place in section data or section '.bss'

For example smacto A copy data to smacto B data.
Quote:
A::B 5,4


Mean copy from five element four elements.


Last edited by Roman on 08 Dec 2021, 03:52; edited 1 time in total
Post 07 Dec 2021, 13:58
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 601
Location: Russia
macomics
DimonSoft wrote:
Code:
struc Foo params&
{
  ; All stuff goes here
  ...
}

macro Foo params&
{
  local ..a
  ..a Foo params
}    


Code:
; Once for any other structure
macro That struct*, params& { local ..a
  ..a struct params }

struc Foo params&
{
  ; All stuff goes here
  ...
}

struc Foo1 par*, arg&
{
  ; All stuff goes here
  ...
}

struc Foo2
{
  ; All stuff goes here
  ...
}

That Foo    arg0, arg1, arg2, "str"
That Foo    arg3, arg1, arg4, "new"
That Foo1   arg5, arg6
That Foo2    


add:
Code:
 times 5 That Foo arg0, $, arg1+%     


Last edited by macomics on 09 Dec 2021, 19:31; edited 1 time in total
Post 07 Dec 2021, 17:56
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1231
Roman
macomics
And how using this?
And how run Function in struct?

And what mean All stuff goes here?
Please give more concrete info.
Post 09 Dec 2021, 03:54
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 601
Location: Russia
macomics
Roman wrote:
macomics
And how using this?
And how run Function in struct?

And what mean All stuff goes here?
Please give more concrete info.
When defining the structure, it is necessary to specify a label. To change this, the author of the topic suggests defining a macro with the same name. However, the whole action of this macro is to define a label with a unique (local) name.
DimonSoft wrote:
The idea is that I sometimes want to have a more readable and flexible way for defining complex data but I definitely don’t want to repeat myself twice, so the “fake” label is a way to go. While the macro is pretty small, it still is boilerplate and gets quite annoying soon.
But why do this for each structure when you can pass the name of this structure to the macro as a parameter, and define the macro itself once for the program. For the given example, the filling of structures does not matter, and several definitions are used to show the possibility of such use. Moreover, in the first two examples there is a concatenation of the structure name and the first parameter when passed to the macro, whereas in the third there is no argument. This feature should be kept in mind, because it can cause incompatibility due to the appearance of an extra (empty) parameter.
Code:
That Foo arg1
; macro That struct*="Foo arg1", params&="" {
; local ..label
; ..label Foo arg1, }
; struc Foo params&="arg1," { . db arg1, } ;<- incorrect
; struc Foo params&="arg1," { label .
; match any, arg1, \{ db arg1 \} } ; <- correct

; OR

That Foo, arg1
; macro That struct*="Foo", params&="arg1" {
; local ..label
; ..label Foo arg1 }
; struc Foo params&="arg1" { . db arg1 } ;<- correct
; struc Foo params&="arg1" { label .
; match any, arg1 \{ db arg1 \} } ; <- correct

; OR

macro That struct*, params& { local ..label
  match any, params \{ common ..label struct, params \}
  match , params \{ ..label struct \} }    
Where did you see function calls in the examples?

ADD: More examples:
Code:
 struc STRA txt& { . db .Length, txt
  .Length = $ - . - 1
  db 0
  .Bytes = $ - . }

TextBlock STRA "Text text text", 13, 10
That STRA "More text, second string", 13, 10
That STRA "One more string", 13, 10, 0

; TextBlock db 16, "Text text text", 13, 10, 0, 26, "More text, second string", 13, 10, 0, 17, "One more string", 13, 10, 0, 0     
Post 09 Dec 2021, 05:33
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1231
Roman
macomics

You example not useful.
This easy write another way and not use macro that.
And I don't understand yet why we need use macro that(for struct) in your example ?


Quote:
Where did you see function calls in the examples?

I propose put functions in struct. Do struct more like a class.
We talk about smacto.
Post 09 Dec 2021, 14:50
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 601
Location: Russia
macomics
Roman wrote:
And I don't understand yet why we need use macro that(for struct) in your example ?

Read:
DimonSoft wrote:
I often find myself writing something like
Code:
struc Foo params&
{
  ; All stuff goes here
  ...
}

macro Foo params&
{
  local ..a
  ..a Foo params
}        
To avoid doing this, it is enough to define the macro once.
Roman wrote:
I propose put functions in struct. Do struct more like a class.
We talk about smacto.
Maybe then we'll go directly to writing in C#.

ADD: Although I noticed that you use the word struct - so what prevents you from modifying your implementation of this macro for Windows by adding to it the ability to add data to sections. I would like to see from you an example of using the concurrent definition of struc and macro with the same names, where you can't do without it.
Post 09 Dec 2021, 15:54
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1231
Roman
I mean more cool features for struc.
For example struc have pointer procs tab and pointer to texts and data and state flags.
Run first proc from this struc and get New thread.
Thread run automaticly all next procs this struc.

We get more easy, flexible way and more powerful struc.

Because modern Cpu have many cores.
Now 8 cores for Cpu its usual things.
Post 10 Dec 2021, 05:45
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 601
Location: Russia
macomics
Repeat: Maybe then we'll go directly to writing in C#.
Roman wrote:
For example struc have pointer procs tab and pointer to texts and data and state flags.
For an assembler program, it's all just binary data. There are dXX/rXX directives for them.
Roman wrote:
Run first proc from this struc and get New thread.
Thread run automaticly all next procs this struc.
How do you imagine it when using BIN/MZ/COFF/COFF64/MSCOFF/MSCOFF64 formats.

Everything that you have described does not require an additional directive for yourself. It is quite possible to implement this using macro instructions/programs for a specific format/operating system. Although all these features mainly appear in multiplatform mechanisms in high-level languages.
Post 10 Dec 2021, 14:02
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 524
Location: Ukraine
Overclick
Code:
.code
macro smacto arg&
        {
        struc arg
        macro arg
        }
smacto FUN { db 5 }

FUN
forr FUN    
Post 01 Feb 2022, 11:53
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1846
Furs
Overclick wrote:
Code:
.code
macro smacto arg&
        {
        struc arg
        macro arg
        }
smacto FUN { db 5 }

FUN
forr FUN    
I was about to say that you can define macros within macros, so DimonSoft doesn't need a FASM "smacto" keyword, he can define one himself.
Post 01 Feb 2022, 18:50
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 524
Location: Ukraine
Overclick
Quote:

I was about to say that you can define macros within macros, so DimonSoft doesn't need a FASM "smacto" keyword, he can define one himself.

Idea is to define macro and struc at once by same name (FUN). My example works with single lined body, but it can call another macros where needed.
Post 01 Feb 2022, 21:38
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.