[A83] Re: Macros vs. call/ret


[Prev][Next][Index][Thread]

[A83] Re: Macros vs. call/ret




Good question.  This is a similar debate among C/C++ programmers, although
there are less issues here.  The only functional difference in assembly
language is size versus speed.  A good example would be a routine to load
the address that HL points to into HL:

; hl = (hl)
load_hl_ind:
 ld a,(hl)
 inc hl
 ld h,(hl)
 ld l,a
 ret

#define LOADHLIND ld a,(hl) \ inc hl \ ld h,(hl) \ ld l,a

This routine is four bytes long, not including the return, which is only
needed if it's a return.  A call takes three bytes.  So, each time you use
the macro, you use four bytes, whereas if you used a call, you would use
three bytes.  But, a call/ret takes 27 t-states.  Which is better?  If it's
a tight inner loop that is called a lot, say in a sprite or map drawing
routine, then it is better to use the macro (or just write it out).  But if
it's used in normal, non speed-critical code (almost all code), it's better
to call the routine (unless of course it's only used once in the program).
If a routine is longer than three bytes, you should be using a call/ret.

I would advise against using macro's unless you have a very good reason.
They are more confusing and hide what the code is really doing.  One of the
reasons I enjoy coding in PHP is that you don't have to worry about tons of
#defines and #ifdef's and all that other silliness.  In C++, you should
never use macro's anyway, unless it has to do with hacks to get around
cross-platform stuff (such as marking functions to be exported when building
a .dll with cygwin/mingw).  Macro's are bad voodoo magic.

> Ok, this may seem like a stupid question.
>
> Is there any benefit to using a macro instead of
> calling a routine in your program?






Follow-Ups: References: