Code has been added to clipboard!
Solidity Standalone Assembly Example 2
Example
{
mstore(0x40, 0x60)
{
let $0 := div(calldataload(0), exp(2, 226))
jumpi($case1, eq($0, 0xb3de648b))
jump($caseDefault)
$case1:
{
// call the function and put arguments and return label onto the stack
$retVal1 calldataload(4) jump(f)
// This code is unreachable. Opcode, which mirror the function's
// effect are added onto the stack height: Remove arguments
// and introduce the return values.
pop pop
let r := 0
$retVal1: // the return point itself
$retVal2 0x20 jump($allocate)
pop pop let retVal := 0
$retVal2:
mstore(retVal, r)
return(retVal, 0x20)
// jump is automatically inserted, despite being useless, because
// the desugaring process is an operatiopn that is purely syntactic
// meaning that is does not analyze control-flow
jump($switchEnd)
}
$caseDefault:
{
revert(0, 0)
jump($switchEnd)
}
$switchEnd:
}
jump($functionAfter)
allocate:
{
// The unreachable code introducing the function arguments is jumped over
jump($toStart)
let $retposition := 0 let size := 0
$toStart:
// both arguments and output variables live in the same scope, which
// actually is allocated.
let pos := 0
{
pos := mload(0x40)
mstore(0x40, add(pos, size))
}
// Replaces arguments with the return values, jumping back afterwards
swap1 pop swap1 jump
// Code that cannot be reached, correcting stack height
0 0
}
f:
{
jump($toStart)
let $retposition := 0 let a := 0
$toStart:
let b := 0
{
let n := 0
$for_start:
jumpi($for_finish, iszero(lt(n, a)))
{
b := mul(2, b)
}
$for_cont:
{ n := add(n, 1) }
jump($for_start)
$for_finish:
}
swap1 pop swap1 jump // Here, a pop instruction is going to be inserted for n
0 0
}
$functionAfter:
stop
}