3

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
Daniel
  • 198
  • 2
  • 14
  • Don't think of variables as just a memory location. A variable can be live in a register, or dead in memory. You can and should keep variables in registers as much as possible, only spilling them to memory when needed. 32bit x86 has 7 general-purpose registers (besides the stack pointer) that you can and should use. – Peter Cordes Jan 27 '16 at 03:01

1 Answers1

5

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.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • *Almost every non-Intel architecture provides direct mem-mem move instructions*? Most non-Intel architectures *are* RISC, not CISC, and RISC architectures are usually load/store machines. I tested `int a, b; void foo(){a=b;}` on godbolt, and ARM/ARM64/PPC/AVR all load/store using scratch registers. http://goo.gl/k5oFXS. x86's `movs` instruction is unusual. – Peter Cordes Jan 27 '16 at 02:57
  • @PeterCordes: While it does seem recent designs tend to be RISC, the x86 is decidedly a CISC design without mem-to-mem moves. However the M68K, NS320x, Rx, Vax, 8051 family, and even the Z80 are CISC. I have revised that comment. Thanks. – wallyk Jan 27 '16 at 17:51