1/5
_Follow along with this video:_ --- ### Understanding Stack Operations in Programming Let's look at a practical example of how the stack operates with two common op codes `PUSH1` and `ADD`. To recap what we saw in a previous lesson, the `ADD` op code takes the top two items on the stack and returns the sum of these items - placing this sum on the top of the stack. You may be asking **"How does data get added to the stack to begin with?"**. This is where `PUSH` comes in. By navigating to [**evm.codes**](https://www.evm.codes/?fork=shanghai) we can see that there is a different `PUSH` code, pending the byte size of the data we're adding to the stack! ### Understanding PUSH and ADD We'll start by considering how to add a 1 byte object to the stack, for this we'd use the `PUSH1` op code. ::image{src='/formal-verification-1/11-push-and-add-opcodes/push-and-add-opcodes-1.png' style='width: 75%; height: auto;'} If we were then to call the `ADD` op code, it's going to take the top 2 items on our stack, sum them and add this sum back to the top of our stack. ::image{src='/formal-verification-1/11-push-and-add-opcodes/push-and-add-opcodes-2.png' style='width: 75%; height: auto;'} > **Remember:** Were there _three_ items in our stack, the `ADD` op code would only sum the top two items, the result would be pushed to the top of the stack, leaving the first item in our stack untouched. ### Wrap Up Here we've seen an example of two very common op codes. - PUSH - adds items to the stack - ADD - removes the top two items from the stack, and adds their sum to the top of the stack I encourage you to familiarize yourself with more of the stack input/outputs on evm.codes. In later lessons we'll look more closely at op codes such as MSTORE and SSTORE which remove things from the stack and write them to memory!
A simple explanation of how PUSH and ADD opcodes work in EVM - In this lesson, we discuss the role of the stack in Ethereum Virtual Machine (EVM) execution and the PUSH and ADD opcodes. We look at how to understand and interpret stack input and output, and we see how these two opcodes function in a practical application.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Solidity Developer
Assembly and Formal VerificationDuration: 30min
Duration: 4h 38min
Duration: 3h 57min
Duration: 1h 56min
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar