The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Sergiu FUNIERU on February 28, 2010, 06:07:35 PM

Title: Why can't I copy directly to a variable?
Post by: Sergiu FUNIERU on February 28, 2010, 06:07:35 PM
This code is from Introduction To Assembler (the chm file from the MASM32 distribution),
from the "Addressing and Pointers" chapter.

    mov eax, lpvar    ; copy address into eax
    mov eax, [eax]    ; dereference it
    mov nuvar, eax    ; copy eax into new variable


Why don't we do directly this?
    mov nuvar, [lpvar]
Title: Re: Why can't I copy directly to a variable?
Post by: jj2007 on February 28, 2010, 06:30:04 PM
Direct memory to memory moves work only with three opcodes: push, pop and movs. However, you can use the m2m macro:

include \masm32\include\masm32rt.inc

.data
MySrcVar dd 123
MyDestVar dd 0

.code
start: m2m MyDestVar, MySrcVar
MsgBox 0, str$(MyDestVar), "Masm mem to mem:", MB_OK
exit
end start


Quote; memory to memory assignment
    ; ----------------------------
      m2m MACRO M1, M2
        push M2
        pop  M1
      ENDM
Title: Re: Why can't I copy directly to a variable?
Post by: dedndave on February 28, 2010, 10:13:51 PM
it is a little bit involved, but you can use VirtualProtect to allow modification of immediate values in the code segment
usually called self-modifying code

        mov     SomeLabel,0FFFFFFFFh
ImmedLabel LABEL DWORD
.
.
.
        mov     ImmedLabel-4,eax
Title: Re: Why can't I copy directly to a variable?
Post by: hutch-- on March 01, 2010, 01:31:00 AM
Sergiu,

There is no opcode built into the hardware to perfom direct memory to memory copy, the opcodes that JJ listed for you do a similar thing but almost all opcodes that involve data movement require a register on one side, its built into the hardware that way.