The MASM Forum

General => The Campus => Topic started by: Ben321 on March 23, 2015, 07:25:08 PM

Title: How come this doesn't work?
Post by: Ben321 on March 23, 2015, 07:25:08 PM
This code isn't working.
mov [eax], FFh

EAX contains the address of the destination of the MOV operation. While "mov eax,SOME_DATA_SOURCE_or_CONSTANT_DATA_VALUE" would move 4 bytes from the data source (or constant value) into the EAX register, the square brackets around EAS like [EAS] is supposed to completely change the meaning of it. [EAX] is supposed to mean "use the the value stored in EAX register as a memory address". Therefore, "mov [eax],SOME_DATA_SOURCE_or_CONSTANT_DATA_VALUE" is supposed to mean "copy SOME_DATA_SOURCE_or_CONSTANT_DATA_VALUE to memory at the address stored in the EAX register". But it doesn't work.  Every time I compile, I get this error.

Quoteerror A2070: invalid instruction operands

Why am I getting this error?
Title: Re: How come this doesn't work?
Post by: dedndave on March 23, 2015, 09:45:33 PM
    mov     [eax],dl
    mov     [eax],dx
    mov     [eax],edx


in the above lines, the assembler knows the size of the operand (byte, word, dword)

    mov     [eax],0FFh

the assembler doesn't know whether you want to write a byte, word, or dword sized operand

    mov byte ptr [eax],0FFh
    mov word ptr [eax],0FFh
    mov dword ptr [eax],0FFh


now, the assembler knows what you want
Title: Re: How come this doesn't work?
Post by: mabdelouahab on March 23, 2015, 09:46:03 PM
Quote from: Ben321 on March 23, 2015, 07:25:08 PM
... would move 4 bytes from the data source (or constant value) into the EAX register
mov dword ptr [eax], 0FFh
Title: Re: How come this doesn't work?
Post by: Ben321 on March 24, 2015, 04:22:29 AM
Quote from: dedndave on March 23, 2015, 09:45:33 PM
    mov     [eax],dl
    mov     [eax],dx
    mov     [eax],edx


in the above lines, the assembler knows the size of the operand (byte, word, dword)

    mov     [eax],0FFh

the assembler doesn't know whether you want to write a byte, word, or dword sized operand

    mov byte ptr [eax],0FFh
    mov word ptr [eax],0FFh
    mov dword ptr [eax],0FFh


now, the assembler knows what you want

What does the "ptr" keyword do?
Title: Re: How come this doesn't work?
Post by: Gunther on March 24, 2015, 04:32:28 AM
Hi Ben,

Quote from: Ben321 on March 24, 2015, 04:22:29 AM
What does the "ptr" keyword do?

not much. It's a syntax question for Masm/jWasm. There are other assemblers available and the ptr stuff isn't necessary for those. But again: this syntax is necessary for Masm and jWasm.

Gunther
Title: Re: How come this doesn't work?
Post by: Ben321 on March 24, 2015, 05:10:03 AM
I assume ptr means "pointer" so as to say "use the value stored in eax register as a pointer to the desired memory destination", but I thought that was already covered by putting "eax" in brackets like "[eax]". Without brackets eax by itself means "use the eax register as the destination". So using "ptr" seems quite redundant. However without "ptr" that line of code fails. Why?
Title: Re: How come this doesn't work?
Post by: Gunther on March 24, 2015, 05:38:24 AM
Quote from: Ben321 on March 24, 2015, 05:10:03 AM
However without "ptr" that line of code fails. Why?

The reason is the syntax (see post #4).

Gunther
Title: Re: How come this doesn't work?
Post by: dedndave on March 24, 2015, 06:40:12 AM
[eax]
the value in EAX is an address
for 32-bit code, addresses are always 32-bits wide

the data operand, however, may be byte, word, dword, in some cases, oword
when working with the FPU, it might be real4 ptr, real8 ptr, or real10 ptr

"ptr" does mean pointer
if you want to translate, something like, "the address in EAX is a pointer to a ____"
the PTR operator may be used in a few different ways, though   :P
Title: Re: How come this doesn't work?
Post by: jj2007 on March 24, 2015, 09:00:56 AM
Quote from: dedndave on March 24, 2015, 06:40:12 AM
when working with the FPU, it might be real4 ptr, real8 ptr, or real10 ptr

Don't forget word ptr on the FPU ;-)

include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  push 1234+65536*5678
  fild word ptr [esp]
  fimul word ptr [esp+2]
  fistp dword ptr [esp]
  pop eax
  Inkey Str$("1234*5678=%i", eax)
  Exit
end start
Title: Re: How come this doesn't work?
Post by: rrr314159 on March 24, 2015, 01:33:34 PM
jj,

as long as we're at it let's not forget dword and qword ptr on the FPU :)

include \masm32\include\masm32rt.inc

.data?
    dword1 dd ?
    dword2 dd ?
.code

start:

; standard, sensible way to use dword ptr on the FPU ---------

  push 1234
  push 5678
  fild dword ptr [esp]
  fimul dword ptr [esp+4]
  fistp dword ptr [esp+4]
  pop eax
  pop eax
  printf("1234*5678=%i\n", eax)

; and normal, obvious way to use qword ptr on the FPU ---------

  push 5678
  push 1234
  fild qword ptr [esp]
  fstp qword ptr [esp]
  pop dword1
  pop dword2
 
  printf("1234+65536^2*5678=%.19g\n", qword ptr dword1)
 
ret

end start

Title: Re: How come this doesn't work?
Post by: jj2007 on March 24, 2015, 05:45:03 PM
Quote from: rrr314159 on March 24, 2015, 01:33:34 PM
; and normal, obvious way to use qword ptr on the FPU ---------

*** WARNING - mathematician detected :greensml: