[A83] Re: Macros vs. call/ret
[Prev][Next][Index][Thread]
[A83] Re: Macros vs. call/ret
Although the code can be harder to understand if you use macro's,
if it is documented correctly, it might make understanding/programming easier.
(Not stating this as a fact, but merely as a personal preference everyone has
to decide for himself)
To give options to a routine it might be more useful, otherwise it's less
usefull.
Example:
to draw a pixel at coordinates xx and yy, one would do:
ld b,xx
ld c,yy
bcall(_ipoint);
if one does this a lot of times, e.g. 4-6, it might be more legible to do the
following:
#define setpixel(xx,yy) ld b,xx \ ld c,yy \ bcall(_ipoint) ;bcall(_point)
;is macro in macro.
one would then do:
setpixel(xx1,yy1)
setpixel(xx2,yy2)
setpixel(xx3,yy3)
...
just my thoughts on the subject,
--Peter Martijn
>
> 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?
>
>
>
>
References: