Skip to content

Instantly share code, notes, and snippets.

@dougallj

dougallj/asm.py Secret

Created March 19, 2021 22:14
Show Gist options
  • Save dougallj/c9976a52d592af24960ea7989cf652b1 to your computer and use it in GitHub Desktop.
Save dougallj/c9976a52d592af24960ea7989cf652b1 to your computer and use it in GitHub Desktop.
# python3 asm.py 'nop'
import os
import subprocess
import sys
import struct
def assemble(code, verbose=False):
if not code:
return b''
with open('asm.tmp1.s', 'w') as f:
f.write(code.replace(';', '\n'))
if subprocess.call(['clang', '-c', 'asm.tmp1.s', '-march=armv8.5-a+sha3+fp16fml']):
exit(1)
for line in subprocess.check_output(['objdump', '-d', 'asm.tmp1.o', '--wide']).decode('utf-8').split('\n')[7:-1]:
a, b = line.split('\t', 1)
print('ibuf[o++] = 0x%08x; // %s' % (struct.unpack('<I', bytes.fromhex(a.split(': ')[1].replace(' ', '')))[0], b.replace('\t', ' ').strip()))
txt = subprocess.check_output(['otool', '-t', 'asm.tmp1.o']).decode('ascii')
u32s = []
for line in txt.strip().split('\n')[2:]:
for u32 in line.split()[1:]:
u32s.append(struct.pack('<I', int(u32, 16)))
os.unlink('asm.tmp1.s')
os.unlink('asm.tmp1.o')
return b''.join(u32s)
assemble('\n'.join(sys.argv[1:]))
@name99-org
Copy link

I guess XCode 13.3 slightly changed the format of objdump?
As of 13.3 I find I have to change line 16 to extract elements [6:-1]; the code as shown right now with [7:-1] returns nothing, just an empty line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment