Hello,

I'm a newbie to assembler programming and looking at code samples I was wandering about the meaning of the square brackets and PTR operators. Can anyone confirm that the square bracket operator specifies a memory reference (of the contained register, label, etc) and that the PTR operator is used to do something like a C cast??? To what can the square brackets operator be applied to apart registers and labels???
Thx.

Regards,
YAA
Posted on 2002-09-12 05:03:21 by yaa
Hi yaa ( no pun intended )

Welcome to the community.

You are correct about both [] and PTR, they can be used like



mov eax, [eax] ; read data from the address in eax
mov eax, [count] ; read data from the variable count - altho [] not necessary if using MASM

lea eax, [ebx+ecx*4] ; eax = ( ebx + ( ecx * 4 ))

mov BYTE PTR [edi], 0 ; write a single byte to the address in edi

; Read data from the memory pointed
; to by eax but treat the memory as if it
; was a 'SomeStruct' structure

mov eax, (SomeStruct PTR [eax]).m_dataMember


Hope this helps :alright:

Maelstrom
Posted on 2002-09-12 05:28:41 by Maelstrom
yaa,
the PTR casts are mainly there so that the assembler knows what you are trying to do, and chooses the right opcode. They are not really there for the programmer's benefit (ie. they don't really fill the same purpose as what they do in C/C++).
Posted on 2002-09-12 07:34:59 by sluggy
In MASM, the square brackets do not mean memory. It means that registers written inside them are used to build an address for the instruction. If no registers are mentioned, the brackets are redundant. The result is that the following two instructions are the same:

mov eax,1
mov eax,[1]
Posted on 2002-09-12 20:36:10 by tenkey
In MASM all these generate the same instruction:

mov eax,1
mov eax,[1]
mov eax, DWORD PTR 1
mov eax, DWORD PTR [1]

:eek:

So, how does one move a word from an immediate address to a register?

A1 01000000

verses

B8 01000000

Of course, we never need to do this in Win32asm.
Posted on 2002-09-12 20:48:31 by bitRAKE
Hi,
I have one document that I think could get you a good, quick and precise start to assembler. I would email you, but I think it can be usefull to more people, and as it is small, I'll just attach it here.

I think if you read it BEFORE you start with Iczelion tuts, examples from MASM, etc. around, it will be smoother.....

I myself made a good use of it these days... :)

See'ya'll!


IMPORTANT: Im lazy enough to NOT zip the file.... change its extension to CHM intead of .ZIP ... it stands for "compiled HTML".
:-d
It should run as a stand alone program. No harm.
Posted on 2002-09-12 20:58:49 by wicr0s0ft

In MASM all these generate the same instruction:

mov eax,1
mov eax,[1]
mov eax, DWORD PTR 1
mov eax, DWORD PTR [1]

:eek:

So, how does one move a word from an immediate address to a register?
Add a segment register...
mov eax,[DS:0]

mov eax,[FS:0]
Posted on 2002-09-12 22:23:16 by tenkey
There is another one very useful meaning of [] operator.

This doesn't work:
.if (RECT PTR [eax]).left == 0

But this works.
.if [RECT PTR [eax]].left == 0
Posted on 2002-09-13 01:32:24 by Four-F
yaa,

Most of what you need to know has been said here, there are slightly different meanings between different assemblers with how they use [] but with MASM, it is a notation for a memory operand of a value.

You can load it in different ways depending on where the address is located. If it is data like string characters, you use the OFFSET operator in MASM which actually put the OFFSET of the variable into the register you choose, if its a variable that is created dynamically on the stack, you use the LEA instruction to load the register.

The PTR operator is to tell the assemblr the size of the data as there are some situations where the size cannot be derived by the assembler.

MOV , 0

is not a valid instruction because the memory operand does not set the size. If you want to copy the BYTE value of zero into the memory in , you use the BYTE PTR notation so the assembler knows what to do there.

Regards,

hutch@movsd.com
Posted on 2002-09-13 02:01:26 by hutch--
Originally posted by bitRAKE
In MASM all these generate the same instruction:


mov eax,1
mov eax,[1]
mov eax, DWORD PTR 1
mov eax, DWORD PTR [1]

mov eax, DWORD PTR [1]

error: extra characters on line

And that is why I use FAsm.
Posted on 2002-09-13 03:46:36 by eet_1024
eet_1024,

Bill and the team put the extra syntax into MASM so you could emulate TASM, NASM, MASM and whatever else because they are just nice guys who like to help everybody. :tongue:

Regards,

hutch@movsd.com
Posted on 2002-09-13 04:57:09 by hutch--