3

I don't understand the difference between MOV and MOV ptr.

For example, in this C code:

unsigned char x, y;
x = 2; 

the second line in assembly is:

`MOV x, 2`

but the second line of this C code :

tabbyte[0] = 15
unsigned char tabbyte[4]

in assembly is :

MOV byte ptr tabbyte[0], 15

What's the difference between the two assembly instructions and when should each one be used ?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
IanMoone
  • 181
  • 2
  • 9

2 Answers2

5
  1. Directives BYTE PTR, WORD PTR, DWORD PTR

    There are times when we need to assist assembler in translating references to data in memory.

    For example, instruction

        mov     [ESI], al  ; Store a byte-size value in memory location pointed by ESI
    

    suggests that an 8-bit quantity should be moved because AL is an 8-bit register.

    When instruction has no reference to operand size,

        mov     [ESI], 5   ; Error: operand must have the size specified
    

    To get around this instance, we must use a pointer directive, such as

        mov     BYTE PTR [ESI], 5  ; Store 8-bit value
        mov     WORD PTR [ESI], 5  ; Store 16-bit value
        mov     DWORD PTR [ESI], 5 ; Store 32-bit value
    

    These instructions require operands to be the same size.

    In general, PTR operator forces expression to be treated as a pointer of specified type:

        .DATA
        num  DWORD   0
    
        .CODE
        mov     ax, WORD PTR [num] ; Load a word-size value from a DWORD
    

http://www.c-jump.com/CIS77/ASM/Instructions/I77_0250_ptr_pointer.htm

0___________
  • 60,014
  • 4
  • 34
  • 74
  • @gonçalocosta: This answer doesn't explain why `MOV x, 2` is allowed: MASM has "variables" that magically imply an operand size, when you declare them like `x dd 0` instead of regular labels like `x: dd 0`. And also that bare symbol names are memory operands in MASM-style syntax. – Peter Cordes Apr 30 '19 at 17:11
2

byte ptr, word ptr, etc. are only required to indicate the size to operation if it is not implied by the operands. It is the square brackets ([ and ]) and in MASM a bare symbol that indicates a memory reference. To use the address of a variable in MASM prefix it with offset, for NASM just omit the square brackets.

GNU AS in Intel syntax mode behaves like MASM in this respect.

Timothy Baldwin
  • 3,551
  • 1
  • 14
  • 23
  • 2
    GAS `.intel_syntax` mode will never accept `MOV x, 2` because only MASM has "variables" that magically imply an operand size, when you declare them with `x dd 0` instead of regular labels like `x: dd 0`. – Peter Cordes Apr 30 '19 at 17:09