89

All of the opcodes and their complete descriptions are available in the Ethereum Yellow paper. For convenience, though, I've made a handy reference list of them all: 0s: Stop and Arithmetic Operations 0x00 STOP Halts execution 0x01 ADD Addition operation 0x02 MUL Multiplication operation 0x03 SUB Subtraction ...


19

As others have stated, the x m:=mload(0x40) instruction reads the 32 bytes of memory starting at position 0x40. In solidity, the 0x40 slot in memory is special: it contains the "free memory pointer" which points to the end of the currently allocated memory. When you use inline assembly, you should load the data stored at 0x40 and then only write to ...


16

mload(0xAB) Loads the word (32byte) located at the memory address 0xAB. e.g mload(0x60) loads the word located at 0x60 memory address. let's code to understand more : function f () { assembly { let freemem_pointer := mload(0x40) mstore(add(freemem_pointer,0x00),"36e5236fcd4c61044949678014f0d085") mstore(...


14

EDIT: The function will return false if it is invoked from a contract's constructor (because the contract has not been deployed yet). The code should be used very carefully, if at all, to avoid security hacks such as: https://www.reddit.com/r/ethereum/comments/916xni/how_to_pwn_fomo3d_a_beginners_guide/ (archive) To repeat: Do not use the EXTCODESIZE ...


13

As the author of that module, a few things that came up: Inline assembly allows you to read entire words (256 bits) from data types like string and bytes in a single operation. Solidity-stringutils uses that to do very fast string comparisons by doing subtraction on 32-byte chunks of the two strings being compared. Without assembly, you have to do this byte-...


10

This is the Swarm hash. It is documented at https://solidity.readthedocs.io/en/develop/metadata.html Extract follows Contract Metadata The Solidity compiler automatically generates a JSON file, the contract metadata, that contains information about the current contract. It can be used to query the compiler version, the sources used, the ABI ...


9

All things must have a beginning I think you're getting called from a contract constructor before it's committed to the chain. At this point, it will have a (theoretical) address but no code yet in the chain. It will only get code size after the transaction completes and the block is accepted.


8

Martin Holst Swende published an implementation of a cloner contract here! https://gist.github.com/holiman/069de8d056a531575d2b786df3345665 Excerpt: function clone(address a) returns(address){ /* Assembly of the code that we want to use as init-code in the new contract, along with stack values: # bottom [ STACK ] top ...


8

This code snippet is correct, although it seems important to note that EXTCODESIZE will return 0 when called within the constructor of a contract, since the contract is not created yet. Hence, if msg.sender is a contract, the isContract() modifier could return false if your contract's function is called within the constructor of the msg.sender contract. This ...


8

Within the assembly code, _data is the memory address of the start of the array data. However, the first memory word (32 bytes = 0x20 bytes) is reserved for the length of the array, so we need to step over this. Thus, _data[0] is at memory address _data + 0x20. In the code, this looks like add(_data, 0x20). The array elements follow consecutively, ...


7

DELEGATECALL is an instruction interpreted by EVM. So to execute DELEGATECALL you have to have a program (contract) that gets executed on EVM. DELEGATECALL takes six operands, one of them is the address of the calee. So it can only call deployed contracts. Note that so called library is just a special contract which operations are called with DELEGATECALL. ...


7

This works: return address(keccak256(publicKey) & (2**(8*21)-1)); 2**(8*21)-1 is just a trick to get 0xFFFFFF... (40 Fs) without typing it. :-) EDIT As pointed out by @schnorr, there's no need for the mask: return address(keccak256(publicKey));


7

The explanation is quite simple: In Solidity, bytes is a dynamically-sized byte array: Variables of type bytes and string are special arrays. A bytes is similar to byte[], but it is packed tightly in calldata. string is equal to bytes but does not allow length or index access (for now). In Solidity Assembly, variables are pointers to memory ...


5

This is designed to convert an address to a dynamic bytes type. Addresses are 20 bytes long, and occupy the right-most 20 bytes of a 32-byte word: 0x000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa The bytes type is two (or more) words: the number of bytes followed by the data. The number of bytes in an address is 20 = 0x14, so it need to ...


4

Both create a contract. An externally owned account (EOA) creates a contract by sending a transaction with no to field. A contract (account) creates another contract by using the CREATE opcode. It's cheaper gas-wise for a contract to create another contract, than an EOA to create a contract (TODO fill in gas costs). This question inspires Can the CALL ...


4

Others have pointed in the right direction, but let me try to specifically answer the questions. First, each 256-bit word of contract storage is very expensive. When a contract is using smaller variables, such as uint32 (32 bits), then Solidity will try to pack multiple variables into one storage word. Most of what you see here is the compiler first ...


4

The author of the github page that you referenced didn't update the free memory pointer after allocating the memory. Also as Tjaden Hess noted in the comments return as an opcode and return as a Solidity statement are very different. The opcode causes the entire contract execution to return at that point, with the return value that you designate in ...


4

You can use this function to convert bytes to unit: pragma solidity ^0.4.23; contract mycontract { function bytesToUint(bytes b) public returns (uint256){ uint256 number; for(uint i=0;i<b.length;i++){ number = number + uint(b[i])*(2**(8*(b.length-(i+1)))); } return number; } } The amount of gas used ...


4

The contract prologue instructions changed to provide an additional slot at 0x60, which should always hold the value 0. The documentation states that: The zero slot is used as initial value for dynamic memory arrays and should never be written to (the free memory pointer points to 0x80 initially). FYI I checked several contracts with JEB Decompiler, ...


3

This is about the way that Solidity byte types are represented for use in the EVM: byte types are all left-justified within a 32-byte word, i.e. a single byte type is left-shited by 31 * 8 = 248 bits before being put on the stack. In your first example, variable b is a single-width byte type so it is put on the stack as ...


3

I know this is an old topic, but here's a couple of tutorials I put together to cover assembly in solidity: Functional Assembly - https://www.youtube.com/watch?v=nkGN6GwkMzU Instructional Assembly - https://www.youtube.com/watch?v=axZJ2NFMH5Q I go into the benefits, disadvantages, examples and debugging assembly.


3

Constructor arguments are appended to the deployment byte code in the standard ABI format. For example, this contract takes uint constructor argument. contract Foo { uint a; function Foo(uint _a) public { a = _a; } } It compiles to the following deployment bytecode: ...


3

I'd do something like this, using call() to do an external function call, passing the msg.value inside it. function () payable { bytes4 sig = bytes4(keccak256("()")); // function signature assembly { let x := mload(0x40) // get empty storage location mstore ( x, sig ) // 4 bytes - place signature in empty storage let ret := ...


3

The normal stack is what you describe. This is where the basic opcodes take their operands. The call stack is in relation to calls between contracts. You see, when you send a transaction to a smart contract, the execution there starts with an empty memory and an empty normal stack. That is true when you sign a transaction and send it to a contract. That ...


3

From the docs: calldata is a special data location that contains the function arguments, only available for external function call parameters. Every reference type, i.e. arrays and structs, has an additional annotation, the “data location”, about where it is stored. There are three data locations: memory, storage and calldata. Calldata is only valid ...


3

Yes, checkReturnData() will be able to read the return data from originalContractInterface and will return 1. For context, returndata is the data returned from the last function call. returndatasize is the size of the return data from that call. In the example given in the question, mainFunction() calls originalFunction(), which then sets the returndata ...


3

You'll have a much easier time of this if you can use a recent version of Solidity. Starting with 0.5.x, you can get back the return value as a byte array and then decode it yourself depending on its length. Here's some code that handles both return values (a single uint256 or a pair of them) and returns a 0 as the second value if it wasn't returned from ...


3

Sure. To deploy smart contract one needs to publish a transaction with empty to address and with data containing contract initialization code concatenated with values of constructor parameters. Initialization code usually consists of constructor byte code and contract's byte code to be deployed. So, deployed byte code does not contain constructor and ...


2

Revised as previous answer wasn't completely true as it is possible in memory, but it can only be done in assembly and is quite involved. I've been looking at this quite a lot recently and your issue is primarily down to a memory pointer. There is two reasons why the above won't work in direct memory; the first is the msize opcode should point to the ...


2

It is rather easy I think, even with solidity contract Example{ address recipient = 0x0000cafebabe; function send(){ recipient.send(this.balance); } } results in (optimized bytecode): ...


Only top voted, non community-wiki answers of a minimum length are eligible