News:

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

pointer to struct

Started by Artoo, October 06, 2005, 11:20:14 PM

Previous topic - Next topic

Artoo

I need help with accessing members of a structure then the structure is passed into a procedure/function as a pointer.

eg,

MyObject struct
   m_dwFlags            DB ?
   m_strName            DB 50 dup(?)
MyObject ends


MyFunc PROC    pObject:ptr MyObject

   INT 3
   mov edx,pObject
   assume edx:ptr MyObject
   invoke MessageBox,NULL, ADDR [edx].m_strName, NULL, MB_OK      ;this works, as the dissassembled code is LEA         eax,[edx]
   invoke MessageBox,NULL, ADDR pObject.MyObject.m_strName, NULL, MB_OK   ;this does not work as the dissassembled code is LEA eax,[pObject]
   assume edx:nothing
   RET

MyFunc  ENDP

How can I get the second invoke of the messagebox to be:   MOV EAX, pObject instead of LEA eax,[pObject]

or in other words:
How can I access m_strName directly, without using EDX?

Pleease help!

QvasiModo

Quote from: Artoo on October 06, 2005, 11:20:14 PM
How can I get the second invoke of the messagebox to be:   MOV EAX, pObject instead of LEA eax,[pObject]

Well, pObject.whatever means that pObject is a structure, rather than a pointer to a structure. That's why MASM is generating such code. If you had used another struct member (not the one on top) the code would have been lea eax, [pObject + 4] or something similar.

Quote from: Artoo on October 06, 2005, 11:20:14 PM
How can I access m_strName directly, without using EDX?

You can't. You can pass the pointer directly, but to add or substract something first it has to be loaded into a register first. What you can do is use one of the registers normally preserved by API calls (like EBX, ESI or EDI), but don't forget to preserve it yourself. An example:


MyObject struct
   m_dwFlags            DB ?
   m_strName            DB 50 dup(?)
MyObject ends


MyFunc PROC uses ebx pObject:ptr MyObject

   INT 3
   mov ebx,pObject
   pushcontext
   assume ebx:ptr MyObject
   invoke MessageBox,NULL, ADDR [edx].m_strName, NULL, MB_OK
   invoke MessageBox,NULL, ADDR [ebx].m_strName, NULL, MB_OK
   popcontext
   RET

MyFunc  ENDP