Labels in macros

Bare metal programming in PureBasic, for experienced users
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Labels in macros

Post 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.
BERESHEIT
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Labels in macros

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Labels in macros

Post 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?
BERESHEIT
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Labels in macros

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Labels in macros

Post 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.
BERESHEIT
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Labels in macros

Post by STARGÅTE »

You can use MacroExpandedCount:

Code: Select all

Macro example
	!Location#MacroExpandedCount:
EndMacro


example ; Location1

example ; Location2

example ; Location3
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Labels in macros

Post 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.
BERESHEIT
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Labels in macros

Post by coco2 »

Nice to know, thanks
Post Reply