Created
          March 29, 2016 14:52 
        
      - 
      
- 
        Save chriseth/435f4d0c1603f721c7fe to your computer and use it in GitHub Desktop. 
    EVM in EVM
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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