Okay, just when I thought I knew what I was doing...

I'm working with a font, and need to diddle with the height parameter. I have a LOCAL logFont:LOGFONT on the stack, and I want to get the *contents* of lfHeight into eax.

I started off writing this:


mov eax, logFont.lfHeight


Which loaded the *address* of logFont.lfHeight:


;disassembly
00401309 mov eax,dword ptr [ebp-3Ch]


Um, not what I was expecting, but okay, I'll learn a lesson as soon as I figure out why it does that. For now, just dereference the pointer:



mov eax, [logFont.lfHeight]


Here is the disassembly:



00401309 mov eax,dword ptr [ebp-3Ch]


:confused: :confused: :confused:

I thought dereferenced the pointer var? In both cases, eax is getting something like 0012FEBCh, the address of the lfHeight member, and I can see that I'm *almost* there--the DWORD *at* that address is 0190h, the value I actually wanted to be in eax.

WTF?

-Chalain
Posted on 2002-04-24 17:07:03 by Chalain
I just use something like this:


mov eax,dword ptr[logFont.lfHeight]


Maybe someone else can give you a more technical definition, but that works.


I just did a real quick test on it:



mov logFont.lfHeight,190h
mov eax,dword ptr[logFont.lfHeight]
invoke wsprintf,addr lpBuffer,addr szFmt,eax
invoke MessageBox,hWin,addr lpBuffer,addr szDlgName,MB_OK
Posted on 2002-04-24 17:19:23 by Will
Okay, I just counted 60 bytes back from the stack and I see that 12FF08 really *is* at that point. The field I want is about 2 dwords away, so I must've horked something up somewhere along the way.

Here's the bulk of the function up to this point. What am I doing wrong?



;[EDIT] I just checked my inputs--pLogFont->lfHeight *is*
; correct and is passed in as follows:
LOCAL logFont:LOGFONT
;...setup logFont
invoke CreatePointFontIndirect, addr logFont, hdc

CreatePointFontIndirect proc pLogFont:DWORD, hdc:HDC
LOCAL logFont:LOGFONT
LOCAL pt:POINT
LOCAL ptOrg:POINT

int 3h
mov eax, hdc
test eax, eax ; is hDC NULL?
jnz @F
; yes, get screen DC
invoke GetDC, NULL
test eax, eax ; did GetDC fail?
jnz @F
ret ; return NULL (eax already contains 0)
mov hdc, eax
@@: ; Okay, hdc now has the correct HDC in it

; Copy passed-in logFont
lea edi, logFont
lea esi, [pLogFont]
mov ecx, sizeof LOGFONT
rep movsb

mov eax, dword ptr [logFont.lfHeight]


Thanks!

-Chalain
Posted on 2002-04-24 17:29:02 by Chalain
	lea edi, logFont ; this _is_ the memory

mov esi, [pLogFont] ; this _is_ a pointer to the memory
mov ecx, sizeof LOGFONT
rep movsb
Posted on 2002-04-24 17:49:47 by bitRAKE
*sigh* I think I see the problem, but I'm too dense to see the answer yet, Bitrake.

Should I do this, then:

mov edi, offset logFont

instead of the lea?

*sigh* I'm still all 'fused... with my luck I probably just proposed how to make *both* halves of the problem wrong...

Thanks,

-Chalain
Posted on 2002-04-24 18:05:40 by Chalain
I posted the solution:

mov esi, ; don't use LEA when you have a pointer.

When 'lea esi,' is assembled, you get 'lea esi,'. So, LEA calculates an address on the stack and puts it in ESI. You already have the address on the stack and should move it to ESI. Let me give some more examples - LEA can be confusing at first:


; these are the same result
mov esi,4
lea esi,[4]

; these are the same result
mov esi,eax
lea esi,[eax]

; these are the same result
mov esi, offset MyData ; in the data segment
lea esi,[MyData]
LEA is to calculate an effective address based on a potential access. I'm sorry if I'm making it sound more complex than it really is. It's really good to play with it in a debugger.
Posted on 2002-04-24 18:09:14 by bitRAKE
Jeez Louise bitrake!

According to the timestamps of the posts it only took you 4 mins to type that reply. You must type a lot faster then I do. :)

Chalain:
Sorry, I think I misunderstood your question but it's time for bed so I'll have to wait till tomorrow to re-read it.
Posted on 2002-04-24 21:42:52 by Will
I'm not as fast as I would like to be, or appear:
Last edited by bitRAKE on April 24th, 2002 at 04:18 PM
Posted on 2002-04-24 22:02:01 by bitRAKE
Have i missed something here,


mov eax, logFont.lfHeight ; pointer in EAX
mov eax, [eax] ; dereference it

This is the nortmal way to dereference a value but after looking at the LOGFONT structure in win32.hlp, the lfHeight member does not seem to be a pointer, just a value ?

Regards,

hutch@movsd.com
Posted on 2002-04-25 05:09:06 by hutch--
Chalain, it's worth noting that
"mov eax, variable" does exactly the same as "mov eax, ".
Imo should be enforced, since you *are* doign a memory
dereference (and then it would be possible to skip "offset" and do
"offset" when no brackets are present, like nasm does it).

As bitrake says, if you have a *pointer* to something, you don't
"lea reg, " - you "mov reg, ". An example...

we have "pLogFont", a pointer that contains the memory adress 401004. The pLogFont variable itself is stored at 401000...
lea eax, ; eax now contains 401000
mov eax, ; eax now contains value of pLogFont, 401004

I hope that'll clear up things a bit more :).
Posted on 2002-04-25 06:46:01 by f0dder