flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > How to define macro within macro ?

Author
Thread Post new topic Reply to topic
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 26 Feb 2012, 22:17
As I am not very familiar with macros, following question.
I am writing a program for creating wav files (content) and want to use a complex data definition. What's the best way to do and use ?

First I need a bit sequence for one pulse consisting of 3 high values and three low values (need 3 for granularity). And have a silent "pulse" consist of 26 values. Then I need a bit0 and bit1 structure. bit0 consists of 4 pulses, followed by a silent pulse and bit1 consists of 9 pulses, followed by a silent pulse.

I try to code as I think (which doesn't work as I want):

Code:
macro wavpulse { 0FAh,0FAh,0FAh,05h,05h,05h ]
macro wavsilent { db 26 dup(80h) }

macro wavbit0 { db 4 dup(wavpulse) },wavsilent
macro wavbit1 { db 9 dup(wavpulse) },wavsilent
    


so wavbit0 should be in fact db 0FAh,0FAh,0FAh,05h,05h,05h,0FAh,0FAh,0FAh,05h,05h,05h,0FAh,0FAh,0FAh,05h,05h,05h,0FAh,0FAh,0FAh,05h,05h,05h followed by 26 times with db 80h

How to code this in a good structure ?
And I need additional the full size of wavbit0 and wavbit1.

Want to use later in this style:

Code:
     invoke  WriteFile,[hfile],wavbit0,sizeof(wavbit0),param_buffer,NULL
 invoke  WriteFile,[hfile],wavbit1,sizeof(wavbit1),param_buffer,NULL
    


I know the sizeof operator known from C is not existing but maybe there is another convenient way.
I know a workaround for coding wavbit0 and wavbit1 manually but wanted to learn something and do more structuring in source code.

Thanks for help. Wink
Post 26 Feb 2012, 22:17
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 26 Feb 2012, 22:28
The simplest version would be without any macros really:
Code:
wavpulse equ 0FAh,0FAh,0FAh,05h,05h,05h
wavsilent equ 26 dup(80h)

wavbit0 db 4 dup(wavpulse),wavsilent
  .size = $ - wavbit0
wavbit1 db 9 dup(wavpulse),wavsilent
  .size = $ - wavbit1    
(you use wavbit0.size and wavbit1.size to get the sizes of defined data)

And with macros it could look like:
Code:
wavpulse equ 0FAh,0FAh,0FAh,05h,05h,05h
wavsilent equ 26 dup(80h)

struc wavbit %pulses {
  . db %pulses dup(wavpulse),wavsilent
  .size = $-.
}

wavbit0 wavbit 4
wavbit1 wavbit 9    
Post 26 Feb 2012, 22:28
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 26 Feb 2012, 23:04
Thanks, that was fast and works great.
I didn't expect that I could pass a complex text sequence to EQU directive. Very Happy
Post 26 Feb 2012, 23:04
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 27 Feb 2012, 10:53
EQU in fasm is just a textual substitution (as opposed to =, which is numerical), and is processed at preprocessor stage like macros (and it does almost the same thing as DEFINE directive).
It is important to know this distinction, otherwise you may fall in one of the traps:
Code:
a equ 2 + 2
b = 2 + 2
dd a*2 ; becomes 2 + 2*2, the result is 6
dd b*2 ; b=4, the result is 8    
Post 27 Feb 2012, 10:53
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 27 Feb 2012, 22:30
Yes, thanks for pointing this out.

Another question regarding this definition:

Code:
wavbit0 db 4 dup(wavpulse),wavsilent
  .size = $ - wavbit0 
    


wavbit0 seems to be a label which could be added with a property like size with the "." operator. Is this just a special case or could this be used to give any other property to a label ?

I wonder if following would be possible to give size and maybe color (or any other attribute in context):

Code:
wavbit0 db 4 dup(wavpulse),wavsilent
  .size = $ - wavbit0 
  .color = 'green'
    
Post 27 Feb 2012, 22:30
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 27 Feb 2012, 22:43
shutdownall: Did you try it? It does work as you will know when you assemble it. But be aware that 'green' is simply a number 0x6e65657267
Post 27 Feb 2012, 22:43
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 28 Feb 2012, 16:23
Okay. But can hold ascii values up to 8 chars(64 bit) - just in reversed order. Very Happy
Also this works to give the duration of each wavbit in microseconds:

Code:
wavbit0 db 4 dup(wavpulse),wavsilent
  .size = $ - wavbit0
  .duration = wavbit0.size*50
  .color = 'magenta'   
    
Post 28 Feb 2012, 16:23
View user's profile Send private message Send e-mail 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.