Questions tagged [assembly]
The assembly tag has no usage guidance.
300
questions
0
votes
0
answers
8
views
With assembly call, is it safe to overwrite memory that were used for inputs with outputs?
Is it safe to reuse memory for both inputs and ouputs when working with assembly call?
If not, what can go wrong?
Contract below uses assembly to call Target
contract YulStaticCall {
function ...
0
votes
1
answer
34
views
Why the method _castToPure in hardhat/console.sol can convert a view method to a pure one?
I have a question when read the console.sol. There is a method named _castToPure that can convert a view method _sendLogPayloadImplementation into a pure method. I cant understand the reason and ...
0
votes
1
answer
39
views
Runtime bytecode not contained in creation bytecode
I'm currently delving into EVM and contract bytecode.
My understanding is that by sending a transaction to the NULL address with both the initcode and contract code, and ensuring that the init code ...
0
votes
0
answers
9
views
Solidity inline assembly custom error revert foundry testing
I have written test cases for the original high level code to test the reverting case of a custome error - ERC20InsufficientBalance(address,uint256,uint256) with the following code:
function ...
1
vote
1
answer
61
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:
...
1
vote
1
answer
52
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 ...
1
vote
1
answer
45
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 ...
3
votes
2
answers
116
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 ...
4
votes
3
answers
156
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 ...
1
vote
0
answers
16
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 ...
0
votes
1
answer
45
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 ...
1
vote
1
answer
24
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 = ...
0
votes
2
answers
133
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: ...
2
votes
1
answer
171
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!
2
votes
1
answer
92
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 ...
0
votes
0
answers
16
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 ...
0
votes
1
answer
48
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 ...
0
votes
1
answer
57
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 ...
1
vote
1
answer
47
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 ...
0
votes
2
answers
356
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 ...
1
vote
1
answer
54
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. ...
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) ...
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 ...
0
votes
1
answer
156
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;
...
2
votes
1
answer
309
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)...
1
vote
1
answer
134
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]?
3
votes
2
answers
148
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 ...
3
votes
3
answers
347
views
How to decode a nested encodePacked?
The structure of the encoding is
abi.encodePacked(
abi.encodePacked(
address,
uint256,
uint256,
address,
uint256,
address,
...
1
vote
0
answers
65
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)
...
1
vote
1
answer
667
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 ...
0
votes
1
answer
31
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 ...
1
vote
1
answer
94
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 ...
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 ...
2
votes
0
answers
76
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() ...
1
vote
2
answers
365
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 ...
0
votes
2
answers
381
views
Convert bytes20 to address in assembly
Can you convert a bytes20 to address in assembly (not solidity-typecasting)?
If so, how?
1
vote
0
answers
84
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 ...
1
vote
1
answer
241
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!
0
votes
1
answer
46
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 ...
1
vote
1
answer
71
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))
}
0
votes
1
answer
140
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() ...
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(...
1
vote
2
answers
256
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 ...
0
votes
1
answer
407
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 ...
0
votes
2
answers
193
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 ...
2
votes
1
answer
93
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 ...
1
vote
2
answers
180
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 ...
0
votes
1
answer
59
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 ...
1
vote
0
answers
50
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 ...
1
vote
1
answer
358
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 ...