33

Suppose I have the following declared:

section .bss
buffer    resb     1

And these instructions follow in section .text:

mov    al, 5                    ; mov-immediate
mov    [buffer], al             ; store
mov    bl, [buffer]             ; load
mov    cl, buffer               ; mov-immediate?

Am I correct in understanding that bl will contain the value 5, and cl will contain the memory address of the variable buffer?

I am confused about the differences between

  • moving an immediate into a register,
  • moving a register into an immediate (what goes in, the data or the address?) and
  • moving an immediate into a register without the brackets
    • For example, mov cl, buffer vs mov cl, [buffer]

UPDATE: After reading the responses, I suppose the following summary is accurate:

  • mov edi, array puts the memory address of the zeroth array index in edi. i.e. the label address.
  • mov byte [edi], 3 puts the VALUE 3 into the zeroth index of the array
  • after add edi, 3, edi now contains the memory address of the 3rd index of the array
  • mov al, [array] loads the DATA at the zeroth index into al.
  • mov al, [array+3] loads the DATA at the third index into al.
  • mov [al], [array] is invalid because x86 can't encode 2 explicit memory operands, and because al is only 8 bits and can't be used even in a 16-bit addressing mode. Referencing the contents of a memory location. (x86 addressing modes)
  • mov array, 3 is invalid, because you can't say "Hey, I don't like the offset at which array is stored, so I'll call it 3". An immediate can only be a source operand.
  • mov byte [array], 3 puts the value 3 into the zeroth index (first byte) of the array. The byte specifier is needed to avoid ambiguity between byte/word/dword for instructions with memory, immediate operands. That would be an assemble-time error (ambiguous operand size) otherwise.

Please mention if any of these is false. (editor's note: I fixed syntax errors / ambiguities so the valid ones actually are valid NASM syntax. And linked other Q&As for details)

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
InvalidBrainException
  • 2,052
  • 6
  • 27
  • 38
  • possible duplicate of [What do the brackets mean in x86 asm?](http://stackoverflow.com/questions/2030366/what-do-the-brackets-mean-in-x86-asm) – legends2k Sep 13 '14 at 11:17
  • `array resb 0` reserves a zero-length space at the label "array". If you want, say, a ten-entry array of byte entries in the .bss section, you should specify `resb 10`. – ecm Jul 25 '19 at 13:32

4 Answers4

25

The square brackets essentially work like a dereference operator (e.g., like * in C).

So, something like

mov REG, x

moves the value of x into REG, whereas

mov REG, [x]

moves the value of the memory location where x points to into REG. Note that if x is a label, its value is the address of that label.

As for you're question:

Am I correct in understanding that bl will contain the value 5, and cl will contain the memory address of the variable buffer?

Yes, you are correct. But beware that, since CL is only 8 bits wide, it will only contain the least significant byte of the address of buffer.

mtvec
  • 16,168
  • 4
  • 47
  • 79
  • 1
    `cl` will only contain the 8 least significant bits of the address. – Alexey Frunze Apr 28 '12 at 10:28
  • @Alex: Yes, I just realized that:-) – mtvec Apr 28 '12 at 10:29
  • when you say "if x is a label, its value is the address of that label" does that mean if x has the address of 0x1234 then the value when it's a label is 0x1234 as well? – Robert Houghton Jul 14 '19 at 02:50
  • @InstructionPointer: in MASM syntax, `mov eax, var` is a load, same as `[var]`. In NASM syntax, `mov eax, var` is a mov-immediate of the label address. (MASM syntax mov eax, OFFSET var`). If you want a load, always use `mov eax, [var]` to avoid ambiguity. If you want the address, there's no portable syntax that unambiguously means the address. (Except in x86-64 PIC code where `lea rdi, [var]` is the best choice, assuming `default rel` for NASM syntax. Except GAS .intel_syntax needs `lea rdi, [var + rip]` for the address with respect to the current location...) – Peter Cordes Jul 14 '19 at 23:50
18

Indeed, your thought is correct.That is, bl will contain 5 and cl the memory address of buffer(in fact the label buffer is a memory address itself).


Now, let me explain the differences between the operations you mentioned:

  • moving an immediate into a register can be done using mov reg,imm.What may be confusing is that labels e.g buffer are immediate values themselves that contain an address.

  • You cannot really move a register into an immediate, since immediate values are constants, like 2 or FF1Ah.What you can do is move a register to the place where the constant points to.You can do it like mov [const], reg .

  • You can also use indirect addressing like mov reg2,[reg1] provided reg1 points to a valid location, and it will transfer the value pointed by reg1 to reg2.


So, mov cl, buffer will move the address of buffer to cl(which may or may not give the correct address, since cl is only one byte long) , whereas mov cl, [buffer] will get the actual value.

Summary

  • When you use [a], then you refer to the value at the place where a points to.For example, if a is F5B1, then [a] refers to the address F5B1 in RAM.
  • Labels are addresses,i.e values like F5B1.
  • Values stored in registers do not have to be referenced to as [reg] because registers do not have addresses.In fact, registers can be thought of as immediate values.
Coldblackice
  • 3,302
  • 4
  • 17
  • 21
byrondrossos
  • 1,947
  • 1
  • 13
  • 19
  • 1
    I'm not well-versed at Assembly, but in the middle bullet point of your top section, shouldn't the last example line of assembly be the other way around? i.e., `mov [const], reg` ? – Coldblackice Dec 16 '12 at 13:11
6

You are getting the idea. However, there are a few details worth bearing in mind:

  1. Addresses can and usually are greater than what 8 bits can hold (cl is 8-bit, cx is 16-bit, ecx is 32-bit, rcx is 64-bit). So, cl is likely going to be unequal to the address of the variable buffer. It'll only have the least significant 8 bits of the address.
  2. If there are interrupt routines or threads that can preempt the above code and/or access buffer, the value in bl may differ from 5. Broken interrupt routines may actually affect any register when they fail to preserve register values.
Alexey Frunze
  • 58,178
  • 10
  • 71
  • 163
4

For all instruction with using immediate values as an operand for to write the value into a ram location (or for calculating within), we have to specify how many bytes we want to access. Because our assemble can not know if we want access only one byte, a word, or a doppleword for example if the immediate value is a lower value, like the following instructions shows.

array db 0FFh, 0FFh, 0FFh, 0FFh
mov byte [array], 3

results:

array db 03h, 0FFh, 0FFh, 0FFh

....

mov word [array], 3

results:

array db 03h, 00h, 0FFh, 0FFh

....

mov dword [array], 3

results:

array db 03h, 00h, 00h, 00h

Dirk