asm.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "fmt"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. func Disassemble(script []byte) (asm []string) {
  23. pc := new(big.Int)
  24. for {
  25. if pc.Cmp(big.NewInt(int64(len(script)))) >= 0 {
  26. return
  27. }
  28. // Get the memory location of pc
  29. val := script[pc.Int64()]
  30. // Get the opcode (it must be an opcode!)
  31. op := OpCode(val)
  32. asm = append(asm, fmt.Sprintf("%v", op))
  33. switch op {
  34. 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:
  35. pc.Add(pc, common.Big1)
  36. a := int64(op) - int64(PUSH1) + 1
  37. if int(pc.Int64()+a) > len(script) {
  38. return nil
  39. }
  40. data := script[pc.Int64() : pc.Int64()+a]
  41. if len(data) == 0 {
  42. data = []byte{0}
  43. }
  44. asm = append(asm, fmt.Sprintf("0x%x", data))
  45. pc.Add(pc, big.NewInt(a-1))
  46. }
  47. pc.Add(pc, common.Big1)
  48. }
  49. return
  50. }