flat assembler
Message board for the users of flat assembler.

 Index > Macroinstructions > Code Comment Macro Goto page 1, 2  Next
Author
chris

Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris
This is an interesting macro(at least IMHO).
I wrote it so that I can comment out multi-lines of code like what I can do in C/C++ or other HLLs.

Code:
```; comment.inc
; enable various comment styles

macro . sym
{
; C/C++ styles
define first +
irps t,sym
\{
match +,first
\\{
define first -
match /,t \\\{ if 0 \\\}
\\}

define last@sym t
\}
match /,last@sym \{ end if\}

; other styles
match ++,sym \{ if  0  \}
match --,sym \{ end if \}

; braces
match [,sym \{ if  0  \}
match ],sym \{ end if \}

match (,sym \{ if  0  \}
match ),sym \{ end if \}
}
```

The above code defines the '.'(dot) as a macro which enables you to do things like:
Code:
```; C/C++ styles
./*
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.*/

./++
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.--/

./******************
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.*********************/

; wild styles
./---------))---((--------
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.---~~~~~---------------/

./~~~~~~~~~~~~~~~~~
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.~~~~~~~~~~~~~~~~~~/

./+++++++++++++++++
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.------------------------/

; simple styles
.++
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.--

; using braces
.(
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.)

.[
mov     eax,ebx
jmp     lab
push    eax
call    ebx
.]
```

