News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Confused with MASM Macro's

Started by Kyle, February 19, 2010, 04:06:42 PM

Previous topic - Next topic

Kyle

I've been looking over optimized MASM code for sin, cos, etc. I've read into macro's before, and I'm used to seeing the params defined. But for the code from bMath, they don't use the usual format it seems. For example in the following code:

bSin        MACRO
      fist  _tmp1
      mov   edi, _tmp1
      fisub _tmp1             ;  TOS = signed fractional
      rol   edi, 4            ;  now edi is the adjusted
      and   edi, 8            ;   sign index (0 or 8)
      mov   esi, _tmp1
      and   esi, 03ffh
      rol   esi, 3
      sub   esi, edi
      fmul  _bs [esi]
      add   esi, edi
      fadd  _bsin [esi]       ;  Sin(BittiansIn) in TOS
ENDM


I've checked to see where _tmp1, _bs, and _bsin were defined, but I can't find them anywhere in the code. Am I just too C'd and I am misunderstanding something about MASM? Or what am I missing?

For anyone who hasn't ever seen bMath, I attached it. Its from http://www.bmath.net/

hutch--

Kyle,

They are probably globals but you can emulate them just by working out the data size.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

BogdanOntanu

A macro is expaned in the middle of the code where it is used (practically insterted with some extra parsing done to match arguments and locals). Hence a macro can make use of adiacent code's defined variables like any other "typed in" code can... if this is the wanted behaviour.

This kind of macro is considered a bad programming style but sometimes it is usefully. And you can do it in C also.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

Kyle

Well I'm guessing theyre DWORD's, but I suppose what I'm really confused about is how params are passed to it since it isn't setup.

jj2007

Quote from: Kyle on February 19, 2010, 06:28:41 PM
Well I'm guessing theyre DWORD's, but I suppose what I'm really confused about is how params are passed to it since it isn't setup.

A macro does not necessarily have parameters. It is just an alias for some lines that perform a repetitive task. In this case, as Hutch pointed already out, it works on a number of variables whose names have been defined elsewhere. They can be global or local.

BogdanOntanu

Quote from: Kyle on February 19, 2010, 06:28:41 PM
Well I'm guessing theyre DWORD's, but I suppose what I'm really confused about is how params are passed to it since it isn't setup.

The macro has NO parameters. Hence no parameters are actually passed to it.

Instead of using parameters the macro makes use of  some varibales already defined in code (this use is by refering to their names).

Of course that those variables (named exactly as the macro expects them) MUST exist when the macro is used but other than this there are no parameters defined.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

MichaelW

Quote from: Kyle on February 19, 2010, 04:06:42 PM
I've checked to see where _tmp1, _bs, and _bsin were defined, but I can't find them anywhere in the code. Am I just too C'd and I am misunderstanding something about MASM? Or what am I missing?

They are defined in Bmath.inc
eschew obfuscation