News:

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

Why can't I copy directly to a variable?

Started by Sergiu FUNIERU, February 28, 2010, 06:07:35 PM

Previous topic - Next topic

Sergiu FUNIERU

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]

jj2007

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

dedndave

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

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php