It is currently Sat Dec 05, 2020 12:52 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Labels in macros
PostPosted: Wed Dec 25, 2013 9:40 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8144
Location: Fort Nelson, BC, Canada
I have this code:
Code:
  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.

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject: Re: Labels in macros
PostPosted: Wed Dec 25, 2013 9:55 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3707
Location: Netherlands
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.

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Labels in macros
PostPosted: Wed Dec 25, 2013 10:03 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8144
Location: Fort Nelson, BC, Canada
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?

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject: Re: Labels in macros
PostPosted: Wed Dec 25, 2013 10:32 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3707
Location: Netherlands
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.

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Labels in macros
PostPosted: Wed Dec 25, 2013 10:41 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8144
Location: Fort Nelson, BC, Canada
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.

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject: Re: Labels in macros
PostPosted: Wed Dec 25, 2013 12:22 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1321
Location: Germany, Glienicke
You can use MacroExpandedCount:

Code:
Macro example
   !Location#MacroExpandedCount:
EndMacro


example ; Location1

example ; Location2

example ; Location3

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: Labels in macros
PostPosted: Sat Dec 28, 2013 11:36 pm 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8144
Location: Fort Nelson, BC, Canada
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.

_________________
Veni, vidi, vici.


Top
 Profile  
Reply with quote  
 Post subject: Re: Labels in macros
PostPosted: Fri Jun 13, 2014 6:28 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 5:38 am
Posts: 254
Location: Australia
Nice to know, thanks


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye