{"id":26469786,"url":"https://github.com/ocdbytes/solidity_yul","last_synced_at":"2025-03-19T17:09:23.977Z","repository":{"id":179239864,"uuid":"660813761","full_name":"ocdbytes/Solidity_Yul","owner":"ocdbytes","description":"Yul is a programming language that can be used to interact with the low level memory, stack in solidity. Yul is very handy during the smart contract audit and yul can be used to implement various types of functions that are hard to implement in solidity.","archived":false,"fork":false,"pushed_at":"2023-07-15T13:26:05.000Z","size":1493,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-14T10:46:09.582Z","etag":null,"topics":["solidity","yul-assembly"],"latest_commit_sha":null,"homepage":"","language":"Solidity","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ocdbytes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-06-30T23:15:56.000Z","updated_at":"2023-07-07T05:29:09.000Z","dependencies_parsed_at":"2023-09-03T04:24:25.463Z","dependency_job_id":"5b96b6f5-b43c-48e7-8aa9-ebcc3495c1e4","html_url":"https://github.com/ocdbytes/Solidity_Yul","commit_stats":null,"previous_names":["arun89-crypto/solidity_yul","ocdbytes/solidity_yul"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FSolidity_Yul","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FSolidity_Yul/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FSolidity_Yul/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ocdbytes%2FSolidity_Yul/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ocdbytes","download_url":"https://codeload.github.com/ocdbytes/Solidity_Yul/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244470251,"owners_count":20457908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["solidity","yul-assembly"],"created_at":"2025-03-19T17:09:23.368Z","updated_at":"2025-03-19T17:09:23.972Z","avatar_url":"https://github.com/ocdbytes.png","language":"Solidity","readme":"# YUL\n\nYul is a programming language that can be used to interact with the low level memory, stack in solidity. Yul is very handy during the smart contract audit and yul can be used to implement various types of functions that are hard to implement in solidity.\n\n## Memory Structure\n\nMemory layout in solidity\n\n```sh\n(Each word contains 32 bytes)\n0x00 -\u003e 0x1F = [0x00000000000000000000000000000000]\n0x20 -\u003e 0x3F = [0x00000000000000000000000000000000]\n.\n.\n.\n.\nso on......\n```\n\n## Some important pointers\n\n- Memory ranging from `0x00 -\u003e 0x3F` is known as scratch memory.\n\n`Scratch Memory` : It is the memory that is used to store the values during function execution.\n\n```sh\n          ---|\n0x00 -\u003e 0x1F |\n0x20 -\u003e 0x3F |-\u003e Scratch Memory\n          ---|\n```\n\n- Memory ranging from `0x40 -\u003e 0x5F` store the free memory pointer.\n\n`Free Memory Pointer` : It is a pointer that store the address in the memory that is empty and we can store anything on it.\n\n```sh\n          ---|\n0x40 -\u003e 0x5F |-\u003e Free Memory Pointer\n          ---|\n\n0x40 -\u003e 0x5F = [0x00000000000000000000000000000080]\n```\n\nHere in this example it is shown that `0x80` is the starting address of the memory that is available for storing anything.\n\n## How variables are accessed through the memory (memory arch) ?\n\n```ts\ncontract YulIntro {\n    function test() external view {\n        bytes32 wordFreeMemory;\n\n        assembly {\n            wordFreeMemory := mload(0x40)\n        }\n\n        console.logBytes32(wordFreeMemory);\n    }\n}\n\n// OUTPUT : 0x0000000000000000000000000000000000000000000000000000000000000080\n```\n\nIf we execute the `test()` function the state of memory will be as follows :\n\n\u003cimg src=\"./docs/1.png\"\u003e\u003c/img\u003e\n\n## Basic Code\n\n- Here in this test() function it is shown that how to load the value from memory and store it into other variable, and storing the data into free_memory\n\n  - `mload` : Loading the value from the memory address : mload(memory_address)\n  - `mstore` : Storing the value into an address : mstore(memory_address, value)\n\n```ts\ncontract YulIntro {\n    function test() external view {\n        bytes32 wordFreeMemory;\n\n        assembly {\n            let free_memory := mload(0x40) // (1)\n            mstore(free_memory, 32)        // (2)\n            wordFreeMemory := mload(0x40)  // (3)\n        }\n\n        console.logBytes32(wordFreeMemory);\n        // OUTPUT : 0x0000000000000000000000000000000000000000000000000000000000000080\n    }\n}\n```\n\nHere there are three steps happening :\n\n- `free_memory` pointer stores the pointer of the memory that is free.\n- then `32` (0x20) is stores in the `free_memory` pointer in memory.\n- Now the `wordFreeMemory` stores the pointer at memory location `0x40`.\n\nThe most important thing to notice is that the free memory pointer is not updated automatically while using YUL we need to do it manually.\n\n**(1)**\n\u003cimg src=\"./docs/2.png\"\u003e\u003c/img\u003e\n\n**(2)**\n\u003cimg src=\"./docs/3.png\"\u003e\u003c/img\u003e\n\n## How strings are stored in memory ?\n\nOn the memory level the strings are stored as follows :\n\n```ts\ncontract YulString {\n    function test() external pure {\n        string memory _str = \"hello\";\n    }\n}\n```\n\n\u003cimg src=\"./docs/4.png\"\u003e\u003c/img\u003e\n\nIf we take a look at the memory stack\n\n- Here the length of the string is stored in the free memory pointer `0x80`.\n- String is stored at `0xa0`.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focdbytes%2Fsolidity_yul","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Focdbytes%2Fsolidity_yul","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focdbytes%2Fsolidity_yul/lists"}