EvilZone

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

Title: [Asm] what are pros/cons of using macros over procedures?
Post by: parad0x on 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.
Title: Re: [Asm] what are pros/cons of using macros over procedures?
Post by: ca0s on October 12, 2013, 07:42:57 PM
Macro: it expands inline. E.g.
Code: [Select]
macro herp
    instruction1
    ...
    instructionN
end macro
Code: [Select]
main:
    asd
    fgh
    herp <---
    jkl
    qwe
    herp <---
Will expand to:
Code: [Select]
main:
    asd
    fgh
    instruction1
    ...
    instructionN
    jkl
    qwe
    instruction1
    ...
    instructionN

With procedures, instead:
Code: [Select]
proc derp
    instruction1
    ...
    instructionN
end proc
Code: [Select]
main:
    asd
    fgh
    herp <---
    jkl
    qwe
    herp <---
Will turn into:
Code: [Select]
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