The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: lamer on April 20, 2005, 08:46:35 PM

Title: Working with pointers
Post by: lamer on April 20, 2005, 08:46:35 PM
Hi all!
Some windows messages return pointers as one of params - for example WM_MEASUREITEM returns pointer to MEASUREITEMSTRUCT structure in lParam. As VB programmer I am not familiar with pointers, so I do as following:

LOCAL mit:MEASUREITEMSTRUCT
;...any code
.elseif umsg==WM_INITDIALOG
   invoke RtlMoveMemory,addr mit,lParam,sizeof MEASUREITEMSTRUCT
   ;code
   invoke RtlMoveMemory,lParam,addr mit,sizeof MEASUREITEMSTRUCT
   invoke RtlZeroMemory,addr mit,sizeof MEASUREITEMSTRUCT
   mov eax,TRUE
   ret
;code

It works, but I am sure there is much more efficient and elrgant way to work with pointers.
Title: Re: Working with pointers
Post by: Petroizki on April 21, 2005, 05:20:54 AM
With pointer, you can do direct refers to that memory address. And in your case, there is no need to make a copy of the structure.

The lParam is just a memory address, of where the MEASUREITEMSTRUCT resides. You can retrieve data from the structure for example like this:
mov edx, lParam
mov eax, [edx].MEASUREITEMSTRUCT.itemWidth ; returns an DWORD from memory address pointed by [edx + 12]
mov ecx, [edx].MEASUREITEMSTRUCT.itemHeight ; returns an DWORD from memory address pointed by [edx + 16]
Title: Re: Working with pointers
Post by: lamer on April 21, 2005, 07:03:24 PM
Thank you! :U
Title: Re: Working with pointers
Post by: AeroASM on May 05, 2005, 07:43:32 AM
Also the Rtl functions are very slow; it is better to use rep stosb and rep movsb
Title: Re: Working with pointers
Post by: roticv on May 05, 2005, 01:10:50 PM
I just took a look at RtlZeroMemory. It is not as slow as you think it is and it uses both rep stosd and rep stosb. However I think it would be better to use mmx/sse to clear the memory if the data is sufficiently big.
Title: Re: Working with pointers
Post by: AeroASM on May 05, 2005, 01:41:17 PM
Why do you always prove me wrong?? (joking)

I have a feeling that mmx/sse is quite slow to move data around.

mov takes about one clock tick for a dword, whereas movdqa takes about 7 clock ticks for an oword
Title: Re: Working with pointers
Post by: Mirno on May 05, 2005, 02:27:26 PM
The latency is around 7 or 8 clocks yes, but throughput is one per two clocks (assuming you are bursting writes).
The memory controller will allow several writes to be in flight at any one time, so the latency becomes less and less of an issue the longer the "burst" being performed is.

You will see best performance using movdqa & SSE, MMX (using movq) will beat rep stosd.

Mirno
Title: Re: Working with pointers
Post by: lamer on May 05, 2005, 07:19:26 PM
I just get pleasure from your discussion while writing code in Visual Basic :lol
Difference between Rtl functions and stos...From the VB standpoint... :green2
Well, we are on asm board - so I will ask experts: does this macro seem correct

EmptyString macro string,bytes
push edi
xor eax,eax
lea edi,string
mov ecx,bytes
rep stosb
pop edi
endm


Please, judge me leniently, it is my fist-born...
Title: Re: Working with pointers
Post by: hutch-- on May 05, 2005, 07:22:49 PM
lamer,

You are doing fine, REP STOSB is not really up to pace but its still a lot faster than many alternatives so you have done well.
Title: Re: Working with pointers
Post by: MazeGen on May 05, 2005, 07:51:47 PM
If you are interested in macros, take a look at OPATTR operator - use it for argument checking:


IF ((OPATTR (string) EQ 0) OR ((OPATTR (string)) AND 10000011y) ; forward-referenced label, code label,
; memory expression or data label, or external label?
   lea edi,string
ELSE ; it is immediate expression or register
  IFDIFI <string>, <EDI> ; don't load edi onto itself
    mov edi,string
  ENDIF
ENDIF


This way you can make more stable macros.

Note: forward-referenced label always returns a 0 and that's why I assume it is a label; if it will not be a label, the assembler says "undefined symbol: xxx". I don't know any better way how to handle it.