0

In laymen's terms, what is the difference between opcodes and operands in assembly language programs? I understand that one involves where to get the data and one involves what is to be performed on the data, but is there a way to understand it in more detail?

  • The only way to understand it in more detail is to learn about the mechanisms for getting the data and the mechanisms for operating on the data. There are lots of references to "instruction set architectures" on the net. – BobDalgleish Jan 04 '19 at 17:31

1 Answers1

4

Instructions tell the CPU what to do.  Instructions are defined as bit patterns (numbers, really); they are defined by the instruction set architecture (ISA) for the processor.

Instructions are organized into groups (of related instructions) that share an instruction format.  An instruction format (the whole group of instructions) will typically support many different operations, and, will have a well-defined set of operands — while the specific operations in the group differ, the format (e.g. of the operands) is shared among the instructions in the group.

For an example using the MIPS ISA: the MIPS R-Type instruction format is used for binary operations like add, subtract, etc… These instructions (this instruction format) has two source registers and target register for its operands.

The instruction format is easy to see in a MIPS architecture, since it is fairly simple and quite regular:

R-type format:
+--------+--------+-------+-------+------+----------+
| Opcode | Rs     | Rt    | Rd    | SA   |Func-code |
+--------+--------+-------+-------+------+----------+

I-type format:
+--------+--------+-------+-------------------------+
| Opcode | Rs     | Rt    | 2’s complement constant |
+--------+--------+-------+-------------------------+

J-type format:
+--------+------------------------------------------+
| Opcode | jump_target                              |
+--------+------------------------------------------+

The primary field, the Opcode tells us (and the processor) what the instruction format is. For all R-Type instructions, the Opcode is simply 0 (6-bits of zeros), and the exact operation (e.g. add, sub, and, or) is specified using secondary opcode fields, here called the Func-code and the SA.

Other instruction sets (e.g. x86/x64) follow the same principles but also introduce variable length instructions as well as additional and more complex instruction formats.

Erik Eidt
  • 33,747
  • How do you go from Assembly to something higher like C? – Cody Rutscher Jan 04 '19 at 02:53
  • We create a mapping of declarations of variables in one language to similar in the other. We also create mappings of statements and expressions into similar in the other languages. If, while, for statements in C translate into conditional testing & branching in assembly/machine code. Computational expressions and assignments, translate into computational machine instructions and assignments applied to the mappings of the variables. Where C implicitly uses temporaries (a * b + c * d requires temporarily storing the result of one of the multiplies) in machine code this is more explicit. – Erik Eidt Jan 04 '19 at 05:12
  • @CodyRutscher Note that there's no requirement that things be like this. You could build a machine having randomly shuffled all the meanings of every possible MIPS instructions to a different numeric representation. You wouldn't have identifiable parts of the instruction as "opcode" or "operand", and it'd be awful to build and write code for, but it would be possible. – Caleth Jan 04 '19 at 14:02