2012-12-09 16 views
16

Qual è la differenza tra queste due linee? Quale PTR cambia qui?x86, differenza tra BYTE e BYTE PTR

;first 
mov BYTE [ecx], 0 
;second 
mov BYTE PTR [ecx], 0 
+9

non c'è alcuna differenza. L'assemblatore accetta solo due dialetti diversi. –

+0

+1 per una domanda sull'assemblaggio x86. E @AkiSuihkonen, sembra una risposta piuttosto che un'osservazione. –

+1

Linkas e manca un dettaglio molto importante nella domanda: quale programma assemblatore viene utilizzato: MASM/TASM/NASM/YAMS o altro. E come viene utilizzato (ci sono opzioni dialettali in alcuni di essi). – osgx

risposta

12

Sommario:

  • NASM/yasm richiede word [ecx] quando l'operando-dimensione non è implicito operando. (Altrimenti [ecx] è ok).
  • MASM/TASM richiede word ptr [ecx] quando la dimensione dell'operando non è implicita dall'altro operando. (Altrimenti [ecx] è ok).

Ciascuno soffoca sulla sintassi dell'altro.


ATTENZIONE: area molto strana senza standard ISO o tabelle BNF facili da trovare; e non sono un esperto nel percorrere campi minati di sintassi proprietaria MASM.

E 'il tuo caso non c'è può essere alcuna differenza, ma operatore PTR può significare in altri casi:

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

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 

credo, ci sono anche assembler requisiti specifici (nasm/tasm/other asm) e l'uso di "byte ptr" è più portabile.

di controllare anche la sezione 4.2.16 in book from India e sezioni 8.12.3 (e 8.11.3 "Tipo Conflitti") nella "The Art of Assembly linguaggio di programmazione".

AGGIORNAMENTO: grazie a Frank Kotler, sembra che il NASM "usi una variazione della sintassi dell'assembly Intel" (wiki), che non include l'operazione PTR.

Update1: C'è originale "ASM86 LANGUAGE REFERENCE MANUAL" di Intel, 1981-1983, PTR operatore è definita a pagina 4-15:

PTR Operator

Syntax: type PTR name

Description: The PTR operator is used to define a memory reference with a certain type. The assembler determines the correct instruction to assemble based on the type of the operands to the instruction. There are certain instances where you may specify an operand that has no type. These cases involve the use of numeric or register expressions. Here the PTR operator is used to specify the type of the operand. The following examples illustrate this use:

MOV WORD PTR [BX], 5  ;set word pointed to by BX = 5 
INC DS:BYTE PTR 10   ;increment byte at offset 10 
           ;from DS 

This form can also be used to override the type attribute of a variable or label. If, for example, you wished to access an already defined word variable as two bytes, you could code the following:

MOV CL, BYTE PTR AWORD  ;get first byte 
MOV CL, BYTE PTR AWORD + 1 ;get second byte 

Field Values:

type This field can have one of the following values: BYTE, WORD, DWORD, QWORD, TBYTE, NEAR, FAR

name This field can be: 1. A variable name. 2. A label name. 3. An address or register expression. 4. An integer that represents an offset.

UPDATE2: Grazie a Uni di bitter di Stoccarda! C'è original MACRO-86 manual di Microsoft (1981).Pagina 3-7:

The PTR operator can be used another way to save yourself a byte when using forward references. If you defined FOO as a forward constant, you might enter the statement:

MOV [BX],FOO 

You may want to refer to FOO as a byte immediate. In this case, you could enter either of the statements (they are equivalent):

MOV BYTE PTR [BX],FOO 

MOV [BX],BYTE PTR FOO 

These statements tell MACRO-86 that FOO is a byte immediate. A smaller instruction is generated.

e pagina 3-16:

Override operators

These operators are used to override the segment, offset, type, or distance of variables and labels.

Pointer (PTR)

<attribute> PTR <expression> 

The PTR operator overrides the type (BYTE, WORD, DWORD) or the distance (NEAR, FAR) of an operand.

<attribute> is the new attribute; the new type or new distance.

<expression> is the operand whose attribute is to be overridden.

The most important and frequent use for PTR is to assure that MACRO-86 understands what attribute the expression is supposed to have. This is especially true for the type attribute. Whenever you place forward references in your program, PTR will make clear the distance or type of the expression. This way you can avoid phase errors.