the tricky thing is that you can pass the special characters of assembler like +,-,*,~ etc. to fasm preprocessor as macro arguments.(based on my experience, there are also some special characters for the preprocessor like `, {}, <>, # and comma that can not be used as macro arguments). In a similar way, you can add your own styles of comments.

To Tomasz: `, {}, <>, # and comma, I am not sure if all of them are listed here or there may be some mistakes. Could you complete it, thanks.
14 Jul 2006, 08:24
Joshua

Joined: 12 Jul 2003
Posts: 56
Location: Belgium
Joshua
or a much easier version:
Code:
`comment fix macro comment    `

Code:
```comment {
blah
blahblah
blahblahblah
}    ```
15 Jul 2006, 12:09
Ancient One

Joined: 28 Feb 2005
Posts: 55
Ancient One
how about multiline comment that can really comment ANYTHING? e.g

./*
macro incomplete {
.*/
16 Jul 2006, 07:13
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
chris: "." already has somewhat special meaning in "struc", check manual

joshua - maybe it could be improved to purge macro immeadiately after declaration
16 Jul 2006, 08:49
Joshua

Joined: 12 Jul 2003
Posts: 56
Location: Belgium
Joshua
As in:
Code:
```comment fix macro __COMMENT {
end_comment fix } purge __COMMENT    ```

Usage:
Code:
```comment
blah
blahblah
blahblahblah
end_comment    ```
?
16 Jul 2006, 17:53
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
yup, but you can also fix "}" symbol to leave usage as it was
Code:
```comment {
...
}    ```

(sorry, i am lazy to write it myself, take it as excercise )
16 Jul 2006, 21:59
UCM

Joined: 25 Feb 2005
Posts: 285
UCM
Then you get a problem, since any other macros will be messed up.
17 Jul 2006, 00:48
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
only those inside comments - which would be messed up with any other way mentioned here, too. maybe you forgot that you can unfix "}" back to original meaning
17 Jul 2006, 07:08
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18847
revolution
I often use 'IF 0' and 'END IF' to temporarily disable parts of code when testing. Using the macro style of commenting has too many problems for my liking. For permanent comments I always use the semi-colon.
17 Jul 2006, 07:31
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
revolution: macro style has problem with commenting macros (more precisely: texts conatining "}"). And you can't comment these with "if" anyway.
17 Jul 2006, 08:16
UCM

Joined: 25 Feb 2005
Posts: 285
UCM
vid: Let's see how it would work then, "unfixing" } back to it's original meaning.
17 Jul 2006, 19:29
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
sorry, i got wrong, i tried it and now i think it isn't possible

unfixing "}" is done this way: "} fix }", but i thought i can use something like "\} \fix \}" inside macro, unfortunately fasm's preprocessor works in other way (well... it is stated in manual... my bad)
17 Jul 2006, 22:23
chris

Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris
Quote:
or a much easier version:

yeah, but I like the highlighting effects of the special characters and the flexibility. Unfortunately, you can not comment out macro definitions in this way either:

Code:
```comment fix macro __COMMENT {
end_comment fix } purge __COMMENT

; not work
comment
macro inner{ display 'inner' }
end_comment

; unless you modify the inner macros
comment
macro inner \{ display 'inner' \}
end_comment
```

the same thing happens for another version of using 'fix' - all because of the { or } is messed up with the macros you are trying to comment. I don't know a way to comment out the preprocessor things like macros or strucs definitions using preprocessor macros, even if the 'fix' has some priority over other preprocessor directives, as you can see, it just get messed up.

Quote:

how about multiline comment that can really comment ANYTHING? e.g

./*
macro incomplete {
.*/

well, you have to use semicolon. Basically, the comment macro works only at the assembler level, if does not prevent preprocessor from getting into the commentted parts(it does for the assembler!). IMO, I don't comment out a macro definition because if you don't use it anywhere in your code, it has nothing to do with the output file; however, if you do use it somewhere and you wanna disable it for a while, the comment macro plays the role:
Code:
```macro foo { display 'I am alive!'  }
...

./*
foo
.*/
```

Quote:

chris: "." already has somewhat special meaning in "struc", check manual

you are right, I just overlooked it for the first time. Fortunately, fasm seems to "use some extra circuit" to handle the dot within a struc macro:
Code:
```macro .
{
display 'dot'
}

struc bar x
{
. dd x
}

foobar  bar 1
```

the dot within a struc definition is always replaced with the name of the struc, no matter if it has already some other definitions.

In one word, the comment macro is used for the code that if it were outside this macro, it would be assembled into the output file, and not for the pure preprocessor constructions - consider the following situation:
Code:
```macro prolog_comment_inside size
{
./*
push  ebp
mov   ebp,esp
sub    esp,size
.*/
}

macro prolog size
{
push  ebp
mov   ebp,esp
sub   esp,size
}

start_1:
prolog_comment_inside 16

; or

start_2:
./*
prolog 16
.*/
```

the comment macro used somewhat differently in the above examples, but the rule is that both of them are used for code that will be assembled into output file. I hope this clarifies some aspects of the code comment macro.
18 Jul 2006, 03:29
Joshua

Joined: 12 Jul 2003
Posts: 56
Location: Belgium
Joshua
hmm, maybe a cleaner version would be:
Code:
`comment fix rept 0    `
Usage:
Code:
```comment {
blah
blahblah
blahblahblah
}    ```
The advantage of a macro comment is also that it gets stripped in preprocessing instead of assembler, reducing compile time.

On a side note, it doesn't appear possible to fix special chars... is it possible to change that in a future fasm version?
19 Jul 2006, 18:00
okasvi

Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
I dont really get it, I see this as problem for editor used to be solved, fasm already provides ';' for commenting, and IF you really need faster way to comment blocks of code, use editor that supports commenting blocks of code with ';'.

edit: I think winasm studio or w/e it's called does that, or then it was radasm, or maybe them both.
19 Jul 2006, 19:25
rugxulo

Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
The comment directive is supported by most other assemblers. I'm not saying that I really need it personally, but if it can't be done easily with macros, maybe Privalov should implement it natively.
19 Jul 2006, 20:17
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
honestly, how often are you commenting a complex macro definition?
19 Jul 2006, 22:06
Nikolay Petrov

Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov
vid wrote:
honestly, how often are you commenting a complex macro definition?
personaly i, when is need it to hide or exchange part from working code, but for this is not necessary
rugxulo wrote:
...implement it natively...

_________________
regards
25 Jul 2006, 21:37
Borsuc

Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc
Why can't you guys just use ";" at each line? This way you also look and "inspect" that line to see if it really needs to be commented.

Let's not make Fasm bloated design, having 7 different ways to comment a piece of code (I know I exaggerate here).
29 Jul 2006, 17:56

Joined: 21 Sep 2005
Posts: 187
Location: Ukraine,Kharkov
Quote:

Why can't you guys just use ";" at each line?

and if i must comment a page?
it will eat much of time to comment big numbers of lines.
30 Jul 2006, 06:43
 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