News:

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

Label vs PTR

Started by unktehi, February 25, 2009, 04:15:19 AM

Previous topic - Next topic

unktehi

To me, as I understand using labels and ptrs is that they basically do the same thing. What are the differences and why would I use one over another?


Neil

A Label is the name given (by you) to an address in your program, this is used in the following manner :-

Label: some code
         more code
         test eax,1
         jne Label

This tells the assembler where to go if the test is not true by replacing Label with an address.

A pointer is a register which contains the address of  data which then be accessed in a number of different ways.

Data db 1,2,3,4,5,6,7,8,9

lea esi Data   ;esi now contains the address of the start of the data

The data can now be accessed e.g.  lodsb which is a string operator
                                                or mov al,[esi+5] which adds an offset
                                                or mov al,[esi+ebx] which adds an offset contained in ebx

There are other ways also.

unktehi

Yes, that's what I understood about Labels as well (basically a pointer), but then the presentation I was looking at discussed how Labels and PTRs can be used in a similar manner,  I think that's what confused me.  I'm referring to the label directive.  This was the content on that slide:

Assigns an alternate label name and type to an existing storage location
LABEL does not allocate any storage of its own
Removes the need for the PTR operator

and here were a few examples:

dwList   LABEL DWORD
wordList LABEL WORD

RuiLoureiro

Quote from: unktehi on February 25, 2009, 04:30:51 PM
... ( basically a pointer )
Hi,
     Hummm, ???
This:
.DATA
dwList     dd 500
wordList  dw 20

Is the same as this:
.DATA
dwList     label DWORD
              dd 500
wordList  label WORD
              dw 20
;----------------------------------------------
So, if i want the address of 500 (the pointer) in the esi register i write 
                      mov      esi, offset dwlist

If i want 500 in the register EAX i write   
                      mov      eax, dword ptr [dwList]
Or
                      mov      esi, offset dwList
                      mov      eax, dword ptr [esi]
Rui

unktehi

So what would be the advantage of writing this:

.DATA
dwList     dd 500
wordList  dw 20

over this? Or vice versus?

.DATA
dwList     label DWORD
              dd 500
wordList  label WORD
              dw 20

PBrennick

Think of it this way:

dwList     dd 500

mov      esi, offset dwlist

In the above, dwlist is a Label and esi is a Pointer to the same labeled location. It is easier to manipulate data using a register but you can do either:

mov      al, esi

or

mov      al, dwlist[0]

both access the same memory location but when you are using a register you can move around in the data whereas a Label will always point to the one location

inc      esi

Usually, I will use the Label method to zero a string but if I am going to change data within the string and the offset is calculated at run-time based upon something you are doing at the time; the Pointer method is a lot more flexible.

Paul

The GeneSys Project is available from:
The Repository or My crappy website

RuiLoureiro

Quote from: PBrennick on February 25, 2009, 08:26:19 PM

It is easier to manipulate data using a register but you can do either:

mov      al, esi
Paul,
            not    mov      al, esi      but       mov   al, byte ptr [esi]
            because we cannot pass a 32 bit value (in esi) to a 8 bit register (in al) unless  we use movzx
Rui

RuiLoureiro

Quote from: unktehi on February 25, 2009, 07:57:30 PM
So what would be the advantage of writing this:

.DATA
dwList     dd 500
wordList  dw 20

over this? Or vice versus?

.DATA
dwList     label DWORD
              dd 500
wordList  label WORD
              dw 20
I dont see any advantage (i use the first ...). Is there any one ?
To get the byte at the address dwList i write  mov ..., byte ptr [dwList].
To get the word, mov  ..., word ptr [dwList] and to get the dword, mov ..., dword ptr [dwList]
Rui

unktehi

Ok Thanks guys.  I think I understand it.  For some reason I was overthinking it - I think I was thinking there was more to it than that.

Neil

mov   al, byte ptr [esi] byte ptr is not required in this statement, size is implied by al register, it's needed in this case     mov [esi],48 will fail  mov byte ptr[esi],48 is correct.

ic2

QuoteWhat are the differences and why would I use one over another?

Is there a speed and security advantage somewhere?  That's why I don't use PTR unless I have to.  Never learn all the facts, but I caught the he say once upon a time and never looked back.  Seems that even ASM people don't care anymore since CPU is so fast these days.

FORTRANS

Quote from: unktehi on February 25, 2009, 07:57:30 PM
So what would be the advantage of writing this:

.DATA
dwList     dd 500
wordList  dw 20

over this? Or vice versus?

.DATA
dwList     label DWORD
              dd 500
wordList  label WORD
              dw 20

Hi,

   As I understand it, the LABEL version allows you to
use multiple types on the same memory location/variable.
So you can use the type checking in MASM without
using PTR overrides.  So:


VarWord LABEL WORD
VarDoub DD      500

        MOV     AX,[VarWord]
        MOV     EAX,[VarDoub]


Regards,

Steve N.

RuiLoureiro

Quote from: FORTRANS on February 26, 2009, 03:04:01 PM
So you can use the type checking in MASM without
using PTR
overrides.  So:

VarWord LABEL WORD
VarDoub DD      500

        MOV     AX,[VarWord]
        MOV     EAX,[VarDoub]

Hi
Steve,
              Two names for one variable (VarDoub and VarWord) is it an advantage ?
               I will use   mov    eax, Vardoub           or        mov    ax, word ptr Vardoub
                      <=> mov     eax, dword ptr [Vardoub]  , mov    ax, word ptr [Vardoub]
Rui

PBrennick

Rui,
Yeah, I made a typo

mov al, [esi]

is all that is needed because al has size and tells the assembler all it needs to know. You do not need 'byte ptr' in this case. But, yes, egg on my face and all that. :U

Paul
The GeneSys Project is available from:
The Repository or My crappy website

FORTRANS

Quote from: RuiLoureiro on February 26, 2009, 04:07:34 PM
Hi
Steve,
              Two names for one variable (VarDoub and VarWord) is it an advantage ?
               I will use   mov    eax, Vardoub           or        mov    ax, word ptr Vardoub
                      <=> mov     eax, dword ptr [Vardoub]  , mov    ax, word ptr [Vardoub]
Rui

Hi,

   Well, it is an advantage if you don't like to use the PTR overrides.
It is a disadvantage if you want to prevent the type checking from
being subverted.  It is available, even if a bit strange.  YMMV.  It is
probably not advisable to mix up your code and use both forms?

Cheers,

Steve N.