-
-
Save eddyz87/60991379c547df11d30fa91901862227 to your computer and use it in GitHub Desktop.
movsx disassembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import io | |
import re | |
def main(): | |
# 64 32 16 8 | |
regs = [('rax', 'eax' , 'ax' , 'al'), #r0 | |
('rdi', 'edi' , 'di' , 'dl'), | |
('rsi', 'esi' , 'si' , 'sil'), | |
('rdx', 'edx' , 'dx' , 'dl'), | |
('rcx', 'ecx' , 'cx' , 'cl'), | |
('r8' , 'r8d' , 'r8w' , 'r8b'), | |
('rbx', 'ebx' , 'bx' , 'bl'), | |
('r13', 'r13d', 'r13w', 'r13b'), | |
('r14', 'r14d', 'r14w', 'r14b'), | |
('r15', 'r15d', 'r15w', 'r15b'), #r9 | |
] | |
B64, B32, B16, B8 = 0, 1, 2, 3 | |
BIT_WIDTH = { B64: 64, B32: 32, B16: 16, B8: 8 } | |
def ereg(s): | |
return not not re.match(r'r[0-9+]', s) | |
def gen_asm(dst_size, src_size, edst, esrc): | |
dst_kind = '<ereg>' if edst else '<non-ereg>' | |
src_kind = '<ereg>' if esrc else '<non-ereg>' | |
dst_width = BIT_WIDTH[dst_size] | |
src_width = BIT_WIDTH[src_size] | |
hdr=f'# {dst_width} dst, {src_width} src, movsx {dst_kind} {src_kind}' | |
with io.StringIO() as f: | |
for _dst in regs: | |
if ereg(_dst[0]) != edst: | |
continue | |
dst = _dst[dst_size] | |
for _src in regs: | |
if ereg(_src[0]) != esrc: | |
continue | |
src = _src[src_size] | |
print(f'movsx {dst}, {src}', file=f) | |
return hdr, f.getvalue() | |
def print_asm(asm): | |
shell = ''' | |
as -o /dev/null -aln -msyntax=intel -mnaked-reg | |
''' | |
p = subprocess.run(['bash', '-c', shell], | |
input=asm, capture_output=True, text=True) | |
if p.stderr: | |
print(p.stderr) | |
print() | |
for line in p.stdout.split('\n'): | |
line = line.strip().split() | |
if len(line) < 2: | |
continue | |
hex_bytes = re.sub(r'..', lambda m: f'{m[0]} ', line[2]).split() | |
if len(hex_bytes) != 4: | |
hex_bytes.insert(0, ' ') | |
hex_encoding = ' '.join(hex_bytes) | |
disasm = ' '.join(line[3:]) | |
print(f'{hex_encoding} {disasm}') | |
print() | |
for dst in [B64, B32]: | |
for src in [B32, B16, B8]: | |
if dst == src: | |
continue | |
for edst in [False, True]: | |
for esrc in [False, True]: | |
hdr, asm = gen_asm(dst, src, edst, esrc) | |
print(hdr) | |
print_asm(asm) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment