News:

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

Working with pointers

Started by lamer, April 20, 2005, 08:46:35 PM

Previous topic - Next topic

lamer

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.

Petroizki

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]

lamer


AeroASM

Also the Rtl functions are very slow; it is better to use rep stosb and rep movsb

roticv

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.

AeroASM

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

Mirno

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

lamer

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...

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MazeGen

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.