Page 1 of 1

Labels in macros

Posted: Wed Dec 25, 2013 9:40 am
by netmaestro
I have this code:

Code: Select all

  Macro m_Get_cur_code()
    !mov eax, 1
    !mov cl, byte [p.v_codesize]
    !shl eax, cl
    !sub eax, 1
    !mov ecx, [p.v_index]
    !mov edx, [ecx]
    !mov cl, byte [p.v_offset]
    !shl eax, cl
    !And edx, eax
    !shr edx, cl
    !mov [p.v_cur_code], edx
    !mov eax, [p.v_offset]
    !add eax, [p.v_codesize]
    !mov [p.v_offset], eax
    While offset>=8
      index+1
      offset-8
      bytesleft-1
    Wend
  EndMacro
As you can see the last part of the logic is a small loop written in PB code. I tried writing that section in asm and it worked fine once but the problem is the only way I know how to test-and-branch is using labels. In a procedure this would be no issue but if I put a label in this macro, the second time it gets called it will error out on "duplicate label" and I'm dead. This is with PB labels or assembler labels, same thing. Surely there must be a way to do this loop in a macro without hitting this problem. Can anyone point out how? Also, any tips on optimizing this for speed would be appreciated as I'm not very strong in asm.

Re: Labels in macros

Posted: Wed Dec 25, 2013 9:55 am
by wilbert
The Windows and Linux versions of PureBasic use FASM for assembler.
In that case you can use anonymous labels. @@ @f @b
The problem is that the OS X version of PureBasic uses YASM for assembler which doesn't support this syntax.

Re: Labels in macros

Posted: Wed Dec 25, 2013 10:03 am
by netmaestro
Thanks for the answer, it's helpful. Is there any other choice for making the whole macro in assembler or do I have to leave the PB code there if I want to hold out hope for the program one day working on Mac?

Re: Labels in macros

Posted: Wed Dec 25, 2013 10:32 am
by wilbert
netmaestro wrote:Thanks for the answer, it's helpful. Is there any other choice for making the whole macro in assembler or do I have to leave the PB code there if I want to hold out hope for the program one day working on Mac?
There might be other ways with YASM that you could use with a CompilerIf .
My first suggestion is that you try it on windows and compare the speed with the PB code.
ASM can be much faster but this isn't always the case. If it's not faster, there's no point in using ASM.

Re: Labels in macros

Posted: Wed Dec 25, 2013 10:41 am
by netmaestro
True, that part of the code won't benefit from being written in asm. The PB version of the simple loop that executes a maximum of two times and often not at all will be just as fast. It's just that I thought I might hit that problem sometime in future when it will matter and I won't know how to proceed. But the anonymous labels are working for me, I just won't be putting them in this program for the sake of possible Mac support later.

Re: Labels in macros

Posted: Wed Dec 25, 2013 12:22 pm
by STARGĂ…TE
You can use MacroExpandedCount:

Code: Select all

Macro example
	!Location#MacroExpandedCount:
EndMacro


example ; Location1

example ; Location2

example ; Location3

Re: Labels in macros

Posted: Sat Dec 28, 2013 11:36 pm
by netmaestro
Thanks STARGATE! I didn't know about that command at all. But I don't use a lot of macros either. What a great idea MacroExpandedCount is. Perfect for what I want to do.

Re: Labels in macros

Posted: Fri Jun 13, 2014 6:28 am
by coco2
Nice to know, thanks