Macros !!!!!

Share your advanced PureBasic knowledge/code with the community.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Macros !!!!!

Post by DoubleDutch »

Code updated for 5.20+ (same as PureBasic's Macro)

Yep - PureBasic actually already has macros!!!!

Because PureBasic's assembler is FASM - you can use the features of fasm to create macros!!!

Code: Select all

!macro test
!{
  MessageRequester("test","hello")
!}


!test
!test
This will make a macro called "test", by coding !test twice you will get two message requesters displayed one after the other!

-Anthony
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Wow! Awsome!

Hey Fred! Would it be safe to just use this as PureBasic's "macro"
Or is there possible issues?

This is cool, but I'm unsure how the compiler likes it.
But if Fred say it's cool, then we got a PB macro all ready to go,
and Fred don't have to write a single line of code,
just add a macro section to the documentation.
:lol:
Tension
User
User
Posts: 29
Joined: Tue Mar 22, 2005 3:19 pm

Post by Tension »

@DoubleDutch

Yes! Thanks!

And this would be probably remain valid for as long as PuerBasic uses fasm.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Macros !!!!!

Post by PB »

That's fantastic, and simple to use, too. :) In fact, the syntax is so pure
that I don't see why Fred would even need to add a native version of it.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

May I say orgasmic!!? because it is. But although, why some of you guys would kill for macros? What do they have that I dont know of? :P

Oh by the way if I call !test I get assembler error [70] error invalid operand. but if I use just test it works ok. why is this? im an ASM nooblet . :lol: Using 3.91 maybe thats the reason of my problem. ill update soon then.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

If a Guru says its Fantastic and a Wizard says its Orgasmic then why am I still an Apprentice? hehe ;)

-Anthony
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Tension
User
User
Posts: 29
Joined: Tue Mar 22, 2005 3:19 pm

Post by Tension »

lol!

At least you have a label. I'm still an undefined reference.
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

@dagcrack,

There should be a space between '!' and 'test' as follows:

Code: Select all

!macro test
!{
  MessageRequester("test","hello")
!}


! test
! test
Npath
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

dagcrack wrote:May I say orgasmic!!? because it is. But although, why some of you guys would kill for macros? What do they have that I dont know of? :P

Oh by the way if I call !test I get assembler error [70] error invalid operand. but if I use just test it works ok. why is this? im an ASM nooblet . :lol: Using 3.91 maybe thats the reason of my problem. ill update soon then.
Heh, in that sample, doing the same in a procedure would give the same result :) What's the big thing about macros? :D
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

I second that notion. What is the difference between the flat assembly macros and regular Pure procedures?

Npath
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Thats basically what Im asking here.

@npath: Oh I didnt knew! lets blame the double dutch guy! :P
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: Macros !!!!!

Post by traumatic »

AFAIK you can't use params with that approach, can you? :wink:
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

npath: Without a space works okay here - but it should also work with a space... :)

Shannara: In that example yes, but that was just a proof of concept. Macros expand code - but (normally) give more rapid execution. There are cases where code can actually slow down when expanded, bit this is kinda obvious and also depends on the processor used. I have worked on processors where cache was on 4k boundries! If your code goes over a 4k address boundry then you get a massive slow down!!!

npath: Do a procedure with more code in it and call it a few times, then do it via the macro method - time both... The size of the executable with the Procedure will be smaller, but slower (under normal conditions). The macro version will have a larger executable, but will be faster (again, under normal conditions)...

traumatic: Ask a Wizard or a Guru! ;)

There may be a way... do some experiments, take a few guesses. Don't forget that you can use the features of Fasm :D

-Anthony
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
S.M.
Enthusiast
Enthusiast
Posts: 118
Joined: Sat Apr 24, 2004 1:11 pm
Contact:

Post by S.M. »

@traumatic
AFAIK you can't use params with that approach, can you?
It is possible:

Code: Select all

!macro ErrText string{
!JMP $+37
MessageBox_(0,0,0,0)
!Push 48
!Push 0
!Push [v_#string]
!Push 0
!CALL _MessageBoxA@16
!}


a$="Hello !"
!ErrText a$
Another nice example: :wink:

Code: Select all

!macro DISASM Count{
!PUSH ECX
!JMP $+19
DisASMCommand(0)
!MOV Ecx,Count
!MOV Eax,$+55
!PUSH ECX
!Call PB_DisASMCommand
!PUSH EAX
Debug GetDisASMString() 
!POP EAX
!POP ECX
!LOOP $-47
!POP ECX
!}


!DISASM 3 ;Disassembles the next 3 ASM-Commands
A=5
regards
Stefan
leo1991
New User
New User
Posts: 7
Joined: Tue Feb 24, 2004 7:25 pm

Post by leo1991 »

Macros are faster :D

Code: Select all

!macro test 
!{ 
bla / 5 * 99
d = 70 * 100000 / (90 + 700)
l = a > c
d * 1000
!} 

Procedure test()
    bla / 5 * 99
    d = 70 * 100000 / (90 + 700)
    l = a > c
    d * 1000
EndProcedure

#N = 100000000
t1 = GetTickCount_()
For I = 0 To #N
    ! test 
Next
t1 = GetTickCount_() - t1

t2 = GetTickCount_()
For I = 0 To #N
    test()
Next
t2 = GetTickCount_() - t2

out.s = "macro: "+Str(t1)+#CRLF$
out.s + "proc: "+Str(t2)

MessageRequester("result",out)
Post Reply