Giter Club home page Giter Club logo

yul_by_example's Introduction

Yul By Example


// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

contract Yul {
    function yulFunction() public {
        assembly {
            mstore(0x40, "Yul By Example.")
        }
    }
}

Source Code

book/src

Book URL

🔗 https://yul-by-example.vercel.app/

Run Book Locally

To run a copy of this book locally on your device, clone the repo and install mdbook via cargo (that comes with Rust). If you don't have Rust installed in your PC, you can download and setup Rust here.

To install mdbook, you can refer to this.

To display the book on your browser, run mdbook serve book/ -o from the base directory.

Contributors ✨

yul_by_example's People

Contributors

0xfps avatar perelyn-sama avatar 0xscratch avatar tanim0la avatar

Stargazers

Sambhav Jain avatar  avatar Soviet Panda avatar mmc avatar Emmanuel Oluwatobiloba avatar Haoyang Ma avatar  avatar GorrilaObserver avatar Noah avatar CryptoMADMAN avatar zgos avatar Kian Shahi avatar Cheng JIANG avatar tolu avatar Zodomo avatar Lubomir Anastasov avatar A5 Pickle avatar Adyan avatar Omeguhh avatar  avatar 0xpeetza avatar Jake Sung avatar  avatar BoxChen avatar jeissoni avatar Beirao avatar  avatar Salman Dabbakuti  avatar Dhruv Agarwal avatar Tank avatar Seenu avatar york avatar zetsukhun avatar James Morgan avatar gas_limit avatar Мизила Фирокс avatar  avatar  avatar Anton Sakhniuk avatar Emmanuel Ikwuoma avatar Guijin Ding avatar Suthan Somadeva avatar Sabnock avatar  avatar Emmanuel Aboderin avatar  avatar C H A L K avatar Avengers Assemble avatar c3phas avatar Dave avatar jrccrj avatar  avatar Apoorv Lathey avatar  avatar fakemonk avatar Raphael Nembhard avatar ABIMS avatar yeahokyok avatar Aero avatar JohnIdyu avatar Giorgio Dalla avatar 0x01 avatar  avatar  avatar dwong avatar paddy avatar hobbescodes avatar Stefan Latinović avatar Chirag Agrawal avatar Alexander Biryukov avatar  avatar Harshad Dewangan avatar V.O.T avatar MHXW avatar techer.eth avatar  avatar Nick Doherty avatar  avatar naz avatar  avatar felix otieno owade avatar Jerome (@0xSCSamurai) avatar 0xHarbs avatar Raiyan Mukhtar avatar  avatar firatstory.eth avatar Saksham Thapa avatar Devdatt N avatar Alberto avatar  avatar  avatar  avatar Lance avatar Tola David avatar Dmytro Andriievych avatar MrDeadCe11 avatar Guillaume avatar Mirko Pezo avatar forfun avatar qdqd avatar

Watchers

Anthony Albertorio avatar  avatar  avatar  avatar  avatar Raphael Nembhard avatar  avatar

yul_by_example's Issues

Calldata encoding.

Create a Yul code to show how data is encoded in calldata for int and uint types, arrays, addresses, bytes, strings, fixed byte arrays and structs.

Unchecked math.

How deadly is an unchecked math? Since every math in the assembly{} block is unchecked, what could go wrong?
See here.

    uint256 a = type(uint256).max;
    uint256 b = type(uint256).max / 2;

    assembly {
        let sum := add(a, b)
    }

    // What could go wrong 👀?

improvements

1.you don't need to check for underflow and overflow, since it's version 0.8.17
2.add natspec documentation
3.caching value in some part of code will reduce gas instead of calling the opcode twice we can simply store the value in the stack and fetch it back for later use, e.g ` // Function to increment count by 1
function inc() public {
assembly {
// Assign count variable slot to countSlot.
let countSlot := count.slot

        // We update the value at the `count` slot by incrementing the
        // current value at the slot by 1, then calling an sstore() to overwrite the
        // value at slot 0.
        // However, this doesn't check for overflows and underflows, so we must make sure that
        // we do not go beyond the limit for our type maximum before making the update.
        // We are working with uint256, so the max is 0x("ff" * 32).
        if eq(sload(countSlot), 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) { revert(0, 0) }
        sstore(countSlot, add(sload(countSlot), 1))
    }
}

here we can simply reduce the gas value by doing something like this function inc() public {
assembly {
// Assign count variable slot to countSlot.
let countSlot := count.slot

        // We update the value at the `count` slot by incrementing the
        // current value at the slot by 1, then calling an sstore() to overwrite the
        // value at slot 0.
        // However, this doesn't check for overflows and underflows, so we must make sure that
        // we do not go beyond the limit for our type maximum before making the update.
        // We are working with uint256, so the max is 0x("ff" * 32).
        let cachedValue := sload(countSlot)
        if eq(cachedValue, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) { revert(0, 0) }
        sstore(countSlot, add(cachedValue, 1))
    }
}

`
4. in the loop contract we don't need to initialize the value to zero they are already zero by default

Regarding Comments in Structs.sol

Hey @0xfps,

while going through Structs.sol, the file looked like this:

Screenshot (68)

That part is not commented and gonna be hard for new users to understand like what's going on here..What If I add comments in here?

Screenshot (69)

Thanks!

call

i might be wrong but i think the call part using assembly might be wrong

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.