2

Sure MOV "moves" (copies actually) something, but how ? Does it put the actual value from the source into the destination or puts some kind of address.

This question came to me when I saw in Jeff Duntemann introductive assembly book that he is using interrupt 80h of Linux like this:

mov eax,4    ; Specify sys_write call
mov ebx,1    ; Specify File Descriptor 1: Standard output
mov ecx,Buff ; Pass address of the character to write
mov edx,1    ; Pass number of chars to write
int 80h      ; Call sys_write

I've had little practice with TASM before I started reading , but knew about the instruction LEA.So when I saw:

mov ecx,Buff ; Pass address of the character to write

It blew me away since I used LEA (Load Effective Address) or OFFSET to put addresses into registers and he is using MOV.

Are both forms correct? He is using NASM, though, so is it because of the Assembler? I am very confused right now, since I was used to see that MOV puts the value not an address.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Paul
  • 11
  • 1
  • 5
  • Yes, it's because different assemblers unfortunately use different syntax. I covered the difference between MASM and NASM syntax for `label` as a source operand (among other things) in my answer on http://stackoverflow.com/questions/34058101/referencing-the-contents-of-a-memory-location-x86-addressing-modes. You might find some of it helpful (or not, IDK!) Usually when you use `LEA` to get an address into a register (instead of `mov reg, imm32`), it's in 64bit mode to get RIP-relative addressing (position independent that doesn't need relocation.) – Peter Cordes Dec 23 '15 at 13:59

1 Answers1

2

The MOV (move) instruction transfer data between memory and registers or between registers. The MOV instruction performs basic load data and store data operations between memory and the processor’s registers and data movement operations between registers.The MOV instruction cannot move data from one memory location to another or from one segment register to another segment register. Memory-to-memory moves are performed with the MOVS (string move) instruction.

enter image description here

The LEA (load effective address) instruction computes the effective address in memory (offset within a segment) of a source operand and places it in a general-purpose register. This instruction can interpret any of the processor’s addressing modes and can perform any indexing or scaling that may be needed.

Intel 64 and IA-32 architectures Software Developer's manual

It's also highly useful as a 3-operand non-destructive add, doing e.g. ecx = eax + edx*4 - 15


NASM avoids this undesirable situation by having a much simpler syntax for memory references. The rule is simply that any access to the contents of a memory location requires square brackets around the address, and any access to the address of a variable doesn’t. So an instruction of the form mov ax, foo will always refer to a compile−time constant, whether it’s an EQU or the address of a variable; and to access the contents of the variable bar, you must code mov ax, [bar]. This also means that NASM has no need for MASM’s OFFSET keyword, since the MASM code mov ax, offset bar means exactly the same thing as NASM’s mov ax,bar.

Nasm documentation


From all the above you may see that that there are different ways to get the address of a variable in asm and these ways may and will differ in different assembly languages. To find an answer to your question it is good idea to check the documentation (assembly languages are usually well documented).

You should always remember in programming there are always several ways one particular problem can be solved.

Community
  • 1
  • 1
hrust
  • 734
  • 1
  • 11
  • 34
  • A lot of the use-cases for LEA are for math on non-address values. e.g. general-purpose integer math, with the result written to a 3rd register. – Peter Cordes Dec 23 '15 at 13:44