dis.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env bash
  2. prog="$1"
  3. data="$(od -An -b -w6 $prog)"
  4. function dec8() {
  5. printf "%d" "0$1"
  6. }
  7. function dec16() {
  8. printf "%x" "$1"
  9. }
  10. function decv16() {
  11. printf "%x" "0$1"
  12. [[ ! -z "$2" ]] && [[ "$2" != "000" ]] && printf "%x" "$2"
  13. }
  14. function decv10() {
  15. printf "%d" "$1"
  16. [[ ! -z "$2" ]] && [[ "$2" != "000" ]] && printf "%d" "$2"
  17. }
  18. function decop() {
  19. op="0x$(printf "%x" 0$1 | tr -t [:lower:] [:upper:])"
  20. txt="$(grep -e "$op$" src/vm.h | cut -d' ' -f2)"
  21. printf "(%2x) " "$op"
  22. if [[ "$txt" =~ J.*S ]]; then
  23. printf "\e[1m\e[32m%4s\e[0m" "$txt"
  24. elif [[ "$txt" =~ "RST" ]]; then
  25. printf "\e[32m%4s\e[0m" "$txt"
  26. elif [[ "$txt" =~ J.* ]]; then
  27. printf "\e[1m\e[31m%4s\e[0m" "$txt"
  28. elif [[ "$txt" == "INIT" ]] || [[ "$txt" == "HALT" ]]; then
  29. printf "\e[1m\e[33m%4s\e[0m" "$txt"
  30. elif [[ "$txt" == "ADD" ]] || [[ "$txt" == "SUB" ]] ||
  31. [[ "$txt" == "MLT" ]] || [[ "$txt" == "DIV" ]] ||
  32. [[ "$txt" == "INC" ]] || [[ "$txt" == "DEC" ]] ||
  33. [[ "$txt" == "MOD" ]]; then
  34. printf "\e[1m\e[37m%4s\e[0m" "$txt"
  35. elif [[ "$txt" == "AND" ]] || [[ "$txt" == "OR" ]] ||
  36. [[ "$txt" == "XOR" ]] || [[ "$txt" == "NOT" ]]; then
  37. printf "\e[1m\e[36m%4s\e[0m" "$txt"
  38. elif [[ "$txt" == "CRR" ]] || [[ "$txt" == "CRI" ]] ||
  39. [[ "$txt" == "CRM" ]]; then
  40. printf "\e[1m\e[35m%4s\e[0m" "$txt"
  41. else
  42. printf "%4s" "$txt"
  43. fi
  44. }
  45. i=0
  46. while IFS=$' ' read -r -a line; do
  47. printf "[@%4s]|O:%s|R:%3s,%3s,%3s|V:0x%-2s (%s)\n" \
  48. "$(dec16 $i)" "$(decop ${line[0]})" "$(dec8 ${line[1]})" "$(dec8 ${line[2]})" \
  49. "$(dec8 ${line[3]})" "$(decv16 ${line[4]} ${line[5]})" "$(decv10 ${line[4]} ${line[5]})"
  50. ((i++))
  51. done <<<"$data"