1

For opcodes like dup2, how does the EVM read the stack value that is not at the top of the stack for this kind of operation?

1 Answer 1

1

The Stack is implemented as part of the client. So let’s say we consider Geth (the most popular client).

In this case, the Stack is simply represented as a list behind the scenes. Literally just a list: https://github.com/ethereum/go-ethereum/blob/master/core/vm/stack.go#L34-L36

The dup2 instruction is an instruction among what they call the dup family instructions (just a fancy way of saying: you can have dup with all kinds of numbers like dup2, dup3, and so on).

All that dupN does behind the scenes is to take the N’th stack element down from the top and push it to the top.

In Go, this is simply implemented as such: Take the <length of stack> - Nth element (btw. the top of the stack is at index - 1 — aka the most recent item on the stack is at the end of the list since we append, and not prepend to the list/stack).

We can see the literal dup2 code right here:

https://github.com/ethereum/go-ethereum/blob/master/core/vm/stack.go#L72

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.