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

mload

Load word from memory.

Parameters

evm : The current EVM frame.

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

msize

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

Parameters

evm : The current EVM frame.

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