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
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 arrayadd edi, 3
, edi
now contains the memory address of the 3rd index of the arraymov 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)
Square brackets — the operand is an indirect address. If the operand is enclosed in square brackets, the assembler treats the operand as an indirect address; that is, it uses the contents of the operand as an address.
The brackets mean to de-reference an address. For example mov eax, [1234] means, mov the contents of address 1234 to EAX. So: 1234 00001. EAX will contain 00001.
Square brackets means 'the variable at the memory address stored in RAX”. So: mov RAX, 12. means “store value 12 into RAX” mov [RAX], 12. means “store value 12 in the memory cell whose address is stored in RAX'
The parentheses indicate the move instruction should consider the value in rbp to be a memory address, that it should move the value 42 to the memory address referenced by rbp (or actually to the memory address four bytes before the value of rbp ) and not into rbp itself.
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
.
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.
F5B1
, then [a] refers to the address F5B1 in RAM.F5B1
.You are getting the idea. However, there are a few details worth bearing in mind:
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.buffer
, the value in bl
may differ from 5. Broken interrupt routines may actually affect any register when they fail to preserve register values.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With