Daniel
Daniel

Reputation: 318

Moving variable's content into another variable

It seems to me like a very stupid question, but i couldn't find an answer.

is there any other way to pass a data variable content, to another variable, not using push/pop, or moving it first to a register?

I mean something like that:

.data

txt dd 1
txt1 dd 2

.code 

start:

mov txt1, txt

;or - mov [txt1], txt

ret

end start

Upvotes: 4

Views: 9448

Answers (1)

wallyk
wallyk

Reputation: 57784

In the 8086 family, the easiest way is to use an intermediate register:

   mov   eax, txt
   mov   txt1, eax

Many non-Intel CISC architectures provides a direct memory to memory move instruction. RISC architectures rarely do.

If there is more than that, it might be simpler to use a string move instruction which requires setting up the ESI and EDI registers, the DF flag, and if you want to use a rep prefix, the ECX register:

 lea  edi, dest   ; or mov edi, offset dest.  Shorter encoding.  Only use LEA for 64bit RIP-relative addressing.
 lea  esi, src
 cld
 movsd       ; moves 32-bit value [ESI] to [EDI] and increments both pointers
 movsd       ; moves another

Clearly that is not worth it for one or two words, but if you have the artificial constraints (no intermediate register, no push/pop), then that maybe satisfies the conditions.

If your function can assume its callers all strictly follow the standard calling convention, you can assume the direction flag is already cleared on function entry. Bootloader code should assume as little as possible about initial state, since different BIOSes jump to it with different states.

Upvotes: 5

Related Questions