Questions tagged [assembly]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
0answers
2 views

Is it possible to compile Yul with truffle?

I have a contract written in Yul that I want to compile. It is not clear to me if this is possible with truffle. Alternatively I tried compiling it in remix and then migrating the artifact file, but ...
2
votes
1answer
57 views

Disassemble "delete array" in solidity

am trying to learn about EVM bytes code, but faced this problem. First, this is the solidity code pragma solidity ^0.8.0; contract Assembly { function f() public { uint8[3] memory a = [1, ...
0
votes
1answer
21 views

How to read the current program counter (PC) in Solidity or Yul?

I was reading about the EVM dialect in the docs, and saw this: Instruction Explanation pc() Current position in code I went to Remix to try it out: function read_pc() external view returns (uint256 ...
1
vote
2answers
32 views

How can one access values returned via "return(0, returndatasize())"?

I was looking at the _delegate function in OpenZeppelin's Proxy.sol: /** * @dev Delegates the current call to `implementation`. * * This function does not return to its internall call site, it will ...
1
vote
1answer
18 views

How to read high-level storage variable from inline assembly?

This code does not compile: pragma solidity >=0.8.0; contract MyContract { uint256 public foo = 314; function myFunction() external view returns (uint256 result) { assembly { ...
1
vote
1answer
28 views

Understanding why this blob of assembly code adds 0x20 and 0x1f [duplicate]

The execute function defined in MakerDAO's DSProxy contains this blob of assembly code: assembly { let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0) ...
1
vote
1answer
56 views

Storing immutable state in contract data

This post highlights how to use CREATE2 to implement smart contract state using contract data. However, the author also says: "if storage doesn’t need to change, you can do away with metamorphic ...
0
votes
0answers
13 views

transfer from one contract to another

// ContractA function nice(address to) public payable { require(_to.send(_value), ERROR_SEND_REVERTED); } I also have a contractB which was deployed with EIP-1167. Now, The problem is that when ...
0
votes
1answer
25 views

Assigning variables via assembler

I have a task to split a time string in UTC format (e.g. 2021-06-16T00:29:01Z) into a year, month and day. Since the UTC format assumes a constant string length, I wanted to assign values to variables ...
1
vote
1answer
33 views

How to make inline assembly keccak256 work like Solidity keccak256

Is there a way to use inline assembly keccak256(p, n) work like the Solidity keccak256(p)? I imagine there would be a neutral input I could give for either p or n to make that happen?
0
votes
1answer
31 views

Storage and memory layout of strings

How exactly are strings stored in storage? And, how does that differ from the way they're stored in memory when loaded from storage into a memory variable?
0
votes
0answers
9 views

Overriding memory with mstore()

Is it safe to override memory using mstore() ? In my case, I have an external function receiving bytes calldata data as argument, and I'm doing multiple calls in that same function. To do this, I ...
2
votes
1answer
31 views

How to enable the inline assembly IntelliSense for Solidity on VSCode

I have installed the solidity v0.0.125 extension from VSCode marketplace, but it seems doesn't support the inline assembly snippets by default. How to enable the feature or I need install more ...
1
vote
0answers
44 views

Multiple low level function calls using inline assembly in a smart contract not working

I'm trying to low-level call two separate contracts (B and C, with the same function signature) using inline assembly from my own contract (A) within the same function. The call to B modifies some ...
2
votes
1answer
66 views

Solidity Assembly basic callData return 4 bytes

I write a small test script to see how callData works in remix. The script is listed below: contract Assem { uint256 public count; function callData() public pure returns(bytes4) { ...
0
votes
0answers
12 views

Converting a for loop with arrays into assembly

here is a simple function for generating a sequence of numbers. I am wondering if anyone could help revise it to a version using Assembly for efficiency and perhaps speed. The relevant materials on-...
1
vote
1answer
25 views

Why 1225312 is the last addressable location in memory?

I was dabbling with inline assembly, and I wanted to discover the extent of addressable memory. After many trials, I found out that the only usable memory locations that could be accessed with OPCODEs ...
0
votes
0answers
19 views

Solidity: Converting a hex-string into bytes efficiently

I need to convert a string variable (passed to a function), containing a hex-string, into bytes in Solidity. In a way, this is the same as the following, just that the string variable will be passed ...
2
votes
0answers
34 views

Smart contract function signature only 1 byte

I have noticed some contract calls only contain the function signature in the 1st byte of the input data. For example: ...
0
votes
1answer
79 views

abi.encode and abi.decode arguments, including variable-sized arrays

I'm passing some arguments to a function and abi.encode()-ing them. Then I pass this data to another function where I abi.decode() it, but Im not entirely sure how to do it: Arguments I'm encoding: ...
1
vote
1answer
108 views

Understanding Solidity Inline Assembly

I am new to inline assembly. I have tried understanding different opcodes, and understood quite a bit, but still not be able to correlate them in context to STATICCALL opcode. Kindly help me ...
1
vote
1answer
82 views

What is the benefit of using create2() to create a smart contract?

Why would someone use create2() inline assembly code to create a child contract in factory pattern as opposed to using newContract() to create an instance of "newContract"? What are the ...
2
votes
0answers
22 views

Struct getter and Setter using inline assembly in solidity

I have a storage slot defined bytes32 internal constant USER_STRUCT_SLOT = 0xd722976edd762e336e189ef10d2d356b8698dc8a2e03a97b539ab9d3974a1630; struct UserData { uint256 level; ...
1
vote
1answer
44 views

Compile Solidity to assembly keeping a correspondence with the source code

I would like to compile a contract written in Solidity (or even just the part relating to a single method) to study it and understand if I can optimize it. I already managed to get the bytecode as ...
1
vote
1answer
83 views

What does add(source, 32) do in solidity?

I understand what add() does. It used used in the assembly to perform additions. My question is why are we adding 32 here? What is the purpose of doing so? The full code is here
1
vote
0answers
20 views

Inline assembly function parameter has same name with local variable

contract Test { function f() public returns(uint r) { uint x = 100; assembly { function f(x) -> y { y := add(x, 1) } r := f(100) } } } Use ...
2
votes
0answers
21 views

Why use assembly in this operation?

I'm trying to understand why assembly for this sum is required? // 1 _structHash = keccak256( abi.encode( _blockhash, totalAddresses, ...
2
votes
0answers
10 views

Is it possible to call a higher-level function from within assembly?

Take the following code: contract Foo { function increment(uint256 x) external returns (uint256 result) { result = x + 1; } function increment_twice(uint256 x) external ...
1
vote
0answers
20 views

What is the neatest way to get hold of max uint256 in assembly?

Since Solidity v0.6.8, the neatest way to get hold of max uint256 in Solidity is this: uint256 foo = type(uint256).max; Is there something like that for Yul? I couldn't find anything in the docs. I ...
0
votes
1answer
39 views

Do the Solidity v0.8 overflow checks apply to assembly operations too?

Since Solidity v0.8, the compiler has checks for {over,under}flow by default for all arithmetic operations. Do these checks apply to assembly (yul) arithmetic operations too?
1
vote
2answers
55 views

How to assign to negative numbers in yul?

Take this code snippet: function powOfTen(uint256 x) internal pure returns (int256 result) { assembly { switch x case 1 { result := -18 } } } Which doesn't compile: ...
2
votes
0answers
31 views

How to forward returned bytes from calls executed with assembly?

I would like to make an external call with assembly, but am having trouble retrieving the return data as "bytes". The code below currently returns only 32 bytes, which just shows the length ...
1
vote
0answers
154 views

test return value of transaction

contractA function test() internal { stateVariable = 20; uint num = 20; assembly { mstore(0x00, num) return(0x00, 0x20) } } How do I test the return ? I am using hardhat , but i don'...
3
votes
1answer
105 views

Can abi.encodeWithSignature be optimized by pre-compute?

I have a proxy implementation with method exchange and delegator with method exchange2 with the following code: function exchange2(int128 i, int128 j, uint256 dx, uint256 min_dy) public { (...
1
vote
1answer
580 views

What programming languages was Uniswap's open-source code written in?

Uniswap is a decentralized exchange that is actually a decentralized app where people can trade ERC-20 tokens without a middleman. Uniswap's code is open-source: https://github.com/Uniswap Was the ...
0
votes
0answers
20 views

how to access the element of struct in array and mapping using assembly?

how to access the element of struct in array and mapping using assembly? my code case: contract Test { struct User { address addr; uint256 tokenAmount; } mapping (uint256 =...
0
votes
0answers
124 views

staticcall - what does this code do?

I'm lost, does anyone understand this code? assembly { success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60) // Use "invalid" to make gas ...
1
vote
1answer
754 views

How much C++ is needed to write smart contracts?

Solidity seems to be written in C++ so does that mean that smart contracts require some level of C++ programming skills to get by? How much background knowledge is required for writing a simple one or ...
1
vote
1answer
457 views

Instantiating a smart contract in Uniswap using assembly and create2

I am studying Uniswap source code and there is something I do not understand in this file: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2Factory.sol: This factory can ...
0
votes
1answer
210 views

Why chainid is done in assembly in UniswapV2ERC20

I am studying uniswap solidity source code and there is something i do not understand in UniswapV2ERC20 constructor: uint chainId; assembly { chainId := chainid } (pasted from https://github.com/...
1
vote
0answers
34 views

How do I get solcjs to compile yul

I want to write a smart contract using pure Yul but I have no idea how I'm meant to configure / run solcjs to compile my yul code down to bytecode. Any pointers would be much appreciated.
0
votes
1answer
215 views

Encode data into a single bytes variable using assembly

I'm currently testing a few things using the Solidity assembly. Basically, what I want to do is Pack several variables of different type together into one bytes array i.e. similar to what abi....
0
votes
0answers
68 views

How to pass a non-deployed Smart Contract to a deployed contract and call a function on it?

I'm currently trying to pass a Smart Contract (which is NOT deployed) in byte code format as an argument to another Smart Contract (which is deployed). From within the deployed contract I want to run ...
0
votes
1answer
82 views

Is it possible to access struct field from in-line assembly (Yul) when using ABIEncoderV2?

Is it possible to access struct field from Yul? Ideally it could look like this: pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; contract Sample { struct S { uint256 n; ...
0
votes
2answers
336 views

Parameters for delegatecall in inline assembly

I am trying to perform a delegate call from inline-assembly and I am unable to wrap my head around how to access the memory variables assigned outside the assembly block. function() external payable { ...
0
votes
0answers
15 views

How to deploy a compiled stateless contract and which doesn t have creation code/transaction?

I got a deployed contract bytecode from an internal project which I d like to publish on the mainnet. The problem is it s just the bytecode without the creation code. Is there a simpler way to deploy ...
5
votes
1answer
136 views

Copy and deploy a smart contract from opcodes without source code

I extracted opcodes from an already existing and deployed smart contract and I would like to know if it is possible to deploy an exact copy of this contract from its opcodes? My extracted opcodes ...
0
votes
0answers
47 views

Unable to reference local variable in-line assembly

I am unable to compile a contract that references a local variable in in-line assembly. Here is the code: pragma solidity ^0.5.0; /** * @dev Contract module that helps prevent reentrant calls to a ...
2
votes
1answer
93 views

Most gas efficient way to check if byte array contains zeroes?

I have a contract that I pass data packed into an array of uint256 (as it's apparently cheaper to use uint256 over bytes32) and each byte in the uint256 contains positions referencing another array of ...
0
votes
1answer
672 views

how do i pass by reference?

I have a couple of questions. First: So, In a function, i created memory array. I want to pass this memory by reference to another function.. function test2() public returns(uint a) { uint[2] ...