0
$\begingroup$

I am studying assembly language. I feel very difficult in understanding few instruction.

ADD R1,R2,[R3]

ADD R1, R2, R3

What is the difference between these two instructions. I think second instruction takes the value in R3 register and add it to the value in R2 register and store it in R1 register. Then what does it mean by the first instruction. How does encoding works ? How does CPU knows the difference between two instruction?

$\endgroup$
1
  • 1
    $\begingroup$ This is a case of indirect addressing. R3 does not contain an operand, but the address of an operand, to be fetched from memory. Among others, this enables array access at arbitrary offsets, made possible by address computation. $\endgroup$
    – user16034
    Mar 15, 2022 at 21:39

2 Answers 2

1
$\begingroup$

The assembly instructions you learn are used by the assembler to generate machine code. Every operation has a unique machine code. The 2 add operations are different in this case as their addressing modes are different, but their opcode would be the same as the operation to be done remains same in both cases, just the way the operand is fetched differs. In the first one R3 contains a memory address which in turn contains some data that will be added to content of R2 and in the second one the content of R3 is directly added to the content of R2. In both the cases their result will be stored in R1.

$\endgroup$
1
$\begingroup$

R3 denotes the content of the register R3. [R3] denotes the content of the memory at the address contained in R3. (This implies that the registers are able to store addresses.) This mechanism is call indirect addressing. Without it no serious program could be written and computers would be of little use.


As regards the encoding, I don't think that the details of a particular implementation are instructive. But of course, the information contained in the assembly text (the desired addressing mode) is converted to some bit(s) in the instruction opcode.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.