News:

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

offset and ptr

Started by allynm, July 21, 2009, 01:43:01 AM

Previous topic - Next topic

sinsi

One thing that I wondered about is RegisterClassEx and CreateWindowEx. The first takes a pointer to a structure and the other seemingly pushes a lot of structure members.

I guess it depends on how you use the structure - a bit like vb's byval and byref (yeah, I know...vb :shrug:). Read-only versus read/write.
It gets expensive as far as stack goes if your proc passes those structure values to another proc, and another...a pointer is better.
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: hutch-- on July 21, 2009, 09:26:36 AM
The main advantage of using a construction like,


MyProc proc MyPtr:PTR SomeStruc


Is that you can use the structure member names when addressing parts of the structure that had its address passed.


That's exactly what we are looking for. Can you give us an example? One without assume and a register?

sinsi

jj, you always need a register to hold the 'base' address passed as a parameter.
Without an assume, you would use something like '(SomeStruc ptr [eax]).Member5'
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

#18
Quote from: sinsi on July 21, 2009, 10:48:27 AM
jj, you always need a register to hold the 'base' address passed as a parameter.
Without an assume, you would use something like '(SomeStruc ptr [eax]).Member5'

Indeed. But what's the point then to use PTR SomeStruc? This works equally well:

MyProc proc MyPtr:PTR EM_CHARFORMAT
  mov edx, MyPtr
  assume edx: PTR SomeStruc

  invoke MessageBoxA, 0, str$([edx].Member1), addr appTitle, MB_OK
  ret
MyProc endp


EDIT: Here is a minimalistic example showing the difference between passing a pointer to structure and passing the structure itself. For Masm, MyPtr is simply a DWORD...

include \masm32\include\masm32rt.inc

.code
myrect RECT <123, 456, 111, 222>

TestMeP proc MyPtr
  mov edx, MyPtr
  print str$( [edx.RECT.top] ), " is the top of the rectangle", 13, 10
  ret
TestMeP endp

TestMeS proc MyStruc:RECT
  print str$(MyStruc.top), " is the top of the rectangle", 13, 10
  ret
TestMeS endp

start:
  invoke TestMeP, offset myrect
  invoke TestMeS, myrect
  exit

end start


Edit(2): Removed superfluous brackets in TestMeS.

hutch--

jj,

Without writing a test piece, I would try using the struct name with its appended member name and see if it builds OK.

MyPtr:PTR SomeStruc

mov eax, MyPtr.member1  ; assuming member1 is a DWORD.

If this does not work then there is no advantage in using the PTR structname.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

allynm

My thanks to all of you who have generously contributed to this question.  I am trying hard to absorb your knowledge.

Mark A.