This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0x52Introduced: Frontier (EVM genesis)MSTORE writes a 32-byte word to memory at the specified offset. The value is taken from the stack as a 256-bit unsigned integer and written in big-endian order. This is the primary mechanism for writing arbitrary data to memory during execution.
Specification
Stack Input:
offset (top)value
Stack Output:
(empty)
Gas Cost: 3 + memory expansion costOperation:
memory[offset:offset+32] = value (big-endian)
Behavior
MSTORE pops two values: offset (top of stack) and value (next). It writes the 32-byte representation of value to memory starting at offset.
Offset is interpreted as unsigned 256-bit integer
Value is written as 32 bytes in big-endian order (most significant byte first)
Memory automatically expands to accommodate write (quadratic cost)
Overwrites existing memory without checking
All 32 bytes are always written (no partial writes)
const frame = createFrame();const allOnes = (1n << 256n) - 1n; // 0xFFFF...FFFFframe.stack.push(0n); // offsetframe.stack.push(allOnes); // valuemstore(frame);// All 32 bytes should be 0xFFfor (let i = 0; i < 32; i++) { console.log(frame.memory.get(i)); // 0xFF}
Write Zero (Clear Memory)
const frame = createFrame();// Pre-populate memoryfor (let i = 0; i < 32; i++) { frame.memory.set(i, 0xAA);}// Clear with MSTORE 0frame.stack.push(0n); // offsetframe.stack.push(0n); // value (zero)mstore(frame);// All bytes cleared to 0for (let i = 0; i < 32; i++) { console.log(frame.memory.get(i)); // 0}
Write at Non-Aligned Offset
const frame = createFrame();frame.stack.push(16n); // offset 16 (misaligned)frame.stack.push(0x12345678n); // valuemstore(frame);// Expands memory to 48 bytes (2 words)console.log(frame.memorySize); // 64 (next word boundary)// Bytes 16-47 contain the written valueconsole.log(frame.memory.get(44)); // 0x12console.log(frame.memory.get(47)); // 0x78
Sequential Writes
const frame = createFrame();// Write first wordframe.stack.push(0n);frame.stack.push(0x0102030405060708n);mstore(frame);// Write second wordframe.stack.push(32n);frame.stack.push(0x090a0b0c0d0e0f10n);mstore(frame);// Check both words writtenconsole.log(frame.memory.get(7)); // 0x08console.log(frame.memory.get(39)); // 0x10console.log(frame.memorySize); // 64
Gas Cost
Base cost: 3 gas (GasFastestStep)Memory expansion: Quadratic based on access rangeFormula:
const frame = createFrame();// First writeframe.stack.push(0n);frame.stack.push(0xAAAAAAAAn);mstore(frame);// Second write (overwrite)frame.stack.push(0n);frame.stack.push(0xBBBBBBBBn);mstore(frame);// Second value winsconsole.log(frame.memory.get(31)); // 0xBB
// Good: Manage free memory pointerlet ptr := mload(0x40)mstore(ptr, value)mstore(0x40, add(ptr, 0x20)) // Update for next allocation// Risky: Fixed offsetsmstore(0x100, value) // Assumes offset 0x100 is free
Implementation
TypeScript
/** * MSTORE opcode (0x52) - Write 32-byte word to memory */export function mstore(frame: FrameType): EvmError | null { // Pop offset and value from stack if (frame.stack.length < 2) { return { type: "StackUnderflow" }; } const offset = frame.stack.pop(); const value = frame.stack.pop(); // Cast offset to u32 const off = Number(offset); if (!Number.isSafeInteger(off) || off < 0) { return { type: "OutOfBounds" }; } // Calculate memory expansion const endBytes = BigInt(off + 32); const expansionCost = calculateMemoryExpansion(endBytes); // Charge gas frame.gasRemaining -= 3n + expansionCost; if (frame.gasRemaining < 0n) { return { type: "OutOfGas" }; } // Expand memory const alignedSize = Math.ceil((off + 32) / 32) * 32; frame.memorySize = Math.max(frame.memorySize, alignedSize); // Write 32 bytes in big-endian order for (let i = 0; i < 32; i++) { const byte = Number((value >> BigInt((31 - i) * 8)) & 0xFFn); frame.memory.set(off + i, byte); } // Increment PC frame.pc += 1; return null;}
Testing
Test Coverage
import { describe, it, expect } from 'vitest';import { mstore } from './0x52_MSTORE.js';describe('MSTORE (0x52)', () => { it('writes 32 bytes to memory at offset 0', () => { const frame = createFrame(); const value = 0x0102030405...n; frame.stack.push(value); frame.stack.push(0n); expect(mstore(frame)).toBeNull(); expect(frame.stack.length).toBe(0); expect(frame.memory.get(0)).toBe(0x01); expect(frame.memory.get(31)).toBe(0x20); expect(frame.pc).toBe(1); }); it('writes zero to clear memory', () => { const frame = createFrame(); // Pre-populate for (let i = 0; i < 32; i++) { frame.memory.set(i, 0xFF); } frame.stack.push(0n); frame.stack.push(0n); mstore(frame); for (let i = 0; i < 32; i++) { expect(frame.memory.get(i)).toBe(0); } }); it('expands memory to word boundary', () => { const frame = createFrame(); frame.stack.push(0xABn); frame.stack.push(1n); mstore(frame); expect(frame.memorySize).toBe(64); }); it('overwrites existing memory', () => { const frame = createFrame(); frame.stack.push(0xAAAAn); frame.stack.push(0n); mstore(frame); frame.stack.push(0xBBBBn); frame.stack.push(0n); mstore(frame); expect(frame.memory.get(31)).toBe(0xBB); }); it('returns OutOfGas when insufficient', () => { const frame = createFrame({ gasRemaining: 2n }); frame.stack.push(0xFFn); frame.stack.push(0n); expect(mstore(frame)).toEqual({ type: "OutOfGas" }); }); it('returns StackUnderflow when insufficient stack', () => { const frame = createFrame(); frame.stack.push(0n); expect(mstore(frame)).toEqual({ type: "StackUnderflow" }); });});
Edge Cases Tested
Basic write (32 bytes)
Zero write (memory clear)
Non-aligned offset
Overwrite handling
Word boundary alignment
Stack underflow/overflow
Out of gas conditions
Big-endian encoding
Security Considerations
Incorrect Free Pointer Management
// Risky: Not updating free pointerassembly { let ptr := mload(0x40) mstore(ptr, value) // Missing: mstore(0x40, add(ptr, 0x20))}// Next allocation overwrites this data!// Correct: Always update pointerassembly { let ptr := mload(0x40) mstore(ptr, value) mstore(0x40, add(ptr, 0x20))}
Memory Overlap Bugs
// Bad: Assumes memory layoutassembly { let a := mload(0x80) let b := mload(0xA0) mstore(0x80, a + b) // Overwrites a!}// Good: Use separate offsetsassembly { let a := mload(0x80) let b := mload(0xA0) mstore(0xC0, a + b) // Safe, no overlap}
Benchmarks
MSTORE is among the fastest EVM operations:Relative performance:
MSTORE (new memory): 1.0x baseline
MSTORE (existing memory): 1.0x baseline
MLOAD: 1.0x (similar cost)
SSTORE: 100x slower (storage vs memory)
Gas scaling:
First word: 3 gas
Second word: 3 gas (small expansion)
Large memory: Quadratic scaling beyond practical use