Skip to content

Instantly share code, notes, and snippets.

@eramax
Last active April 12, 2023 07:22
Show Gist options
  • Save eramax/52410b11c4f09e366da3a5e08486bd75 to your computer and use it in GitHub Desktop.
Save eramax/52410b11c4f09e366da3a5e08486bd75 to your computer and use it in GitHub Desktop.
assembly and disassembly x86 64 instructions
n() { ndisasm -b64 tmp.bin; xxd -g1 tmp.bin; xxd -b tmp.bin; rm tmp.bin; }
a() { { echo "bits 64"; echo "$@"; } > tmp.s; nasm -f bin -D ARCH_x86_64 -o tmp.bin tmp.s; n; }
h() { printf '%s' "$@" | xxd -r -p > tmp.bin; n; }
b() { binary_value=$(echo "$@" | tr -d '[:space:]\n'); hex_value=$(printf '%0*X' $(((${#binary_value}+3)/4)) "$((2#$binary_value))"); h "$hex_value"; }
hx() { h "${@//[[:space:]]/}" ; }
# nasm_build() {
# ndisasm -b 64 tmp.bin
# xxd -g1 tmp.bin
# xxd -b tmp.bin
# rm tmp.bin
# }
# asm() {
# { echo "bits 64"; echo "$@"; } > tmp.s
# nasm -f bin -D ARCH_x86_64 -o tmp.bin tmp.s
# nasm_build
# }
# nasm_hex() {
# printf '%s' "$1" | xxd -r -p > tmp.bin
# nasm_build
# }
# bin() {
# binary_value=$(echo "$*" | tr -d ' ')
# hex_value=$(printf '%0*X' $(((${#binary_value}+3)/4)) "$((2#$binary_value))")
# nasm_hex "$hex_value"
# }
# hex() {
# hex_value=$(echo "$*" | tr -d ' ')
# nasm_hex "$hex_value"
# }
## Tests
# $ hx 01 f1 01 f1  ✔
# 00000000 01F1 add ecx,esi
# 00000002 01F1 add ecx,esi
# 00000000: 01 f1 01 f1 ....
# 00000000: 00000001 11110001 00000001 11110001 ....
# $ a 'add ecx,esi'  ✔
# 00000000 01F1 add ecx,esi
# 00000000: 01 f1 ..
# 00000000: 00000001 11110001 ..
# $ b 00000001 11110001  ✔
# 00000000 01F1 add ecx,esi
# 00000000: 01 f1 ..
# 00000000: 00000001 11110001 ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment