News:

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

A struct alias for register holding an address ?

Started by dsouza123, July 20, 2008, 03:07:32 PM

Previous topic - Next topic

dsouza123

What techniques are available with MASM for using a register
holding an address to a structure, to access the items in the structure by name ?

This wasn't a declared variable in the .data or .data? sections.

An example, eax has the address to a hostent struct
which is allocated by the gethostbyname API call


hostent STRUCT
  h_name      DWORD      ?
  h_alias     DWORD      ?
  h_addr      WORD       ?
  h_len       WORD       ?
  h_list      DWORD      ?
hostent ENDS


MASM

        mov     eax, [eax + 12]  ; would like to use name of element in struct
        mov     eax, [eax]       ; instead of 12
        mov     eax, [eax]
        bswap   eax


FASM

        virtual at eax
          .host   hostent
        end     virtual
        mov     eax,[.host.h_addr_list]
        mov     eax,[eax]
        mov     eax,[eax]
        bswap   eax


FASM has the the virtual construct which allows an alias of sorts
for eax, in this case it overlays a variable .host of type struct hostent
allowing access to the individual elements of the struct by name.

BasilYercin

MASM...

mov  Ecx,[Ebx + NMHDR.code]

mov Ecx, (NMHDR ptr [ebx]).code


Tedd

'assume' is probably the closest to what you're looking for:

assume eax:ptr hostent        ;say eax points to a hostent struct
mov     eax, [eax].h_list
assume eax:nothing            ;remove any further 'assumptions' about eax (or the next lines will cause warnings)
mov     eax, [eax]
mov     eax, [eax]
bswap   eax


Or, if you're only using it once, you can be more direct:

mov     eax, [eax].hostent.h_list
mov     eax, [eax]
mov     eax, [eax]
bswap   eax

No snowflake in an avalanche feels responsible.

dsouza123

Thanks, BasilYercin and Tedd all four ways worked.

jj2007

For me, this one is the most intuitive version.

.data?
MyHos hostent <?>

.code
mov edx, offset MyHos
mov eax, [edx.hostent.h_name]
mov ecx, [edx.hostent.h_alias]

alanabbott

Many thanks to all, ASSUME seams to be the answer to my quest, passing an external structure to a dynamically loaded DLLs and their work areas to make then totally reentrant.

Alan