The MASM Forum

General => The Workshop => Topic started by: habran on June 30, 2014, 05:29:19 PM

Title: offsetof MACRO
Post by: habran on June 30, 2014, 05:29:19 PM
I was googling the net to find something about the C macro offsetof and as usual MSDN is stingy to give some acceptable info and example
I was curious if someone ever translated it to ASM and I found nothing
so I decided to shad lights on it
In masm there is no need for that macro because it has it built in,
however I wrote one for those who were not clear what is it about
especially for C programmers who want to learn assembly language 

;defined in stddef.h
;#define offsetof(s,m)   (size_t)( (ptrdiff_t)&(((s *)0)->m) )
;s = struct
;m = member
;returns offset of a member in struct
;E.G.
;   mov edx, offsetof(MYFRAME,EditWindow)
;it is a same as
;   mov edx, MYFRAME.EditWindow

;struct MYFRAME
; rcFrame RECT <>
; FrameWindow HWND
; EditWindow  HWND
; rcEdit RECT <>
;ends

offsetof MACRO s,m
sm EQU <>   
    sm CATSTR sm,<s>,<.>,<m>
    EXITM<sm>
ENDM

 
Title: Re: offsetof MACRO
Post by: qWord on June 30, 2014, 07:28:45 PM
offsetof MACRO s,m
    EXITM <s.&m>
ENDM


:t
Title: Re: offsetof MACRO
Post by: habran on June 30, 2014, 10:15:49 PM
Cool  8)
You said more with less words :t
My intention was to show the steps what is actually happening :biggrin:
Title: Re: offsetof MACRO
Post by: Gunther on July 01, 2014, 02:10:28 AM
Habran,

Quote from: habran on June 30, 2014, 10:15:49 PM
Cool  8)
You said more with less words :t
My intention was to show the steps what is actually happening :biggrin:

You should know that qWord is a really macro magician. Some of his tricks border on witchcraft.

Gunther
Title: Re: offsetof MACRO
Post by: dedndave on July 01, 2014, 02:12:44 AM
qWord understands the syntax - a few other members do too, of course

that's the whole trick to macros
it's not clearly explained in the masm manual

maybe we can get qWord to write a macro tutorial   :biggrin:
Title: Re: offsetof MACRO
Post by: Gunther on July 01, 2014, 02:19:17 AM
Quote from: dedndave on July 01, 2014, 02:12:44 AM
it's not clearly explained in the masm manual

maybe we can get qWord to write a macro tutorial   :biggrin:

That's an excellent idea. It would help a lot of members.

Gunther
Title: Re: offsetof MACRO
Post by: habran on July 01, 2014, 06:14:57 AM
C'mon Gunther,  witchcraft!? that reminds me on witch-hunt in medieval history :icon_eek:
It is not flattering ;)
Sorcery maybe or Shamanism or Magic :biggrin:
Hey qWord maybe you should change your avatar to this:
                          (http://upload.wikimedia.org/wikipedia/en/1/1f/MerlinDisney.gif)

what do you say dedndave 8)
Title: Re: offsetof MACRO
Post by: jj2007 on July 01, 2014, 06:48:46 AM
include \masm32\include\masm32rt.inc

offsetof MACRO s,m
    EXITM <s.&m>
ENDM

.code
start:
   print str$(offsetof(RECT, top)), " = "
   print str$(RECT.top)
   exit
end start
;)
Title: Re: offsetof MACRO
Post by: qWord on July 01, 2014, 07:21:07 AM
The trick with macros is to get around MASM's bugs  :biggrin:

(http://www.un.org.kh/images/stories/demining%20undp.jpg)
Title: Re: offsetof MACRO
Post by: habran on July 01, 2014, 09:23:32 AM
maybe this can help :P
                                                                                (http://upload.wikimedia.org/wikipedia/commons/thumb/8/87/FLIT_Spray_Can_1.jpg/440px-FLIT_Spray_Can_1.jpg)
Title: Re: offsetof MACRO
Post by: dedndave on July 01, 2014, 09:30:57 AM
qWord is a macro wizard
however, that icon is already in use by Tom Bleeker   :P

http://www.madwizard.org/ (http://www.madwizard.org/)
Title: Re: offsetof MACRO
Post by: habran on July 01, 2014, 09:39:18 AM
Damn!
What about this one:
                                 (http://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Nuremberg_chronicles_-_Merlin_%28CXXXVIIIr%29.jpg/440px-Nuremberg_chronicles_-_Merlin_%28CXXXVIIIr%29.jpg)
Title: Re: offsetof MACRO
Post by: dedndave on July 01, 2014, 10:00:41 AM
"Nothin' up my sleeve!"

(http://www.sellsbrothers.com/posts/Image/144)
Title: Re: offsetof MACRO
Post by: habran on July 01, 2014, 10:11:33 AM
dedndave you are a real wizard :bgrin:

hey jj2007 :t

I have a suggestion for hutch:
why not devote a topic for macros where people can contribute they own macros
Title: Re: offsetof MACRO
Post by: hutch-- on July 01, 2014, 04:08:48 PM
> The trick with macros is to get around MASM's bugs

We generally call them undocumented features.  :icon_mrgreen:
Title: Re: offsetof MACRO
Post by: jj2007 on July 01, 2014, 05:40:28 PM
Quote from: hutch-- on July 01, 2014, 04:08:48 PM
> The trick with macros is to get around MASM's bugs

We generally call them undocumented features.  :icon_mrgreen:

Hutch, these features are all documented now (http://www.japheth.de/JWasm/Manual.html#CHAPMASMBUGS).
Title: Re: offsetof MACRO
Post by: hutch-- on July 01, 2014, 11:16:31 PM
 :biggrin:

> Hutch, these features are all documented now.

Nah, this is just propaganda from the competition.

What makes me laugh is MASM was always a bad mannered old pig, that's part of its charm. Its task is to screw mnemonics together, not to hold the hand of the timid. You learn its "features" the hard way like normal and manipulate them to do the task you want. Now the ancient macro engine is an even bigger old pig with notation that was out of date in 1988 but interestingly enough if you massage it hard enough, you get results.
Title: Re: offsetof MACRO
Post by: qWord on July 01, 2014, 11:32:35 PM
Quote from: hutch-- on July 01, 2014, 11:16:31 PMNow the ancient macro engine is an even bigger old pig with notation that was out of date in 1988 but interestingly enough if you massage it hard enough, you get results.
:P

Still one of my favorites:
foo macro
echo did you see me?
EXITM <0>
endm

% ; foo()
Undocumented feature "expand macro function and discard return value"  :biggrin:
Title: Re: offsetof MACRO
Post by: jj2007 on July 01, 2014, 11:43:44 PM
Quote from: qWord on July 01, 2014, 11:32:35 PM
% ; foo()
Undocumented feature "expand macro function and discard return value"  :biggrin:

Similar (but quite useful):

include \masm32\include\masm32rt.inc

void MACRO args
  @CatStr(<;>, <args>)
ENDM

.code
start:
  void rv(MessageBox, 0, "Hello World", "It's all void:", MB_OK)

  exit
end start