github.com/karalabe/go-ethereum@v0.8.5/vm/asm.go (about)

     1  package vm
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  
     7  	"github.com/ethereum/go-ethereum/ethutil"
     8  )
     9  
    10  func Disassemble(script []byte) (asm []string) {
    11  	pc := new(big.Int)
    12  	for {
    13  		if pc.Cmp(big.NewInt(int64(len(script)))) >= 0 {
    14  			return
    15  		}
    16  
    17  		// Get the memory location of pc
    18  		val := script[pc.Int64()]
    19  		// Get the opcode (it must be an opcode!)
    20  		op := OpCode(val)
    21  
    22  		asm = append(asm, fmt.Sprintf("%v", op))
    23  
    24  		switch op {
    25  		case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
    26  			pc.Add(pc, ethutil.Big1)
    27  			a := int64(op) - int64(PUSH1) + 1
    28  			if int(pc.Int64()+a) > len(script) {
    29  				return nil
    30  			}
    31  
    32  			data := script[pc.Int64() : pc.Int64()+a]
    33  			if len(data) == 0 {
    34  				data = []byte{0}
    35  			}
    36  			asm = append(asm, fmt.Sprintf("0x%x", data))
    37  
    38  			pc.Add(pc, big.NewInt(a-1))
    39  		}
    40  
    41  		pc.Add(pc, ethutil.Big1)
    42  	}
    43  
    44  	return
    45  }