The second use of PTR is to access data by type other than the type in the variable definition. Most often this occurs in structures. If the structure is defined as WORD but you want to access an item as a byte, PTR is the operator for this. However, a much easier method is to enter a second statement that defines the structure in bytes, too. This eliminates the need to use PTR for every reference to the structure. Refer to the LABEL directive in Section 4.2.1, Memory Directives.

Examples:

CALL WORD PTR [BX][SI] 
MOV BYTE PTR ARRAY, (something) 

ADD BYTE PTR FOO,9 

Dopo aver letto questo e cercando alcune definizioni di sintassi da questi documenti, penso che scrivere PTR sia obbligatorio. L'utilizzo di mov BYTE [ecx], 0 non è corretto secondo il manuale MACRO-86.

+7

Nasm si spegne in 'PTR'. Masm/Tasm sbatteranno senza di essa. –

+0

@ Il commento di Frank dovrebbe essere davvero la risposta qui, piuttosto che questo pasticcio ben intenzionato ... ;-) Se non ti dispiace, questa è una domanda abbastanza comune, quindi potrebbe valerne la pena di tornare indietro e riscrivere/riformattare ampie porzioni di questa risposta (e includere un TL; DR per la massima chiarezza). –

+0

@Cody, Ciao, in realtà riesco a capire qualsiasi cosa di questa risposta ora (era più simile al mio taccuino personale per registrare alcuni URL di alcuni interessanti manuali antichi/museali). Puoi aiutarci a modificarlo riscrivendolo (probabilmente con la conversione in wiki)? – osgx

6

Si sta utilizzando un assemblatore permissivo, a quanto pare, il supporto del compilatore C per l'assemblaggio in linea non è sicuro. La sintassi corretta è BYTE PTR per comunicare all'assemblatore che il valore nel registro ECX deve essere trattato come un puntatore. PTR. Ma questa è la sintassi che è su uno specifico, potrebbe già dire che intendevi usarlo come puntatore mettendo [parentesi] attorno al nome del registro. L'utilizzo di [ecx] ha già chiarito che intendevi memorizzare zero nell'indirizzo fornito dal registro ECX.

Quindi, sa come utilizzare il registro ECX, l'unica altra cosa che non si sa è quanti byte devono essere impostati su zero. Le scelte sono 1, 2 o 4. Hai chiarito, 1. BYTE.

0

In MASM, BYTE PTR [ecx] accede alla memoria all'indirizzo ecx. BYTE [ecx] è un errore di sintassi ("errore di sintassi dell'assemblatore inline in 'primo operando'; trovato '['").

In NASM o YASM, BYTE [ecx] accede alla memoria all'indirizzo ecx. BYTE PTR [ecx] è un errore di sintassi ("errore: virgola, due punti o fine linea prevista" in NASM, "simbolo non definito 'PTR'" in YASM).

In TASM, BYTE PTR [ecx] e BYTE [ecx] sono equivalenti - sia la memoria di accesso all'indirizzo ecx.

Tuttavia, nel gas dell'assemblatore Gnu, quando si utilizza la sintassi Intel BYTE PTR [ecx] accede alla memoria in ecx, ma BYTE [ecx] effettivamente accede alla memoria all'indirizzo ecx + 1. Cioè, BYTE [ecx] è equivalente a PTE BYTE [ecx + 1], che non sembra essere né sano né documentato.

assemblatore GNU versione 2.18, 2.24, o 2.26.1:

cat > foo.S << EOF 
.intel_syntax noprefix 
movb BYTE [ecx], 0 
movb BYTE PTR [ecx], 0 
.att_syntax prefix 
EOF 

as foo.S 
objdump -dM intel a.out 

0: 67 c6 41 01 00   mov BYTE PTR [ecx+0x1],0x0 
5: 67 c6 01 00    mov BYTE PTR [ecx],0x0 
+1

Nell'assemblatore GNU l'identificatore BYTE, WORD, DWORD sono anche come definisce 1,2,4 'movb BYTE [ecx], 0' è in realtà lo stesso di' movb 1 [ecx], 0' che è 'mov BYTE PTR [ecx + 1], 0'. 'movb WORD [ecx], 0' è uguale a' movb 2 [ecx], 0' o 'mov BYTE PTR [ecx + 2], 0'. 'mov eax, WORD' è lo stesso di' mov eax, 2'. 'mov eax, BYTE' è lo stesso di' mov eax, 1' ecc. –

+0

Interessante, grazie! "mov ecx, BYTE" è un errore di sintassi in NASM, MASM e TASM, ma non gas. –

Problemi correlati