0
votes

Recently when I was studying the concept of addressing modes, the first type being immediate addressing mode, consider the example ADD #NUM1,R0 (instruction execution from left to right)

Here, is the address of NUM1 stored in Register R1? What about when we do ADD #4,R0 to make it point to the next data, when we use #4, I understood that it adds 4 to contents of Register R0. Is there a difference when we use #NUM1 and #4. Please explain!

1
The "address mode" nomenclature is a mess and the exact meaning of an assembly expression like 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 to r0). This contrasts to something like add (5), r0 (I'm inventing this syntax) where 5 is used to read from memory to fetch the number to add to r0 (i.e. the content of the address 5 is added to r0).Margaret Bloom

1 Answers

1
votes

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.