Author Topic: Nested pointers Q's  (Read 3144 times)

xoreaxeax

  • Regular Member
  • *
  • Posts: 10
Nested pointers Q's
« on: May 25, 2012, 06:02:41 AM »
I always get confused with the proper combination of lea's and movs I should use to access pointers that are referenced by another pointer, is there a trick to keeping track of them?  Say I have like 4 calls to HeapAlloc and somewhere in each memory block would be a pointer to the next memory block, how would I say access a value in the last memory block?

qWord

  • Member
  • *****
  • Posts: 1454
  • The base type of a type is the type itself
    • SmplMath macros
Re: Nested pointers Q's
« Reply #1 on: May 25, 2012, 06:27:34 AM »
you must dereference the pointer four times:
Code: [Select]
.data
    pMem PVOID ?
.code
...
mov eax,pMem  ; mov pointer of first block into eax
mov eax,[eax]   ;  load pointer of block 2
mov eax,[eax]   ;  ... block 3
mov eax,[eax]   ; ... block 4
mov eax,[eax]   ; obtain value
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

  • Member
  • *****
  • Posts: 7548
  • Assembler is fun ;-)
    • MasmBasic
Re: Nested pointers Q's
« Reply #2 on: May 25, 2012, 06:30:19 AM »
There is no limit to nesting, but you have to keep track, well, by commenting well your code. If it's nested deeply, it's probably bad design. Of course, ...
mov eax, pHeapTop
mov eax, [eax]
mov eax, [eax]
mov eax, [eax]
... may produce the right result, but what for?

By the way, a simple but complete example usually helps to get concrete answers ;-)

P.S.: Funny that qWord uses exactly the sequence I had just written...

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4809
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Nested pointers Q's
« Reply #3 on: May 25, 2012, 03:30:32 PM »
 :biggrin:

JJ,

Long ago a philosopher said, show me a man who had a vision in a dream and I will show you a man who dreamed he had a vision.

> P.S.: Funny that qWord uses exactly the sequence I had just written...

It may be the case that you used exactly the same sequence as qWord.  :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

tenkey

  • Regular Member
  • *
  • Posts: 10
Re: Nested pointers Q's
« Reply #4 on: May 25, 2012, 07:22:43 PM »
If you're creating a chain of blocks, something like the following...

Code: [Select]
memblock struct
nextblock dword ?  ; pointer to next memblock
datablock dword 127 dup(?)
memblock ends

; store NULL in "nextblock" field in the last block

getlast:
    mov eax,pStartChain
    cmp eax,NULL
    je  endsearch  ; EAX is NULL
findlast:
    mov ecx,[eax].memblock.nextblock
    cmp ecx,NULL
    je  endsearch  ; EAX points to last block
    mov eax,ecx
    jmp findlast
endsearch:
    ret