Skip to content

Instantly share code, notes, and snippets.

@chriseth
Created March 29, 2016 14:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseth/435f4d0c1603f721c7fe to your computer and use it in GitHub Desktop.
Save chriseth/435f4d0c1603f721c7fe to your computer and use it in GitHub Desktop.
EVM in EVM
contract EVM {
struct VMState {
uint[1024] stack;
uint stackHeight;
bytes bytecode;
uint pc;
uint[] mem;
}
function step(VMState _state) internal returns (bool)
{
if (_state.pc >= _state.bytecode.length) return false;
byte instr = _state.bytecode[_state.pc];
if (instr == 0x00) // STOP
return true;
else if (instr == 0x01) // ADD
{
if (_state.stackHeight < 2) return false;
_state.stack[_state.stackHeight - 2] = _state.stack[_state.stackHeight - 1] + _state.stack[_state.stackHeight - 2];
_state.stackHeight--;
_state.pc++;
}
else if (instr == 0x02) // MUL
{
if (_state.stackHeight < 2) return false;
_state.stack[_state.stackHeight - 2] = _state.stack[_state.stackHeight - 1] * _state.stack[_state.stackHeight - 2];
_state.stackHeight--;
_state.pc++;
}
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment