freecoding.school100% FREE · NO SIGNUP
The EVM CoreISSUE #25 of 48

yul · inline assembly fundamentals

SoliaVSThe Reentrancy Reaper
Solia saysYul is the EVM’s assembly-like intermediate language — explicit, low-level, no high-level types. inline assembly drops down to it when Solidity is too expensive or not flexible enough.

Yul is an explicit, low-level intermediate language for the EVM. Where Solidity has a + b, Yul has add(a, b) as a function call; storage is sload(slot), memory is mload(offset), and there are no high-level types. Inside Solidity you drop into Yul via assembly { … } blocks to bypass the compiler’s safety and gas overhead.

Use Yul when you need bit-level control (shifts, packing, manual layout), when Solidity adds expensive checks you can prove are unnecessary, or when you write libraries used by tens of thousands of contracts. The trade-off is loss of type safety and easier bugs. The demo shows Yul ops alongside the equivalent Solidity.

Power-ups you unlock

The Reentrancy Reaper attacks — common mistakes

Boss battleShow three Yul ops (add, mul, mstore) and their Solidity equivalents.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>
// Yul is explicit: every op is a function call, every memory cell is named by offset.
const mem = {};
const ops = [
  { yul:'let a := 5',                 act:()=> 5,                       label:'a' },
  { yul:'let b := 7',                 act:()=> 7,                       label:'b' },
  { yul:'let c := add(a, b)',         act:(a,b)=> a + b,                label:'c' },
  { yul:'let d := mul(c, 2)',         act:(c)=> c * 2,                  label:'d' },
  { yul:'mstore(0, d)',               act:(d)=> { mem[0] = d; return d; }, label:'memory[0]' },
  { yul:'let e := mload(0)',          act:()=> mem[0],                  label:'e' }
];
const v = {};
const log = ['Yul op'.padEnd(28) + 'result'];
v.a = ops[0].act();           log.push('  ' + ops[0].yul.padEnd(28) + 'a = ' + v.a);
v.b = ops[1].act();           log.push('  ' + ops[1].yul.padEnd(28) + 'b = ' + v.b);
v.c = ops[2].act(v.a, v.b);   log.push('  ' + ops[2].yul.padEnd(28) + 'c = ' + v.c);
v.d = ops[3].act(v.c);        log.push('  ' + ops[3].yul.padEnd(28) + 'd = ' + v.d);
ops[4].act(v.d);              log.push('  ' + ops[4].yul.padEnd(28) + 'memory[0] = ' + mem[0]);
v.e = ops[5].act();           log.push('  ' + ops[5].yul.padEnd(28) + 'e = ' + v.e);
log.push('');
log.push('Solidity:  uint c = a + b;   uint d = c * 2;');
log.push('Yul:       let c := add(a, b)   let d := mul(c, 2)');
document.getElementById('o').textContent = log.join('\n');
</script></body></html>
▶ Open the interactive comic issue
‹ Alt-Vms · Movevm · Svm · CairovmAbi Encoding · Encode · EncodePacked · Decode ›