/** * @title Storage * @dev Storage & retrieve value in a variable */ contract Storage{ uint256 number; /** * @dev Storage value in variable * @param num value to store */ function store(uint256 num) public { number = num; } /** * @dev Return value * @return value of 'number' */ function retrieve() public view returns (uint256){ return number; } }
// duplicate free memory pointer DUP1 // 0xa0 = 160 in decimal, 32 * 5 = 160 first array is length 5 PUSH1 0xa0 // free memory pointer (0x80) + space for array (0xa0) = new free memory pointer ADD // Save this new value 0x120 to the free memory location PUSH1 0x40 MSTORE
///////////////////////////////////////// // Memory Initialisation Varaible "a" /// ///////////////////////////////////////// // duplicate 0x80 DUP1 // push 0x05 = 5 in decimal (array Length) PUSH1 0x85 // Swqp the top items on the stack in this case 0x05 and 0x80 SWAP1 // push 0x20 = 32 in decimal (array item size) PUSH1 0x20 // Duplicate the 3rd item on the stack in this case 0x05 to the top of the stack DUP3 // 0x05 * 0x20 = 5 * 32 in decmial = 160 on top of the stack (size of array in bytes) MUL // Duplicate 0xa0 = 160 in decimal DUP1 // Returns size of calldata in bytes currently just function signature = 0x04 or 4 in decmial CALLDATASIZE // duplicate 4th item on stack (0x80) DUP4 // 0x80 (byte offset in the memory where the result will be copied.), 0x04 (byte offset in the calldata to copy.), 0xa0 (byte size to copy.) this offsets the 4 bytes in our call data with a size of 0xa0 which yeild a 160 bit set of 0's to be stored at the free memory pointer location // this effectively initialises our array in memory CALLDATACOPY // The remaining lines in this section manipulate the stack to ensure we have the memory location of variable "a" and removes any items that are no longer needed // duplicate 0xa0 DUP1 // duplicate 0x80 DUP3 // new free memory pointer as before ADD // swap 1st (0x120) item on the stack and 3rd (0x80) SWAP2 // pop top item off stack (0x80) POP // pop top item off stack (0xa0) POP // Swap top 2 items 0x120 & 0x05 SWAP1 // pop top item off stack (0x05) POP // pop top item off stack (0x120) POP // swap top 2 items 0x80 & 0xb6 (jump location) SWAP1 // simulating a JUMP remove the top item off stack with POP POP // Simulated jump location PUSH2 0xffff // Simulated jump location PUSH2 0xffff // simulating a JUMP, remove the top item off stack with POP POP
//////////////////////////////////////// // Memory Initialisation Variable "b" // //////////////////////////////////////// // duplicate 0x120 (memory start location for variable "b") DUP1 // 0x02 = 2 in decimal = array length PUSH1 0x02 // swap top 2 items 0x02 & 0x120 SWAP1 // 0x20 = 32 in decimal (array item size in bytes) PUSH1 0x20 // duplicate 3rd item on the stack 0x02 DUP3 // 0x02 * 0x20 = 0x40 = 64 (amount of bytes in memory to initialise) MUL // duplicate 0x40 (free memory pointer location) DUP1 // same as before 4 bytes for function signature 0x04 CALLDATASIZE // duplicate 4th item on the stack = 0x120 DUP4 // 0x120 (byte offset in the memory where the result will be copied.), 0x04 (byte offset in the calldata to copy.), 0x40 (byte size to copy.) CALLDATACOPY // The remaining lines in this section manipulate the stack to ensure we have the memory location of variable "a" and removes any items that are no longer needed //duplicate the top of the stack 0x40 DUP1 // duplicate 3rd item on the stack 0x120 DUP3 // add together yields free memory pointer value ADD // swap 0x160 & 0x120 SWAP2 // pop top item off stack (0x120) POP // pop top item off stack (0x40) POP // swap 0x160 & 0x02 SWAP1 // pop top item off stack (0x02) POP // pop top item off stack (0x160) POP // jump location to top of the stack 0xbe SWAP1 // simulate jump pop jump location off stack POP
////////////////////////// // Assign Value to b[0] // ////////////////////////// // push 0x01, value to add b[0] PUSH1 0x01 // push 0x00 PUSH1 0x00 // left shift operation no shift, first input is 0 SHL // duplicate 2nd item on stack (0x120) DUP2 // push 0x00 = [0] where in the array should this item go PUSH1 0x00 // push 0x20 = 64 bytes the length of the array PUSH1 0x02 // duplicate 2nd item on stack (0x00) DUP2 // 0x00 < 0x20 = true = 0x01 (check the user is not trying to store a value at a location that doesn't exist in the array) LT // jump location PUSH2 0x00d7 // 2 POPs since this is a JUMPI (checking if LT returned true or false) // simulate JUMPI POP // simulate JUMPI POP // push 0x20 (32 bytes aray item size) PUSH1 0x20 // 0x20 * 0x00 = 0x00 = 0 in decimal (array item size * index to determine byte offset) MUL // 0x00 + 0x120 ADD // duplicate 2nd on stack 0x01 (value for b[0]) DUP2 // duplicate 2nd on stack 0x120 (memory location for b[]) DUP2 // store 0x01 at memory location 0x120 MSTORE // clean up stack POP POP POP POP