ethereum.forks.gray_glacier.vm.instructions.memoryethereum.forks.paris.vm.instructions.memory

Ethereum Virtual Machine (EVM) Memory Instructions.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementations of the EVM Memory instructions.

mstore

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:
24
    <snip>
35
    # STACK
36
    start_position = pop(evm.stack)
37
    value = pop(evm.stack).to_be_bytes32()
38
39
    # GAS
40
    extend_memory = calculate_gas_extend_memory(
41
        evm.memory, [(start_position, U256(len(value)))]
42
    )
43
44
    charge_gas(evm, GasCosts.OPCODE_MSTORE_BASE + extend_memory.cost)
45
46
    # OPERATION
47
    evm.memory += b"\x00" * extend_memory.expand_by
48
    memory_write(evm.memory, start_position, value)
49
50
    # PROGRAM COUNTER
51
    evm.pc += Uint(1)

mstore8

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:
55
    <snip>
66
    # STACK
67
    start_position = pop(evm.stack)
68
    value = pop(evm.stack)
69
70
    # GAS
71
    extend_memory = calculate_gas_extend_memory(
72
        evm.memory, [(start_position, U256(1))]
73
    )
74
75
    charge_gas(evm, GasCosts.OPCODE_MSTORE8_BASE + extend_memory.cost)
76
77
    # OPERATION
78
    evm.memory += b"\x00" * extend_memory.expand_by
79
    normalized_bytes_value = Bytes([value & U256(0xFF)])
80
    memory_write(evm.memory, start_position, normalized_bytes_value)
81
82
    # PROGRAM COUNTER
83
    evm.pc += Uint(1)

mload

Loads a word from memory.

Parameters

evm : The current EVM frame.

def mload(​evm: Evm​) -> None:
87
    <snip>
96
    # STACK
97
    start_position = pop(evm.stack)
98
99
    # GAS
100
    extend_memory = calculate_gas_extend_memory(
101
        evm.memory, [(start_position, U256(32))]
102
    )
103
    charge_gas(evm, GasCosts.OPCODE_MLOAD_BASE + extend_memory.cost)
104
105
    # OPERATION
106
    evm.memory += b"\x00" * extend_memory.expand_by
107
    value = U256.from_be_bytes(
108
        memory_read_bytes(evm.memory, start_position, U256(32))
109
    )
110
    push(evm.stack, value)
111
112
    # PROGRAM COUNTER
113
    evm.pc += Uint(1)

msize

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

Parameters

evm : The current EVM frame.

def msize(​evm: Evm​) -> None:
117
    <snip>
126
    # STACK
127
    pass
128
129
    # GAS
130
    charge_gas(evm, GasCosts.OPCODE_MSIZE)
131
132
    # OPERATION
133
    push(evm.stack, U256(len(evm.memory)))
134
135
    # PROGRAM COUNTER
136
    evm.pc += Uint(1)