github.com/chenzhuoyu/iasm@v0.9.1/repl/display.go (about)

     1  package repl
     2  
     3  import (
     4      `fmt`
     5      `strings`
     6      `unicode`
     7  )
     8  
     9  func vector(v []byte, indent int) string {
    10      n := len(v)
    11      m := make([]string, 0, len(v))
    12  
    13      /* convert each element */
    14      for i := 0; i < n; i++ {
    15          if i == 0 || (i & 15) != 0 {
    16              m = append(m, fmt.Sprintf("0x%02x", v[n - i - 1]))
    17          } else {
    18              m = append(m, fmt.Sprintf("\n%s0x%02x", strings.Repeat(" ", indent + 1), v[n - i - 1]))
    19          }
    20      }
    21  
    22      /* join them together */
    23      return fmt.Sprintf(
    24          "{%s}",
    25          strings.Join(m, ", "),
    26      )
    27  }
    28  
    29  func display(v byte) string {
    30      if !unicode.IsPrint(rune(v)) {
    31          return "."
    32      } else {
    33          return string(v)
    34      }
    35  }
    36  
    37  func vecdiff(v0 []byte, v1 []byte, indent int) string {
    38      return vector(v0, indent) + strings.Repeat(" ", indent - 2) + "->" + vector(v1, indent)
    39  }
    40  
    41  func asmdump(m []byte, pos uintptr, src string) string {
    42      row := -1
    43      ret := []string(nil)
    44  
    45      /* must have at least 1 byte */
    46      if len(m) == 0 {
    47          panic("empty instruction bytes")
    48      }
    49  
    50      /* convert all the bytes */
    51      for i, v := range m {
    52          if i % 7 != 0 {
    53              ret[row] = ret[row] + fmt.Sprintf(" %02x", v)
    54          } else {
    55              ret, row = append(ret, fmt.Sprintf("(%#06x) %02x", pos + uintptr(i), v)), row + 1
    56          }
    57      }
    58  
    59      /* pad the first line if needed */
    60      if n := len(m); n < 7 {
    61          ret[0] += strings.Repeat(" ", (7 - n) * 3)
    62      }
    63  
    64      /* add the source */
    65      ret[0] += "    " + src
    66      return strings.Join(ret, "\n")
    67  }
    68  
    69  func hexdump(buf []byte, start uintptr) string {
    70      off := 0
    71      nbs := len(buf)
    72      ret := []string(nil)
    73  
    74      /* dump the data, 16-byte loop */
    75      for nbs >= 16 {
    76          _ = buf[off + 15]
    77          ret = append(ret, fmt.Sprintf("%x:", start + uintptr(off)))
    78          ret = append(ret, fmt.Sprintf(" %02x %02x %02x %02x", buf[off +  0], buf[off +  1], buf[off +  2], buf[off +  3]))
    79          ret = append(ret, fmt.Sprintf(" %02x %02x %02x %02x", buf[off +  4], buf[off +  5], buf[off +  6], buf[off +  7]), " ")
    80          ret = append(ret, fmt.Sprintf(" %02x %02x %02x %02x", buf[off +  8], buf[off +  9], buf[off + 10], buf[off + 11]))
    81          ret = append(ret, fmt.Sprintf(" %02x %02x %02x %02x", buf[off + 12], buf[off + 13], buf[off + 14], buf[off + 15]), "  ")
    82  
    83          /* print the 16 characters */
    84          for i := 0; i < 16; i++ {
    85              ret = append(ret, display(buf[off + i]))
    86          }
    87  
    88          /* move to the next line */
    89          off = off + 16
    90          nbs = nbs - 16
    91          ret = append(ret, "\n")
    92      }
    93  
    94      /* still have more bytes */
    95      if nbs > 0 {
    96          ret = append(ret, fmt.Sprintf("%x:", start + uintptr(off)))
    97      }
    98  
    99      /* print the remaining bytes */
   100      for i := 0; i < nbs; i++ {
   101          if i != 7 {
   102              ret = append(ret, fmt.Sprintf(" %02x", buf[off + i]))
   103          } else {
   104              ret = append(ret, fmt.Sprintf(" %02x ", buf[off + i]))
   105          }
   106      }
   107  
   108      /* pad the last line */
   109      if nbs > 0 {
   110          if nbs < 8 {
   111              ret = append(ret, strings.Repeat(" ", (17 - int(nbs)) * 3))
   112          } else {
   113              ret = append(ret, strings.Repeat(" ", (17 - int(nbs)) * 3 - 1))
   114          }
   115      }
   116  
   117      /* print the last characters */
   118      for i := 0; i < nbs; i++ {
   119          ret = append(ret, display(buf[off + i]))
   120      }
   121  
   122      /* check the final terminator */
   123      if nbs <= 0 {
   124          return strings.Join(ret, "")
   125      } else {
   126          return strings.Join(append(ret, "\n"), "")
   127      }
   128  }