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

mload

Load word from memory.

Parameters

evm : The current EVM frame.

def mload(evm: Evm) -> None:
92
    """
93
    Load word from memory.
94
95
    Parameters
96
    ----------
97
    evm :
98
        The current EVM frame.
99
100
    """
101
    # STACK
102
    start_position = pop(evm.stack)
103
104
    # GAS
105
    extend_memory = calculate_gas_extend_memory(
106
        evm.memory, [(start_position, U256(32))]
107
    )
108
    charge_gas(evm, GAS_VERY_LOW + extend_memory.cost)
109
110
    # OPERATION
111
    evm.memory += b"\x00" * extend_memory.expand_by
112
    value = U256.from_be_bytes(
113
        memory_read_bytes(evm.memory, start_position, U256(32))
114
    )
115
    push(evm.stack, value)
116
117
    # PROGRAM COUNTER
118
    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:
122
    """
123
    Push the size of active memory in bytes onto the stack.
124
125
    Parameters
126
    ----------
127
    evm :
128
        The current EVM frame.
129
130
    """
131
    # STACK
132
    pass
133
134
    # GAS
135
    charge_gas(evm, GAS_BASE)
136
137
    # OPERATION
138
    push(evm.stack, U256(len(evm.memory)))
139
140
    # PROGRAM COUNTER
141
    evm.pc += Uint(1)