EvilZone

Programming and Scripting => Assembly - Embedded => : parad0x October 12, 2013, 05:43:25 PM

: [Asm] what are pros/cons of using macros over procedures?
: parad0x October 12, 2013, 05:43:25 PM
I am learning asm on windows using masm. I know that macros execute faster than  procedures but I don't know the pros and cons  of using macros over procedures. Just want to know where should I use macros and where procedures? I may not be able to reply because of some technical  reasons but I regularly check replies to my posts.
: Re: [Asm] what are pros/cons of using macros over procedures?
: ca0s October 12, 2013, 07:42:57 PM
Macro: it expands inline. E.g.
:
macro herp
    instruction1
    ...
    instructionN
end macro
:
main:
    asd
    fgh
    herp <---
    jkl
    qwe
    herp <---
Will expand to:
:
main:
    asd
    fgh
    instruction1
    ...
    instructionN
    jkl
    qwe
    instruction1
    ...
    instructionN

With procedures, instead:
:
proc derp
    instruction1
    ...
    instructionN
end proc
:
main:
    asd
    fgh
    herp <---
    jkl
    qwe
    herp <---
Will turn into:
:
main:
    asd
    fgh
    call herp <---
    jkl
    qwe
    call herp <---

herp:
    instruction1
    ...
    instructionN
    ret
 

It depends on:
- Your task size
- Number of different places you will be using it from

So if your task is composed by only a few instructions which are not worth the overhead of a function call (set up stack frame, jump, destroy stack frame, return...), use a macro. If it is a more involved task, some long operation, and you will be calling it from a lot of different places  then I'd probably go for the procedure, since macro-ing it would result in a much bigger code