1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
pragma solidity >=0.4.21 <0.6.0;
contract Test1 {
function isSameAddress(address a, address b) public returns(bool){ //Simply add the two arguments and return
if (a == b) return true;
return false;
}
}
contract OpCodes {
Test1 test1;
constructor() public { //Constructor function
test1 = new Test1(); //Create new "Test1" function
}
modifier onlyOwner(address _owner) {
require(msg.sender == _owner);
_;
}
// Add a todo to the list
function test() public {
//simple_instructions
/*assembly { pop(sub(dup1, mul(dup1, dup1))) }*/
//keywords
assembly { pop(address) return(2, byte(2,1)) }
//label_complex
/*assembly { 7 abc: 8 eq jump(abc) jumpi(eq(7, 8), abc) pop }
assembly { pop(jumpi(eq(7, 8), abc)) jump(abc) }*/
//functional
/*assembly { let x := 2 add(7, mul(6, x)) mul(7, 8) add =: x }*/
//for_statement
assembly { for { let i := 1 } lt(i, 5) { i := add(i, 1) } {} }
assembly { for { let i := 6 } gt(i, 5) { i := add(i, 1) } {} }
assembly { for { let i := 1 } slt(i, 5) { i := add(i, 1) } {} }
assembly { for { let i := 6 } sgt(i, 5) { i := add(i, 1) } {} }
//no_opcodes_in_strict
assembly { pop(callvalue()) }
//no_dup_swap_in_strict
/*assembly { swap1() }*/
//print_functional
assembly { let x := mul(sload(0x12), 7) }
//print_if
assembly { if 2 { pop(mload(0)) }}
//function_definitions_multiple_args
assembly { function f(a, d){ mstore(a, d) } function g(a, d) -> x, y {}}
//sstore
assembly { function f(a, d){ sstore(a, d) } function g(a, d) -> x, y {}}
//mstore8
assembly { function f(a, d){ mstore8(a, d) } function g(a, d) -> x, y {}}
//calldatacopy
assembly {
let a := mload(0x40)
let b := add(a, 32)
calldatacopy(a, 4, 32)
/*calldatacopy(b, add(4, 32), 32)*/
/*result := add(mload(a), mload(b))*/
}
//codecopy
assembly {
let a := mload(0x40)
let b := add(a, 32)
codecopy(a, 4, 32)
}
//codecopy
assembly {
let a := mload(0x40)
let b := add(a, 32)
extcodecopy(0, a, 4, 32)
}
//for_statement
assembly { let x := calldatasize() for { let i := 0} lt(i, x) { i := add(i, 1) } { mstore(i, 2) } }
//keccak256
assembly { pop(keccak256(0,0)) }
//returndatasize
assembly { let r := returndatasize }
//returndatacopy
assembly { returndatacopy(64, 32, 0) }
//byzantium vs const Constantinople
//staticcall
assembly { pop(staticcall(10000, 0x123, 64, 0x10, 128, 0x10)) }
/*//create2 Constantinople
assembly { pop(create2(10, 0x123, 32, 64)) }*/
//create Constantinople
assembly { pop(create(10, 0x123, 32)) }
//shift Constantinople
/*assembly { pop(shl(10, 32)) }
assembly { pop(shr(10, 32)) }
assembly { pop(sar(10, 32)) }*/
//not
assembly { pop( not(0x1f)) }
//exp
assembly { pop( exp(2, 226)) }
//mod
assembly { pop( mod(3, 9)) }
//smod
assembly { pop( smod(3, 9)) }
//div
assembly { pop( div(4, 2)) }
//sdiv
assembly { pop( sdiv(4, 2)) }
//iszero
assembly { pop(iszero(1)) }
//and
assembly { pop(and(2,3)) }
//or
assembly { pop(or(3,3)) }
//xor
assembly { pop(xor(3,3)) }
//addmod
assembly { pop(addmod(3,3,6)) }
//mulmod
assembly { pop(mulmod(3,3,3)) }
//signextend
assembly { pop(signextend(1, 10)) }
//sha3
assembly { pop(calldataload(0)) }
//blockhash
assembly { pop(blockhash(sub(number(), 1))) }
//balance
assembly { pop(balance(0x0)) }
//caller
assembly { pop(caller()) }
//codesize
assembly { pop(codesize()) }
//extcodesize
assembly { pop(extcodesize(0x1)) }
//origin
assembly { pop(origin()) }
//gas
assembly { pop(gas())}
//msize
assembly { pop(msize())}
//pc
assembly { pop(pc())}
//gasprice
assembly { pop(gasprice())}
//coinbase
assembly { pop(coinbase())}
//timestamp
assembly { pop(timestamp())}
//number
assembly { pop(number())}
//difficulty
assembly { pop(difficulty())}
//gaslimit
assembly { pop(gaslimit())}
//call
address contractAddr = address(test1);
bytes4 sig = bytes4(keccak256("isSameAddress(address,address)")); //Function signature
address a = msg.sender;
assembly {
let x := mload(0x40) //Find empty storage location using "free memory pointer"
mstore(x,sig) //Place signature at beginning of empty storage
mstore(add(x,0x04),a) // first address parameter. just after signature
mstore(add(x,0x24),a) // 2nd address parameter - first padded. add 32 bytes (not 20 bytes)
mstore(0x40,add(x,0x64)) // this is missing in other examples. Set free pointer before function call. so it is used by called function.
// new free pointer position after the output values of the called function.
let success := call(
5000, //5k gas
contractAddr, //To addr
0, //No wei passed
x, // Inputs are at location x
0x44, //Inputs size two padded, so 68 bytes
x, //Store output over input
0x20) //Output is 32 bytes long
}
//callcode
assembly {
let x := mload(0x40) //Find empty storage location using "free memory pointer"
mstore(x,sig) //Place signature at beginning of empty storage
mstore(add(x,0x04),a) // first address parameter. just after signature
mstore(add(x,0x24),a) // 2nd address parameter - first padded. add 32 bytes (not 20 bytes)
mstore(0x40,add(x,0x64)) // this is missing in other examples. Set free pointer before function call. so it is used by called function.
// new free pointer position after the output values of the called function.
let success := callcode(
5000, //5k gas
contractAddr, //To addr
0, //No wei passed
x, // Inputs are at location x
0x44, //Inputs size two padded, so 68 bytes
x, //Store output over input
0x20) //Output is 32 bytes long
}
//delegatecall
assembly {
let x := mload(0x40) //Find empty storage location using "free memory pointer"
mstore(x,sig) //Place signature at beginning of empty storage
mstore(add(x,0x04),a) // first address parameter. just after signature
mstore(add(x,0x24),a) // 2nd address parameter - first padded. add 32 bytes (not 20 bytes)
mstore(0x40,add(x,0x64)) // this is missing in other examples. Set free pointer before function call. so it is used by called function.
// new free pointer position after the output values of the called function.
let success := delegatecall(
5000, //5k gas
contractAddr, //To addr
x, // Inputs are at location x
0x44, //Inputs size two padded, so 68 bytes
x, //Store output over input
0x20) //Output is 32 bytes long
}
uint256 _id = 0x420042;
//log0
log0(
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20)
);
//log1
log1(
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20)
);
//log2
log2(
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
bytes32(uint256(msg.sender))
);
//log3
log3(
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
bytes32(uint256(msg.sender)),
bytes32(_id)
);
//log4
log4(
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
bytes32(0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20),
bytes32(uint256(msg.sender)),
bytes32(_id),
bytes32(_id)
);
//selfdestruct
assembly { selfdestruct(0x02) }
}
function test_revert() public {
//revert
assembly{ revert(0, 0) }
}
function test_invalid() public {
//revert
assembly{ invalid() }
}
function test_stop() public {
//revert
assembly{ stop() }
}
}