🧠 From Code to Machine: The Layers
In this guide, we’ll follow a basic XOR operation as it transforms from human-readable logic into electrical operations at the processor level. This is where code becomes data — and where the illusion of abstraction breaks down.
We’ll look at:
- High-Level Code (Python, C)
- Assembly Instructions (x86)
- Machine Code (Hexadecimal and Binary)
- Physical Execution (What transistors do)
1. 🧾 High-Level Code (What We Write)
Python — Abstract and Friendly
def xor(a, b):
return a ^ b
- Result:
xor(5, 3)
→6
- Explanation: Binary:
101 ^ 011 = 110
C — Closer to the Metal
int xor(int a, int b) {
return a ^ b;
}
- The same operation, but compiled into native machine instructions.
2. 🛠 Assembly Code (What the CPU Almost Understands)
Once compiled from C, this function turns into x86-64 assembly:
xor:
mov eax, edi ; Move a (1st arg) into eax
xor eax, esi ; XOR with b (2nd arg)
ret ; Return result
- Registers:
eax
holds the result,edi
andesi
are arguments. - The core XOR is a single native instruction:
xor eax, esi
3. 💾 Machine Code (What the CPU Actually Sees)
The compiled version becomes raw bytes:
89 F8 31 F0 C3
Byte(s) | Binary | Instruction |
---|---|---|
89 F8 | 10001001 11111000 | mov eax, edi |
31 F0 | 00110001 11110000 | xor eax, esi |
C3 | 11000011 | ret |
This is pure binary. No comments, no functions — just data.
4. ⚡ Transistor Logic (What the CPU Really Does)
Every byte from above is fetched and interpreted:
89 F8
— copyedi
→eax
31 F0
— XOReax
withesi
C3
— return the result
Under the hood, XOR is implemented in silicon as:
A XOR B = (A AND NOT B) OR (NOT A AND B)
Billions of tiny transistors perform this operation using voltage states. These gates are hardwired into the CPU.
🔍 Why This Matters
- The same logic works across languages.
- XOR takes 1 line in Python, 1 instruction in Assembly, and 1 gate in hardware.
- All abstraction layers are illusions — code is data.
Python Code | C Code | Assembly (x86-64) | Hex Bytes | Binary |
---|---|---|---|---|
a ^ b | a ^ b | mov eax, edi | 89 F8 | 10001001 11111000 |
xor eax, esi | 31 F0 | 00110001 11110000 | ||
ret | C3 | 11000011 |