News:

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

help using PTR with indirect operands

Started by jonwondering, September 03, 2006, 04:41:52 AM

Previous topic - Next topic

jonwondering

hey guys. newbie question. i am a little confused on when to use PTR when using dereference. i am reading Kip Irvine's book on assembly and he says use it to tell the compiler what type of variable i am using, so instead of something like this:

cmp [ebx], 0
i should use: cmp WORD PTR [ebx], 0

when do i use the PTR? in some examples he uses dereferencing without PTR. how does the compiler know what type of variable it is, and how will i know where to put this PTR? thanks.

Shantanu Gadgil

#1
Unless specfied, and to be on the safe side, use the "type specifier" when comparing with direct values.

"direct value" means "non-register" data, either some value like 0, 1, etc or some variable.

The reason is, that it may happen that by default the "cmp [ebx], 0" will take the "default sized" data type at location value of ebx and compare it with 0.
So for a 32bit system, it would use the 32bit value at location of ebx.

As you have mentioned in your code, you are interested in the WORD at location of ebx.  :bg

Another thing, you should not worry (if you are using a proper assembler, of course) if you are comparing a "pointer" location and a register.
So, a "cmp [ebx], cx" should be same as "cmp WORD PTR [ebx], cx" :bg :bg

I say "to be on the safe side" in case of variables, because if you have two "WORD" variables next to each other, like so
var1 word 5
var2 word 2

and you compare like so, "cmp [ebx], var1", you should know how the assembler does the instruction translation, as in, would "var1" get translated only as an address or a proper "WORD PTR" because var1's datatype is WORD!

So, for now, go with "<DATATYPE> PTR" for variables and directvalues!!!  :bg

Hope that helps,
Shantanu
To ret is human, to jmp divine!

jonwondering

it does help SHantanu. thanks for clearing that up!