Questions tagged [assembly]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
56 views

Manipulate bytes with assembly

Using assembly only, how do I switch, for A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48, the last 20 bytes of the second 32-bytes from the bytes variable below, and keep the rest as it is? Original bytes: ...
dNyrM's user avatar
  • 902
1 vote
1 answer
39 views

Inline assembly, how to get string from struct in mapping with Yul?

I don't get how to get next element in struct. I can take id value, but what to do next? i assume, i need to add 1 to slot, to get string value but what it return, reference to the string? and what i ...
Gleb Protasov's user avatar
1 vote
1 answer
33 views

generate url in smart contract, based on block.chainid and address(this)

For the past few days I have been looking into creating a simple function to generate a url based on block.chainid (uint256) and address(this). The idea is as follows: function url() public view ...
Cloudronaut's user avatar
3 votes
2 answers
100 views

Why does mload(0x40) result in 0x20?

I ran the code below and had a question. function mloadTest1() public pure returns(bytes memory) { bytes memory a; assembly { a :=mload(0x40) } return a; } mload(x) is a Yul ...
imelon2's user avatar
  • 498
4 votes
3 answers
130 views

Calling private functions using Yul ( Solidity )

I'm currently learning Yul in Solidity and experimenting with the call() function to call other functions in inline assembly. I've noticed that as soon as a function is set to private, I receive a ...
Pintus20's user avatar
1 vote
0 answers
13 views

Solidity assembly: `target_slot`. Is it an opcode or other stuff?

I found this strange variable or opcode in inline assembly, but I'm not sure: target_ slot. I couldn't find its definition in the contract. I prefer it to be an opcode, but I didn't seem to find it ...
LEVI_104's user avatar
0 votes
1 answer
31 views

What is the reason for this assembly code?

I am looking at the following contract, specifically the following assembly code....I think I decoded that it means make sure that if the contents of slot 0 equals 2, then store 3980109478 (which is ...
user127148's user avatar
1 vote
1 answer
17 views

Inline assembly loop doesn't update memory

I want to copy in inline assembly from calldata the last two bytes of every 32 byte slices to the lower half of the first free memory slot(32 bytes). free = free memory pointer + 14 (bytes) moffset = ...
Igor József Vass's user avatar
0 votes
2 answers
107 views

reading and writing to a packed nested struct using inline assembly

I've been racking my brain with this for some time, and have been unable to increment and decrement specific fields inside nested structs as per the example below. // SPDX-License-Identifier: ...
jlbl's user avatar
  • 1
2 votes
1 answer
159 views

Grab first 20 bytes of 32-bytes input

In solidity casting or assembly, how do I grab the first 20 bytes of this 32-bytes input: 0x00010000c900c90062fceaad00000000000000000002af416812f9bb6f2fdff5? Thanks!
dNyrM's user avatar
  • 902
1 vote
1 answer
62 views

Is there a shortcut for "greater than or equals to" in Solidity assembly?

I have some code where I was experimenting with the Math library, namely the logarithm functions, and I figured these would be the perfect functions to optimize with assembly, after slightly improving ...
Ashraile's user avatar
0 votes
0 answers
15 views

How to have solidity output as minimal assembler-close bytecode as possible?

While as indicated in this answer it is probably impossible to have Solidity optimize away the free memory pointer and it's initialization causing the bytecode to start with 0x6080, I was wondering if ...
Zommuter's user avatar
  • 113
0 votes
1 answer
37 views

Is it possible to make my CREATE2 factory so that the calculated address is totally independent of the address my factory is deployed on?

Is it possible to make my CREATE2 factory so that the calculated address is totally independent of the address my factory is deployed on. For example, this is the formula listed on solidity-by-example ...
Priyank Gupta's user avatar
0 votes
1 answer
47 views

Converting an optimized for addition loop into inline assembly (0.8.20)

I have to loop through an array that will contain <10 addresses. I've already optimized for gas costs as much as possible using native Solidity. But I realized this loop can be improved further ...
Ashraile's user avatar
1 vote
1 answer
45 views

how come loading the funcSig from data isn't enough, you have to shift it also to use it in the rest of the code/function

basically we have this function function FlaggedMethods( bytes memory data ) public returns (address) { bytes4 funcSig; assembly { // Shift right by 224 bits ...
owl's user avatar
  • 13
0 votes
2 answers
273 views

Make Uniswap v2 swap through inline assembly

I'm trying to make swap tokens in smart contract using Uniswap V2. My main goal is to make part where you need to work with Uniswap router as assembly block. I'm not good in assembly, so I need your ...
Fuga14's user avatar
  • 23
1 vote
1 answer
50 views

Cannot getting a storage variable from another contract using inline-assembly

I am trying to develop myself on inline-assembly. There are two contracts: CryptoDevs and Whitelist. In the assembly, i copied the whole code of whitelist contract instance into cryptodevs memory. ...
Recep Çankaya's user avatar
2 votes
0 answers
11 views

How can i implement `MinimalBeaconProxy`?

I'm trying to make MinimalBeaconProxy. But seems like the assembly codes don't work. Can I get some advice of this codes? Contracts contract BeaconProxyCreator { constructor(address beacon) ...
Dongwu Kim's user avatar
0 votes
0 answers
16 views

how this function do in the stack, calldata, and memory

I am not familiar with the inline assembly. May I ask what happened in the arrLength method and how it operates in stack, memory, and calldata pragma solidity 0.4.5; contract Test { // for ...
LEVI_104's user avatar
0 votes
1 answer
130 views

Inline Assembly: RETURN opcode can't not work. Failed to decode output

I know that: return(p, s): end execution, return data mem[p…(p+s)). I want to return something in memory but not a multiple of bytes32. For example, I want to return 1 byte: pragma solidity 0.8.20; ...
LEVI_104's user avatar
2 votes
1 answer
268 views

Trying to use CREATE opcode to deploy a new contract instance using bytecode of a deployed contract

I am trying to get the bytecode from a deployed contract, then by using that bytecode, use the CREATE opcode to deploy a new instance of that contract. Here is the code I'm trying out (but to no avail)...
Allan's user avatar
  • 41
1 vote
1 answer
121 views

How to shift elements in array using inline assembly?

Taking a memory array [1,2,3], is it possible to us shr/shl to make the array [2,3,1]?
Ryan Sea's user avatar
  • 618
3 votes
2 answers
132 views

Remix Assembly vs. Solidity Compiler Assembly

I am just diving into EVM assembly and am a little confused about the following. I have the following simple contract: pragma solidity >=0.8.1; contract MyTest {} When I compile it using Remix, I ...
Sebastian Dine's user avatar
3 votes
3 answers
319 views

How to decode a nested encodePacked?

The structure of the encoding is abi.encodePacked( abi.encodePacked( address, uint256, uint256, address, uint256, address, ...
Ryan Sea's user avatar
  • 618
1 vote
0 answers
63 views

I have low level method that calculates log2, but I don't exactly understand how it works

Somebody, please, can you explain step by step how it assembly code exactly works? function log2(uint x) private pure returns(uint y) { assembly { let arg := x x := sub(x,1) ...
Peter Ivanoff's user avatar
1 vote
1 answer
638 views

Is it possible to reverse the gas used in case of using revert?

I try to refund gas to the minimum even after using revert with the following example. uint A = IERC20(base).balanceOf(address(this)); IERC20(token).transfer(owner,1000000); then I run swaps ...
jhonatanalfred's user avatar
0 votes
1 answer
28 views

I m trying to add additional parameter in fulfillBasicOrder but it gives me error

When I add additional parameter in fulfillBasicOrder it gives me error of InvalidBasicOrderParameterEncoding Please help me out how can I add additional parameter in this function as I have less ...
Umeraqeel's user avatar
1 vote
1 answer
79 views

Why doesn't the Yul instruction `div` work just like the high-level division operator?

I noticed that the following Solidity functions are not equivalent: function a(int256 x) pure returns (uint256 result) { assembly { result := div(sub(0, x), x) } } function b(int256 ...
Paul Razvan Berg's user avatar
3 votes
2 answers
1k views

What is calldata offset for dynamic calldata arrays?

The Solidity docs say this about dynamic calldata arrays: For dynamic calldata arrays, you can access their calldata offset (in bytes) and length (number of elements) using x.offset and x.length But ...
Paul Razvan Berg's user avatar
2 votes
0 answers
74 views

Is there any way to bubble up custom errors without using low-level assembly?

Is there any way to bubble up custom errors without using low-level assembly? Say you have a custom error defined like this: contract For { error Unauthorized(); function reverWithError() ...
Paul Razvan Berg's user avatar
1 vote
2 answers
323 views

Is it safe to open SLOAD to any contract?

I am running into issue of contract size over the limit when deploying my contract. So, I have decided to open SLOAD in my getter so that any contract can read from it. function readBySlot(uint256 ...
Mr.SsS's user avatar
  • 415
0 votes
2 answers
345 views

Convert bytes20 to address in assembly

Can you convert a bytes20 to address in assembly (not solidity-typecasting)? If so, how?
dNyrM's user avatar
  • 902
1 vote
0 answers
80 views

How to values in mapping type struct with solidity assembly?

I have a special needs, store the mapping type struct with assembly code. thank you very for everyone who can response this question. Need your help how to write something in assembly Here is how I am ...
Umer Aqeel's user avatar
1 vote
1 answer
228 views

Grab last 12 bytes of a bytes32

How do I grab the last 12 bytes of a bytes32 in assembly? For example, the last 12 bytes of bytes32 num = 0xdeb02111e4e5fa8c7c05e983a3446893a36d12ad6d79206163636f756e740000 Thanks!
dNyrM's user avatar
  • 902
0 votes
1 answer
41 views

how to return tightly packed storage values in assembly

I have a contract with 2 tightly packed values in storage slot1, how do I return the second address value with assembly? Contract MyContract{ bool a; address b; function getAddress() public ...
Panda Power's user avatar
1 vote
1 answer
62 views

Grab the last 32 bytes of 64-bytes input

In assembly, how do I grab the last 32 bytes in a 64-bytes input? I know that this code is for grabbing the first 32: bytes32 half; assembly { half := mload(add(accData2, 32)) }
dNyrM's user avatar
  • 902
0 votes
1 answer
129 views

How to understand the difference between transfer and function call using assembly call(g,a,v,in,insize,out,outsize)

I understand that if there is an external function call, it will use all 7 parameters of the call(g,a,v,in,insize,out,outsize). However, I got a question I know low-level transfers like .transfer() ...
Yu.Pan's user avatar
  • 15
3 votes
2 answers
1k views

Yul/Inline Assembly: Revert with a custom error message

I am learning Yul for my Bachelor's thesis and I am currently stuck on understanding a small code segment with require and revert functions. A simple require function in Solidity ... require(...
EyeEmmaJedi's user avatar
1 vote
2 answers
247 views

How to get internal calls Calldata in solidity?

How to get signature of setA() when user is calling setB() for below code snippet ? contract Sig { function setA() public { // How to get function signature of `setA()` at this location while ...
Namit Jain's user avatar
0 votes
1 answer
386 views

How could you replicate foundry's vm.load cheatcode with solidity code?

I assume it would need assembly and potentially a delegatecall function, but I have been stuck trying to get anything that could work. The idea is that we would input the contract address and the ...
Yan Digilov's user avatar
0 votes
2 answers
173 views

Appending address array through assembly

I have an assembly part which creates an address which I want to append to an array in an address => address[] mapping. The code works if I use push() but I am trying to reduce gas usage and have ...
gsh192's user avatar
  • 1
2 votes
1 answer
92 views

Does the free memory pointer need padding?

This code is taken from the solidity documentation. library GetCode { function at(address addr) public view returns (bytes memory code) { assembly { // retrieve the size of the ...
yaxnaw's user avatar
  • 23
1 vote
2 answers
162 views

Why does storing values at non-32 multiple memory locations spill over to next location?

Take this line for example (Yul): mstore(0x24, 0x0443455243) If it was mstore(0x20, 0x0443455243), then the entire value would have been stored at location 0x20. However, if 0x24 is used, I'd expect ...
Sohail Saha's user avatar
0 votes
1 answer
54 views

Most gas efficient way to convert uint256 into a handful of uint8s

I am working on a card game. I have a problem where I am choosing to store a representation of 32 uint8s inside one uint256 word in order to save on gas. I store the uint256 word and then want to ...
VoR0220's user avatar
  • 1,710
1 vote
0 answers
49 views

Why evm assembly push4 sometimes doesn't push the value in parameter?

I'm analyzing the fail of this tx on goerli My question is not really about the tx, but about a strange behaviour when I debug it on remix. I'm calling a proxy who called the implementation with ...
Fabien's user avatar
  • 41
1 vote
1 answer
318 views

Is there a way of getting the stateRoot of the last block in a contract?

So i was curious if you could write a contract, that retrieves the StateRoot from the last mined block. I found a Repo from Keydonix, who used the stateRoot from previous blocks to calculate a more ...
Mango's user avatar
  • 13
1 vote
1 answer
278 views

How do I manipulate the elements of a memory array in assembly?

Simple example: Let's say I have a function that takes a fixed size array, finds the largest number in it, and returns another fixed size array in memory with the largest number as the last index: ...
EKN's user avatar
  • 31
0 votes
1 answer
74 views

Free memory point manipulation in solmate library

I am trying to learn a little bit of yul and came across a scenario in the solmate library I don't quite understand. In all of the instances when they are appending addresses to the free memory ...
FuryokuSyndicate0x1's user avatar
0 votes
1 answer
53 views

get uint value inside a bytes32 slice

I have the following code: function test_bytes(bytes32 test) external pure returns(bytes3 idx,uint256 idx2) { //test = ...
Jhonatan's user avatar
0 votes
1 answer
23 views

Problem with expensive function

I have next code : struct Data { uint id; string name; string Surname string moreInfo; } mapping(address => Data[]) private ownerauto; address[] private owners string[] ...
Kilo Arms's user avatar

1
2 3 4 5 6