Ethereum Virtual Machine (EVM) Memory Instructions

Introduction

Implementations of the EVM Memory instructions.

Module Contents

Functions

mstore

Stores a word to memory.

mstore8

Stores a byte to memory.

mload

Load word from memory.

msize

Push the size of active memory in bytes onto the stack.

Module Details

mstore

mstore(evm: ethereum.homestead.vm.Evm)None

Stores a word to memory. This also expands the memory, if the memory is insufficient to store the word.

Parameters

evm – The current EVM frame.

def mstore(evm: Evm) -> None:
    # STACK
    start_position = pop(evm.stack)
    value = pop(evm.stack).to_be_bytes32()

    # GAS
    extend_memory = calculate_gas_extend_memory(
        evm.memory, [(start_position, U256(len(value)))]
    )

    charge_gas(evm, GAS_VERY_LOW + extend_memory.cost)

    # OPERATION
    evm.memory += b"\x00" * extend_memory.expand_by
    memory_write(evm.memory, start_position, value)

    # PROGRAM COUNTER
    evm.pc += 1

mstore8

mstore8(evm: ethereum.homestead.vm.Evm)None

Stores a byte to memory. This also expands the memory, if the memory is insufficient to store the word.

Parameters

evm – The current EVM frame.

def mstore8(evm: Evm) -> None:
    # STACK
    start_position = pop(evm.stack)
    value = pop(evm.stack)

    # GAS
    extend_memory = calculate_gas_extend_memory(
        evm.memory, [(start_position, U256(1))]
    )

    charge_gas(evm, GAS_VERY_LOW + extend_memory.cost)

    # OPERATION
    evm.memory += b"\x00" * extend_memory.expand_by
    normalized_bytes_value = Bytes([value & U8_MAX_VALUE])
    memory_write(evm.memory, start_position, normalized_bytes_value)

    # PROGRAM COUNTER
    evm.pc += 1

mload

mload(evm: ethereum.homestead.vm.Evm)None

Load word from memory.

Parameters

evm – The current EVM frame.

def mload(evm: Evm) -> None:
    # STACK
    start_position = pop(evm.stack)

    # GAS
    extend_memory = calculate_gas_extend_memory(
        evm.memory, [(start_position, U256(32))]
    )
    charge_gas(evm, GAS_VERY_LOW + extend_memory.cost)

    # OPERATION
    evm.memory += b"\x00" * extend_memory.expand_by
    value = U256.from_be_bytes(
        memory_read_bytes(evm.memory, start_position, U256(32))
    )
    push(evm.stack, value)

    # PROGRAM COUNTER
    evm.pc += 1

msize

msize(evm: ethereum.homestead.vm.Evm)None

Push the size of active memory in bytes onto the stack.

Parameters

evm – The current EVM frame.

def msize(evm: Evm) -> None:
    # STACK
    pass

    # GAS
    charge_gas(evm, GAS_BASE)

    # OPERATION
    push(evm.stack, U256(len(evm.memory)))

    # PROGRAM COUNTER
    evm.pc += 1