Opcode
An opcode (abbreviated from operation code, also known as instruction machine code, instruction code, instruction syllable, instruction parcel or opstring) is the portion of a machine language instruction that specifies the operation to be performed.
A machine instruction usually has:
- Opcode → the action (e.g., add, move, jump)
- Operands → the data or locations involved
Example
In a simplified assembly-like instruction:
ADD R1, R2
ADD → opcode (tells CPU to perform addition) R1, R2 → operands (the values/registers being used)
What opcodes do
Opcodes control all low-level operations, like:
- Arithmetic (ADD, SUB)
- Data movement (MOV, LOAD)
- Control flow (JMP, CALL)
- Logic (AND, OR, XOR)
Why they matter
- They’re the lowest-level instructions a CPU understands
- Compilers translate high-level code (like Python or C) into sequences of opcodes
- Different CPU architectures (x86, ARM, etc.) have different opcode sets
Example: x86 Machine Code
Here’s a simple assembly instruction:
MOV EAX, 1
Machine code (in hex):
B8 01 00 00 00
Breakdown:
B8 → opcode (means “move immediate value into EAX”) 01 00 00 00 → operand (the number 1, stored in little-endian format)
So the CPU reads:
“Take the value 1 and put it into register EAX”
Another Example
ADD EAX, EBX
Machine code:
01 D8
Breakdown:
01 → opcode for ADD D8 → specifies registers (EAX and EBX)
What It Looks Like in Memory
A program in memory is just a sequence of bytes:
B8 01 00 00 00 01 D8
The CPU reads byte by byte:
- Sees B8 → knows it’s a MOV instruction
- Reads next 4 bytes → operand
- Then sees 01 → ADD instruction
- Continues…
Key Insight
- Opcodes are just numbers (bytes)
- The CPU has a built-in “decoder” that interprets them
- Some opcodes are 1 byte, others are multiple bytes
- Extra bytes may define registers, memory addressing, etc.