News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

offsetof MACRO

Started by habran, June 30, 2014, 05:29:19 PM

Previous topic - Next topic

habran

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

 
Cod-Father

qWord

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


:t
MREAL macros - when you need floating point arithmetic while assembling!

habran

Cool  8)
You said more with less words :t
My intention was to show the steps what is actually happening :biggrin:
Cod-Father

Gunther

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
You have to know the facts before you can distort them.

dedndave

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:

Gunther

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
You have to know the facts before you can distort them.

habran

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:
                         

what do you say dedndave 8)
Cod-Father

jj2007

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
;)

qWord

The trick with macros is to get around MASM's bugs  :biggrin:

MREAL macros - when you need floating point arithmetic while assembling!

habran

maybe this can help :P
                                                                               
Cod-Father

dedndave

qWord is a macro wizard
however, that icon is already in use by Tom Bleeker   :P

http://www.madwizard.org/

habran

Damn!
What about this one:
                                 
Cod-Father

dedndave

"Nothin' up my sleeve!"


habran

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
Cod-Father

hutch--

> The trick with macros is to get around MASM's bugs

We generally call them undocumented features.  :icon_mrgreen: