Skip to main content
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: 0x53 Introduced: Frontier (EVM genesis) MSTORE8 writes a single byte to memory at the specified offset. Only the least significant byte (bits 0-7) of the stack value is written; all higher-order bits are truncated. This is the only opcode for single-byte writes in the EVM.

Specification

Stack Input:
Stack Output:
Gas Cost: 3 + memory expansion cost Operation:

Behavior

MSTORE8 pops two values: offset (top) and value (next). It extracts the least significant byte of value and writes it to memory at offset.
  • Offset is interpreted as unsigned 256-bit integer
  • Only lowest 8 bits of value are written (all other bits ignored)
  • Memory automatically expands to accommodate write (quadratic cost)
  • Overwrites single byte without affecting adjacent bytes
  • More gas-efficient than MSTORE for single-byte writes (same base cost, smaller memory footprint)

Examples

Basic Single-Byte Write

Truncation of Multi-Byte Value

Write All Ones Byte

Write Zero Byte

Sequential Byte Writes

Partial Overwrite

Gas Cost

Base cost: 3 gas (GasFastestStep) Memory expansion: Quadratic based on access range Formula:
Examples:
  • Writing byte 0: 1 word, no prior expansion: 3 gas
  • Writing byte 31: 1 word, no prior expansion: 3 gas (same word)
  • Writing byte 32: 2 words, 1 word prior: 3 + (4 - 1) = 6 gas
  • Writing bytes 0-255: 8 words: 3 + (64 - 1) = 66 gas
MSTORE8 is more efficient than MSTORE for single-byte writes since it doesn’t force 32-byte alignment in memory updates (though memory is still expanded to word boundaries).

Edge Cases

Writing to Word Boundary

Writing Beyond First Word

Multiple Writes to Same Word

Out of Gas

Stack Underflow

Common Usage

Building Packed Struct in Memory

Encode String Data

Sparse Memory Allocation

Low-Level Bit Setting

Memory Safety

Write safety properties:
  • No side effects: Writing memory doesn’t affect storage or state
  • Byte-level granularity: Single-byte writes don’t affect adjacent bytes
  • No initialization races: Single-byte write triggers memory expansion if needed
Applications must ensure offset correctness:

Implementation

Testing

Test Coverage

Edge Cases Tested

  • Single byte write
  • Multi-byte truncation
  • Zero write
  • Non-aligned offset
  • Word boundary expansion
  • Adjacent byte preservation
  • Stack underflow/overflow
  • Out of gas conditions

Security Considerations

Byte-Level Granularity Bugs

Memory Layout Assumptions

Benchmarks

MSTORE8 is among the fastest EVM operations: Relative performance:
  • MSTORE8: 1.0x baseline
  • MSTORE: 1.0x (same base cost)
  • MLOAD: 1.0x (similar cost)
  • SSTORE: 100x+ slower
Memory expansion efficiency:
  • Single byte write: Same word-boundary expansion as 32-byte write
  • Sequential byte writes more efficient than equivalent MSTORE operations
  • Useful for sparse memory allocation

References