flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Code Comment Macro

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
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
        add     eax,ecx
        jmp     lab
        push    eax
        call    ebx
.*/

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

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

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

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

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

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

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

.[
        mov     eax,ebx
        add     eax,ecx
        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. Wink

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. Very Happy
Post 14 Jul 2006, 08:24
View user's profile Send private message Reply with quote
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
}    
Post 15 Jul 2006, 12:09
View user's profile Send private message Reply with quote
Ancient One



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

./*
macro incomplete {
.*/
Post 16 Jul 2006, 07:13
View user's profile Send private message MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
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
Post 16 Jul 2006, 08:49
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
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    
?
Post 16 Jul 2006, 17:53
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
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 Wink )
Post 16 Jul 2006, 21:59
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM
Then you get a problem, since any other macros will be messed up.
Post 17 Jul 2006, 00:48
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
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
Post 17 Jul 2006, 07:08
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16891
Location: In your JS exploiting you and your system
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.
Post 17 Jul 2006, 07:31
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
Location: Slovakia
vid
revolution: macro style has problem with commenting macros (more precisely: texts conatining "}"). And you can't comment these with "if" anyway.
Post 17 Jul 2006, 08:16
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM
vid: Let's see how it would work then, "unfixing" } back to it's original meaning.
Post 17 Jul 2006, 19:29
View user's profile Send private message Reply with quote
vid
Verbosity in development


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

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)
Post 17 Jul 2006, 22:23
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
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!'  }
...

; still dead
./*
    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.
Post 18 Jul 2006, 03:29
View user's profile Send private message Reply with quote
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?
Post 19 Jul 2006, 18:00
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 383
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.
Post 19 Jul 2006, 19:25
View user's profile Send private message MSN Messenger Reply with quote
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. Smile
Post 19 Jul 2006, 20:17
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
Location: Slovakia
vid
honestly, how often are you commenting a complex macro definition?
Post 19 Jul 2006, 22:06
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
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
Post 25 Jul 2006, 21:37
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2468
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). Wink
Post 29 Jul 2006, 17:56
View user's profile Send private message Reply with quote
dead_body



Joined: 21 Sep 2005
Posts: 188
Location: Ukraine,Kharkov
dead_body
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.
Post 30 Jul 2006, 06:43
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-2019, Tomasz Grysztar.

Powered by rwasa.