ethereum.forks.amsterdam.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:
31
    <snip>
42
    # STACK
43
    start_position = pop(evm.stack)
44
    value = pop(evm.stack).to_be_bytes32()
45
46
    # GAS
47
    extend_memory = calculate_gas_extend_memory(
48
        evm.memory, [(start_position, U256(len(value)))]
49
    )
50
51
    charge_gas(evm, GasCosts.OPCODE_MSTORE_BASE + extend_memory.cost)
52
53
    # OPERATION
54
    evm.memory += b"\x00" * extend_memory.expand_by
55
    memory_write(evm.memory, start_position, value)
56
57
    # PROGRAM COUNTER
58
    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:
62
    <snip>
73
    # STACK
74
    start_position = pop(evm.stack)
75
    value = pop(evm.stack)
76
77
    # GAS
78
    extend_memory = calculate_gas_extend_memory(
79
        evm.memory, [(start_position, U256(1))]
80
    )
81
82
    charge_gas(evm, GasCosts.OPCODE_MSTORE8_BASE + extend_memory.cost)
83
84
    # OPERATION
85
    evm.memory += b"\x00" * extend_memory.expand_by
86
    normalized_bytes_value = Bytes([value & U256(0xFF)])
87
    memory_write(evm.memory, start_position, normalized_bytes_value)
88
89
    # PROGRAM COUNTER
90
    evm.pc += Uint(1)

mload

Loads a word from memory.

Parameters

evm : The current EVM frame.

def mload(evm: Evm) -> None:
94
    <snip>
103
    # STACK
104
    start_position = pop(evm.stack)
105
106
    # GAS
107
    extend_memory = calculate_gas_extend_memory(
108
        evm.memory, [(start_position, U256(32))]
109
    )
110
    charge_gas(evm, GasCosts.OPCODE_MLOAD_BASE + extend_memory.cost)
111
112
    # OPERATION
113
    evm.memory += b"\x00" * extend_memory.expand_by
114
    value = U256.from_be_bytes(
115
        memory_read_bytes(evm.memory, start_position, U256(32))
116
    )
117
    push(evm.stack, value)
118
119
    # PROGRAM COUNTER
120
    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:
124
    <snip>
133
    # STACK
134
    pass
135
136
    # GAS
137
    charge_gas(evm, GasCosts.OPCODE_MSIZE)
138
139
    # OPERATION
140
    push(evm.stack, U256(len(evm.memory)))
141
142
    # PROGRAM COUNTER
143
    evm.pc += Uint(1)

mcopy

Copies the bytes in memory from one location to another.

Parameters

evm : The current EVM frame.

def mcopy(evm: Evm) -> None:
147
    <snip>
156
    # STACK
157
    destination = pop(evm.stack)
158
    source = pop(evm.stack)
159
    length = pop(evm.stack)
160
161
    # GAS
162
    words = ceil32(Uint(length)) // Uint(32)
163
    copy_gas_cost = ExecutionGas(GasCosts.OPCODE_COPY_PER_WORD * words)
164
165
    extend_memory = calculate_gas_extend_memory(
166
        evm.memory, [(source, length), (destination, length)]
167
    )
168
    charge_gas(
169
        evm,
170
        GasCosts.OPCODE_MCOPY_BASE + copy_gas_cost + extend_memory.cost,
171
    )
172
173
    # OPERATION
174
    evm.memory += b"\x00" * extend_memory.expand_by
175
    value = memory_read_bytes(evm.memory, source, length)
176
    memory_write(evm.memory, destination, value)
177
178
    # PROGRAM COUNTER
179
    evm.pc += Uint(1)