News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Variables In Memory Can't Be Dereferenced - Only Registers?

Started by Fred Harris, January 09, 2016, 07:33:48 AM

Previous topic - Next topic

Fred Harris

Can only addresses held in a register be dereferenced? I have a test IncrOne() proc like so which is just supposed to increment by one the number passed into it, only thing is, I'm passing in a pointer to the number, rather than the number itself...


IncrOne proc pNum:DWORD
  mov eax, [pNum]         ; pNum is a pointer to the number I wish to increment
  add eax, 1              ; in my start: code I had a dwNumber variable set to 5
  ret                     ; I passed in to here the address of that
IncrOne endp


The above doesn't work -  pNum comes into the above procedure holding a virtual memory address such as 4206832.  I thought I could dereference that parameter by enclosing it in square brackets, i.e., '[]'.  But my debug output is showing me, for example, 4206833 after the operation.  The original number in my test above was 5.  I put these variables in my uninitialized data segment ...


.data?
dwNumber dd ?
pNumber  dd ?


...and after start: did this...


mov dwNumber, 5              ; mov 5 into memory variable dwNumber
lea eax, OFFSET dwNumber     ; load eax with address of memory variable dwNumber
mov pNumber, eax             ; load memory variable pNumber with address of dwNumber held in eax
push pNumber                 ; push pNumber on stack for retrieval in proc InCrOne
call IncrOne                 ; call InCrOne proc


And as I said...it doesn't work.  From Hutch's "asmintro.chm" he has...

Using square brackets around EAX gives access to the information at the address in EAX. This is the case with any 32 bit register. A register enclosed in square brackets is effectively a memory operand.

So the critical component of that is a 'register enclosed in square brackets' apparently.  It doesn't work with a variable in memory apparently.  Am I coming to the right conclusions here?

I did get it to work by doing this...


IncrOne proc pNum:DWORD
  mov eax, pNum
  mov eax, [eax]
  add eax, 1
  ret
IncrOne endp


...which is what is shown in Hutch's chm mentioned above.

jj2007

It is easier to push the offset:

include \masm32\include\masm32rt.inc

.data
dwNumber dd 5

.code
start:
  push offset dwNumber    ; push a pointer to dwNumber on stack
  call IncrOne                 ; call InCrOne proc
  MsgBox 0, cat$(str$(eax), " is the result"), "Hi:", MB_OK
  exit

IncrOne proc pNum:DWORD
  mov eax, pNum
  mov eax, [eax]
  add eax, 1
  ret
IncrOne endp

end start

Fred Harris