Author Topic: [Asm] what are pros/cons of using macros over procedures?  (Read 1819 times)

0 Members and 1 Guest are viewing this topic.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
[Asm] what are pros/cons of using macros over procedures?
« 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.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: [Asm] what are pros/cons of using macros over procedures?
« Reply #1 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