ethereum.forks.bpo1.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:
26
    <snip>
37
    # STACK
38
    start_position = pop(evm.stack)
39
    value = pop(evm.stack).to_be_bytes32()
40
41
    # GAS
42
    extend_memory = calculate_gas_extend_memory(
43
        evm.memory, [(start_position, U256(len(value)))]
44
    )
45
46
    charge_gas(evm, GasCosts.OPCODE_MSTORE_BASE + extend_memory.cost)
47
48
    # OPERATION
49
    evm.memory += b"\x00" * extend_memory.expand_by
50
    memory_write(evm.memory, start_position, value)
51
52
    # PROGRAM COUNTER
53
    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:
57
    <snip>
68
    # STACK
69
    start_position = pop(evm.stack)
70
    value = pop(evm.stack)
71
72
    # GAS
73
    extend_memory = calculate_gas_extend_memory(
74
        evm.memory, [(start_position, U256(1))]
75
    )
76
77
    charge_gas(evm, GasCosts.OPCODE_MSTORE8_BASE + extend_memory.cost)
78
79
    # OPERATION
80
    evm.memory += b"\x00" * extend_memory.expand_by
81
    normalized_bytes_value = Bytes([value & U256(0xFF)])
82
    memory_write(evm.memory, start_position, normalized_bytes_value)
83
84
    # PROGRAM COUNTER
85
    evm.pc += Uint(1)

mload

Loads a word from memory.

Parameters

evm : The current EVM frame.

def mload(​evm: Evm​) -> None:
89
    <snip>
98
    # STACK
99
    start_position = pop(evm.stack)
100
101
    # GAS
102
    extend_memory = calculate_gas_extend_memory(
103
        evm.memory, [(start_position, U256(32))]
104
    )
105
    charge_gas(evm, GasCosts.OPCODE_MLOAD_BASE + extend_memory.cost)
106
107
    # OPERATION
108
    evm.memory += b"\x00" * extend_memory.expand_by
109
    value = U256.from_be_bytes(
110
        memory_read_bytes(evm.memory, start_position, U256(32))
111
    )
112
    push(evm.stack, value)
113
114
    # PROGRAM COUNTER
115
    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:
119
    <snip>
128
    # STACK
129
    pass
130
131
    # GAS
132
    charge_gas(evm, GasCosts.OPCODE_MSIZE)
133
134
    # OPERATION
135
    push(evm.stack, U256(len(evm.memory)))
136
137
    # PROGRAM COUNTER
138
    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:
142
    <snip>
151
    # STACK
152
    destination = pop(evm.stack)
153
    source = pop(evm.stack)
154
    length = pop(evm.stack)
155
156
    # GAS
157
    words = ceil32(Uint(length)) // Uint(32)
158
    copy_gas_cost = GasCosts.OPCODE_COPY_PER_WORD * words
159
160
    extend_memory = calculate_gas_extend_memory(
161
        evm.memory, [(source, length), (destination, length)]
162
    )
163
    charge_gas(
164
        evm,
165
        GasCosts.OPCODE_MCOPY_BASE + copy_gas_cost + extend_memory.cost,
166
    )
167
168
    # OPERATION
169
    evm.memory += b"\x00" * extend_memory.expand_by
170
    value = memory_read_bytes(evm.memory, source, length)
171
    memory_write(evm.memory, destination, value)
172
173
    # PROGRAM COUNTER
174
    evm.pc += Uint(1)