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:0x53Introduced: 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:
offset (top)value
Stack Output:
(empty)
Gas Cost: 3 + memory expansion costOperation:
memory[offset] = value & 0xFF
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
import { mstore8 } from '@tevm/voltaire/evm/instructions/memory';import { createFrame } from '@tevm/voltaire/evm/Frame';const frame = createFrame();frame.stack.push(0n); // offsetframe.stack.push(0x42n); // value (only 0x42 written)const err = mstore8(frame);console.log(frame.memory.get(0)); // 0x42console.log(frame.pc); // 1 (incremented)
Truncation of Multi-Byte Value
const frame = createFrame();// Value with multiple bytes - only lowest byte writtenconst value = 0x123456789ABCDEFn;frame.stack.push(0n); // offsetframe.stack.push(value); // valuemstore8(frame);console.log(frame.memory.get(0)); // 0xEF (only lowest byte)
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
const frame = createFrame();// Write at byte 31 (last byte of first word)frame.stack.push(31n);frame.stack.push(0x99n);mstore8(frame);// Expands to 32 bytes (1 word)console.log(frame.memorySize); // 32console.log(frame.memory.get(31)); // 0x99
Writing Beyond First Word
const frame = createFrame();// Write at byte 32 (first byte of second word)frame.stack.push(32n);frame.stack.push(0x77n);mstore8(frame);// Expands to 64 bytes (2 words)console.log(frame.memorySize); // 64console.log(frame.memory.get(32)); // 0x77
Multiple Writes to Same Word
const frame = createFrame();// Write different bytes in same wordframe.stack.push(5n);frame.stack.push(0x11n);mstore8(frame);frame.stack.push(10n);frame.stack.push(0x22n);mstore8(frame);frame.stack.push(15n);frame.stack.push(0x33n);mstore8(frame);console.log(frame.memory.get(5)); // 0x11console.log(frame.memory.get(10)); // 0x22console.log(frame.memory.get(15)); // 0x33
const frame = createFrame();// Only one value on stackframe.stack.push(0n);const err = mstore8(frame);console.log(err); // { type: "StackUnderflow" }
Common Usage
Building Packed Struct in Memory
assembly { let offset := 0 // Pack multiple small values mstore8(offset, byte0) // 1 byte mstore8(add(offset, 1), byte1) mstore8(add(offset, 2), byte2) mstore8(add(offset, 3), byte3) // Continue with MSTORE for larger values mstore(add(offset, 4), word4)}
Encode String Data
assembly { // Encode string prefix (length in first slot) let str := 0x40 mstore(str, stringLength) // Write individual characters let offset := add(str, 0x20) for { let i := 0 } lt(i, stringLength) { i := add(i, 1) } { mstore8(add(offset, i), charByte) }}
Sparse Memory Allocation
assembly { // Write sparse bytes without padding full words mstore8(0x00, 0xAA) mstore8(0x100, 0xBB) // Skip 256 bytes mstore8(0x200, 0xCC) // Skip another 256 bytes // More memory-efficient than MSTORE}
Low-Level Bit Setting
assembly { let byte_offset := 10 let current := mload8(byte_offset) let updated := or(current, 0x01) // Set lowest bit mstore8(byte_offset, updated)}
Memory Safety
Write safety properties:
No side effects: Writing memory doesn’t affect storage or state