Suppose I have the following declared:
section .bss
buffer resb 1
And these instructions follow:
mov al, 5
mov [buffer], al
mov bl, [buffer]
mov cl, buffer
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
vsmov cl, [buffer]
- For example,
UPDATE: After reading the responses, I suppose the following summary is accurate:
Assume the declaration array resb 0
exists under section .bss
. My understanding is that:
mov edi, array
puts the memory address of the zeroth array index inedi
.mov [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]
puts the DATA at the zeroth index intoal
.mov al, [array+3]
puts the DATA at the third index intoal
.mov [al], [array]
is invalid, for whatever reason.mov array, 3
is invalid, because you can't say "Hey, I don't like the offset at whicharray
is stored, so I'll call it 3"mov [array], 3
puts the value 3 into the zeroth index of the array.
Please mention if any of these is false.
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.
results:
....
results:
....
results:
Dirk
The square brackets essentially work like a dereference operator (e.g., like
*
in C).So, something like
moves the value of
x
intoREG
, whereasmoves the value of the memory location where
x
points to intoREG
. Note that ifx
is a label, its value is the address of that label.As for you're question:
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 ofbuffer
.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
orFF1Ah
.What you can do is move a register to the place where the constant points to.You can do it likemov [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) , whereasmov cl, [buffer]
will get the actual value.Summary
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 variablebuffer
. It'll only have the least significant 8 bits of the address.buffer
, the value inbl
may differ from 5. Broken interrupt routines may actually affect any register when they fail to preserve register values.