Is there a difference when we use #NUM1
and #4
In the final machine code in the executable that a CPU will actually run, no, there isn't.
If you have an assembler that directly creates an executable (no separate linking step), then the assembler will know at assemble time the numeric address of NUM1
, and simply expand it as an immediate, producing exactly the same machine code as if you'd written add #0x700, R0
. (Assuming the NUM1
label ends up at address 0x700
for this example.)
e.g. if the machine encoding for add #imm, R0
is 00 00 imm16
, then you'll get 00 00 07 00
(assuming a bit-endian immediate).
Here, is the address of NUM1 stored in Register R1?
No, it's added to R0. If R0 previously contained 4
, then R0 will now hold the address NUM1+4
.
R1 isn't affected.
Often you have an assembler and a separate linker (e.g. as foo.s -o foo.o
to assemble and then link with ld -o foo foo.o
).
The numeric address isn't available at assemble time, only at link time. An object file format holds metadata for the symbol relocations, which let the linker fill in the absolute numeric addresses once it decides where the code will be loaded.
The resulting machine code will still be the same.
add #num1, r0
depends on the assembler. The Immediate addressing mode usually means that the operand of an instruction is embedded in a dedicated field in the instruction itself. To the user this means they can use numeric constants in an instruction (e.g.add #5, r0
adds 5 tor0
). This contrasts to something likeadd (5), r0
(I'm inventing this syntax) where 5 is used to read from memory to fetch the number to add tor0
(i.e. the content of the address 5 is added tor0
). – Margaret Bloom