Questions tagged [assembly]
The assembly tag has no usage guidance.
238
questions
0
votes
0
answers
10
views
Assembly loop transferring ERC20 Token
I have a dynamic array, would like to use assembly to loop through the addresses. The amount of tokens to send is dependent on the struct, where it looks up a user's address. Understandably, the best ...
0
votes
0
answers
8
views
Reading return data from a Solidity `call`
Given a call to an external contract,
(bool success, bytes memory returnData) = addr.call(abiEncoding);
how do I read a single value of a given type from the return data, without using assembly?
The ...
0
votes
0
answers
21
views
it's possible to define global assembly function in solidity
We can define a function like this
function A() external pure {
assembly {
function doSomthing() -> data { // We define this method in the A function
}...
0
votes
1
answer
68
views
How to write struct with solidity assembly ( understand assembly )
In the first example, I tried to change the second slot of the struct but failed
mapping(uint256 => MyStruct) public myStruct;
struct MyStruct {
uint128 slot1;
uint128 slot2; //...
0
votes
1
answer
37
views
Return bytes from inline assembly
I have two little tests in which I'm trying to return bytes that are populated via inline assembly. It works when I'm using a pre-defined return variable, but I'm getting a buffer overrun when I try ...
0
votes
0
answers
31
views
Passing array to external function in inline assembly
How can I pass a (nested) array to an external function if the call is made within an inline assembly block? I wrote the following little test contract and everything works just fine until I add the ...
0
votes
1
answer
21
views
What exactly is each parameter for assembly delegatecall?
I've been searching everywhere but I'm still not clear what each of the parameters are for delegatecall. What I have found is this delegatecall(g, a, in, insize, out, outsize), where g is gas and a is ...
0
votes
1
answer
20
views
What does dataoffset do?
in the doc it says:
... Can only take string literals (the names of other objects) as arguments and return the size and offset in the data area
but I'm still confused, could anybody give me an example ...
2
votes
1
answer
59
views
how to get the data after mstore in yul?
function here:
function encodeAmount(
uint256 amount,
bytes memory data
) public pure returns(bytes memory){
bytes memory amountToEncode = abi.encode(amount);
assembly {
mstore(...
1
vote
1
answer
28
views
Access contract's runtime code from one of its functions
Is there anyway to access the runtime code of a function from its contract? Using assembly may be?
Thanks
0
votes
1
answer
50
views
Difference between mstore(0x40) and mstore(mload(ox40))
I do not understand the difference between mstore(0x40) and mstore(mload(ox40)). I know that mstore(mload(ox40), something) stores something in the first free 32bytes of the memory. What does mstore(...
1
vote
1
answer
35
views
Assembly vs solidity delegatecall
Is there a real difference nowadays, performance-wise, between using assembly delegatecall and the high-level solidity version?
I know that in the past assembly was used so you could get the returned ...
0
votes
1
answer
59
views
Deploy contract with Bytecode from smart contract
I have a contract that has a function deployContract(bytes memory runtimeBytecode) I want this function to be able to deploy the bytes sent to it as a new smart contract.
I understand that I will have ...
0
votes
2
answers
45
views
Right Shift not working in inline assembly
I working on in-line assembly in the smart contract.
function rightShift(uint256 x) public pure returns (uint256 result) {
assembly {
result := shr(x,1)
}
}
Above function ...
0
votes
1
answer
32
views
Write to storage with assembly inline
I have this contract where I use inline assembly to add two numbers.
pragma solidity ^0.8.0;
contract Test {
function add(uint a, uint b) external {
assembly {
let sum := add(...
0
votes
1
answer
109
views
Opensea source code
for (i = 0; i < words; i++) {
/* Conceptually: array[i] = (!mask[i] && array[i]) || (mask[i] && desired[i]), bitwise in word chunks. */
assembly {
let commonIndex := ...
0
votes
1
answer
105
views
Calling functions using inline assembly (YUL)
I've been trying to figure out how to make function calls using in-line assembly (yul)
For example, I have a function _getCurrentRate() where I would like to be capable of calling from several ...
1
vote
1
answer
213
views
How to store and retrieve string which is more than 32 bytes(or could be less than 32 bytes as well) at a desired hashed slot in solidity?
I have a desired storage slot:
bytes32 public slot = keccak256(abi.encodePacked("mydesiredStorageslot"));
I wish to store on this slot an url which, if not shorted, can be more than 32 ...
0
votes
1
answer
110
views
sstore struct within mapping, inline assembly
I'm trying to read from and write to a mapping of structs using inline assembly.
getValues() is an example of how I'm able to read something from storage. I can read the first two values using and() ...
0
votes
1
answer
62
views
Understanding string utils library
function toSlice(string memory self) internal pure returns (slice memory) {
uint ptr;
assembly {
ptr := add(self, 0x20)
}
return slice(bytes(self).length, ...
1
vote
1
answer
75
views
sload. with struct - loading bytes
I'm trying to load varios bytes from a struct in assembly. I found a good explanation here for uint. While this is working for uint and the whole memory slot i cant figure out how to get a bytes1 or ...
1
vote
0
answers
51
views
Store variables from Inline Assembly and use them after the block?
Unlike almost all examples of inline assembly usage, I would not like to immediately return the function after doing something in the assembly block.
Rather, I would like to save the result to some ...
0
votes
1
answer
246
views
Set data for call(), delegatecall(), etc, in Yul / inline assembly?
It seems that almost all the examples of external calls are simple proxies, which do something like this:
assembly {
// Copy msg.data. We take full control of memory in this inline ...
0
votes
1
answer
147
views
Accessing state variables using Assembly
I made some experiments with assembly, unfortunately, there is not much documentation and most of them cover the same topics (maths operations, extcodesize, etc.). Here I'm trying to read my state ...
1
vote
1
answer
205
views
How does Bytes-Utils slice function work?
I'm quite confused of how the bytes-utils library's slice function. I have a few questions about how it works, especially about the code below. The full code can be found here.
Why is tempBytes ...
0
votes
0
answers
17
views
Send fixed Event using assembly on Solidity
Is it possible to send an event in hex format? I mean, I want to send the event to the EMV from an Smart Contract without using the function log/event. I want to send a raw chain of bytes directly to ...
0
votes
1
answer
115
views
EXTCODESIZE returning 32 for contract size less than 32
I'm currently completing the Ethernaut challenges, one of them is to deploy a small contract (runtime bytecode is 10 bytes long).
I'm running the tests on my local machine, and using Solidity to ...
5
votes
2
answers
191
views
Detect .call, view, pure execution mode
Is it possible to detect, within a contract, that a function is executing in read-only mode?
Background:
Consider a modular system similar to Diamond pattern that uses a proxy-based forwarding scheme ...
0
votes
0
answers
126
views
How to set msg.sender in assembly
I'm making the following call in contract A:
address payable partyClone = payable(Clones.cloneDeterministic(
partyRegistryImplementation,
salt
));
where ...
0
votes
0
answers
50
views
brownie compile throws Compiler Error: Stack too deep. Although Remix IDE works fine
I'm new to solidity.
I've found this tutorial on YouTube How to store NFT Metadata on-chain. The author provided the complete code. I tested it in Remix IDE and it worked perfectly.
Then, I created a ...
0
votes
1
answer
59
views
Manipulating individual bytes in assembly
I am learning assembly and I am trying to write to individual bytes in a bytes32 array. I have the following code:
contract TestAssembly {}
function test(
uint8 _count
) public {
...
0
votes
1
answer
43
views
Using assembly to store more values per word
I need to store 1250 numbers 16 bits elements in an array.
Storing unit256 cost 20.000 gas units each (SSTORE opcode), which at 200gwei gas price, will cost around 4.5 ETH.
My understanding is that I ...
0
votes
0
answers
37
views
Decode bytes32 with multiple arguments
I found a Chainlink Adapter that fits my goals and it returns several arguments separated by : in bytes32 format.
eg.
(fin:win:2:1651:VP) => ...
0
votes
1
answer
49
views
Does stack persist through external calls?
Looking through the docs I found that a malicious caller can take the stack depth to ~1000 and then make a call which would fail due to stack depth. Based on that let's assume that stack indeed ...
0
votes
1
answer
80
views
Recovering data from Bytes using Assembly
I have im my js file 3 variables:
var stringData = "stingData";
var intNonce = 1234;
var hexData = "0x65225648";
I want to concatenate then and send to my contract:
var data = ...
0
votes
1
answer
441
views
How to bit shift a bytes in solidity / assembly?
I can bit shift uint_n_ easily but I don't manage to bit shift a bytes memory myBytes.
When doing myBytes << i I have a compile error and when using assembly I receive 0.
See my MWE for assembly ...
0
votes
1
answer
194
views
Understanding ERC721._checkOnERC721Received in openzeppelin library
When you use OpenZeppelin's ERC721 implementation, in most cases you call _safeMint function. _safeMint does some obvious basic operations except this one:
require(
_checkOnERC721Received(address(...
0
votes
1
answer
256
views
Is the source code of any Ethereum smart necessarily contract open?
I thought that any smart contract had open code. However, from time to time I'll encounter smart contracts that have no publicly available Solidiy code, on etherscan.
How can this be? Doesn't that ...
1
vote
1
answer
102
views
Is it possible for the Solidity compiler to remove an external call?
I am experiencing a weird situation where it seems that the Solidity compiler (or optimizer) is removing an external call.
I am performing an ERC20 token transfer on forked mainnet using Hardhat. From ...
1
vote
1
answer
74
views
Cannot compile code with inline assembly using solc linux binaries
The code with inline assembly fails to compile with the solc binaries from soliditylang.org. The same code compiles without issue using Remix or solcjs. Because of this issue I cannot even compile the ...
0
votes
0
answers
40
views
how to have base contracts have same variables
I have 2 base contracts.
contract Base1 {
function nice1() public {
}
}
contract Base2 {
function nice2() public {
}
}
contract Derived is Base1, Base2 {
function ...
2
votes
1
answer
123
views
Q: `extcodesize` is non-zero after `selfdestruct`
I ran into an EVM edge case. Is it correct that extcodesize(addr) is not zero, after calling selfdestruct on addr in the same transaction?
This behavior seems strange and I could not find any ...
1
vote
1
answer
185
views
Is there a size limitation on create2 bytecode field?
I have the following method in my Solidity contract.
function deploy(bytes memory bytecode, uint _salt) public payable returns (address){
address addr;
assembly {
addr := create2(
...
4
votes
1
answer
448
views
How does Uniswap v3's logarithm library (TickMath.sol) work?
I want to calculate logarithm of a non-standard base at low gas cost. First thought is to use change of base rule with log2() fixed point methods mentioned in this thread.
However Uniswap v3 core has ...
1
vote
1
answer
300
views
What is the layout of returndata from returndatacopy opcode?
I am attempting to perform operations on returned data from delegatecall opcode using a similar familiar proxy pattern:
assembly {
let ptr := mload(0x40) // load empty memory address pointer
...
1
vote
1
answer
507
views
Solidity shr opcode
What does this code mean?
sender := shr(96, calldataload(sub(calldatasize(), 20)))
Btw sender is an address variable.
1
vote
1
answer
115
views
mstore8 assembly instruction makes call goes out of gas
My solidity code example:
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.2;
library TestLibrary {
function sz_varint(uint256 i) internal pure returns (uint256) {
uint256 ...
0
votes
0
answers
222
views
Solidity inline-assembly: How to persist a value to storage and access from any scope within a function call
I'm writing a contract in inline-assembly. I have a parent function in my contract which interacts with an external contract. This in turn triggers a callback function in my contract. So multiple ...
0
votes
0
answers
12
views
Sending arbitrary bytes to a function for later unpacking
I have a function I want to write that takes the smallest possible input data as an exercise:
function test(bytes memory test_data
) public {
// the bytes memory is 53 bytes, 16 + 16 + 20 +...
2
votes
1
answer
326
views
How to load the first 4 bytes from a bytes calldata var?
It is common for calldata to be encoded as bytes calldata when developing contracts in Solidity.
I need to extract the function selector from such a bytes var. What's the best way to load the first 4 ...