ethereum.shanghai.vm.instructions.memoryethereum.cancun.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:
32
    """
33
    Stores a word to memory.
34
    This also expands the memory, if the memory is
35
    insufficient to store the word.
36
37
    Parameters
38
    ----------
39
    evm :
40
        The current EVM frame.
41
42
    """
43
    # STACK
44
    start_position = pop(evm.stack)
45
    value = pop(evm.stack).to_be_bytes32()
46
47
    # GAS
48
    extend_memory = calculate_gas_extend_memory(
49
        evm.memory, [(start_position, U256(len(value)))]
50
    )
51
52
    charge_gas(evm, GAS_VERY_LOW + extend_memory.cost)
53
54
    # OPERATION
55
    evm.memory += b"\x00" * extend_memory.expand_by
56
    memory_write(evm.memory, start_position, value)
57
58
    # PROGRAM COUNTER
59
    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:
63
    """
64
    Stores a byte to memory.
65
    This also expands the memory, if the memory is
66
    insufficient to store the word.
67
68
    Parameters
69
    ----------
70
    evm :
71
        The current EVM frame.
72
73
    """
74
    # STACK
75
    start_position = pop(evm.stack)
76
    value = pop(evm.stack)
77
78
    # GAS
79
    extend_memory = calculate_gas_extend_memory(
80
        evm.memory, [(start_position, U256(1))]
81
    )
82
83
    charge_gas(evm, GAS_VERY_LOW + extend_memory.cost)
84
85
    # OPERATION
86
    evm.memory += b"\x00" * extend_memory.expand_by
87
    normalized_bytes_value = Bytes([value & U256(0xFF)])
88
    memory_write(evm.memory, start_position, normalized_bytes_value)
89
90
    # PROGRAM COUNTER
91
    evm.pc += Uint(1)

mload

Load word from memory.

Parameters

evm : The current EVM frame.

def mload(evm: Evm) -> None:
95
    """
96
    Load word from memory.
97
98
    Parameters
99
    ----------
100
    evm :
101
        The current EVM frame.
102
103
    """
104
    # STACK
105
    start_position = pop(evm.stack)
106
107
    # GAS
108
    extend_memory = calculate_gas_extend_memory(
109
        evm.memory, [(start_position, U256(32))]
110
    )
111
    charge_gas(evm, GAS_VERY_LOW + extend_memory.cost)
112
113
    # OPERATION
114
    evm.memory += b"\x00" * extend_memory.expand_by
115
    value = U256.from_be_bytes(
116
        memory_read_bytes(evm.memory, start_position, U256(32))
117
    )
118
    push(evm.stack, value)
119
120
    # PROGRAM COUNTER
121
    evm.pc += Uint(1)

msize

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

Parameters

evm : The current EVM frame.

def msize(evm: Evm) -> None:
125
    """
126
    Push the size of active memory in bytes onto the stack.
127
128
    Parameters
129
    ----------
130
    evm :
131
        The current EVM frame.
132
133
    """
134
    # STACK
135
    pass
136
137
    # GAS
138
    charge_gas(evm, GAS_BASE)
139
140
    # OPERATION
141
    push(evm.stack, U256(len(evm.memory)))
142
143
    # PROGRAM COUNTER
144
    evm.pc += Uint(1)

mcopy

Copy the bytes in memory from one location to another.

Parameters

evm : The current EVM frame.

def mcopy(evm: Evm) -> None:
148
    """
149
    Copy the bytes in memory from one location to another.
150
151
    Parameters
152
    ----------
153
    evm :
154
        The current EVM frame.
155
156
    """
157
    # STACK
158
    destination = pop(evm.stack)
159
    source = pop(evm.stack)
160
    length = pop(evm.stack)
161
162
    # GAS
163
    words = ceil32(Uint(length)) // Uint(32)
164
    copy_gas_cost = GAS_COPY * words
165
166
    extend_memory = calculate_gas_extend_memory(
167
        evm.memory, [(source, length), (destination, length)]
168
    )
169
    charge_gas(evm, GAS_VERY_LOW + copy_gas_cost + extend_memory.cost)
170
171
    # OPERATION
172
    evm.memory += b"\x00" * extend_memory.expand_by
173
    value = memory_read_bytes(evm.memory, source, length)
174
    memory_write(evm.memory, destination, value)
175
176
    # PROGRAM COUNTER
177
    evm.pc += Uint(1)