Created
April 11, 2014 18:14
-
-
Save Experiment5X/10489245 to your computer and use it in GitHub Desktop.
Convert an assembly instruction into its bytecode. It uses gas and otool, so this will only work on OS X, but it'd be pretty easy to modify it for linux.
This file contains hidden or 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
# USAGE INSTRUCTIONS | |
# It's an interactive shell, where you have the following options... | |
# - Just type in an assembly instruction in Intel syntax, and it'll spit out the bytecode | |
# - Change the syntax to AT&T with the att command | |
# - Change the syntax back to Intel with the intel command | |
# - Quit with the q command | |
import os | |
import sys | |
import tempfile | |
import subprocess | |
import re | |
intelSyntax = True | |
def getBytecode(instruction): | |
gasCode = '' | |
if intelSyntax: | |
gasCode = '.globl _start\n\n.text\n.intel_syntax\n_start:\t%s\n' % instruction | |
else: | |
gasCode = '.globl _start\n\n.text\n_start:\t%s\n' % instruction | |
asmFile = open('%s/code.s' % os.getcwd(), 'w') | |
asmFile.write(gasCode) | |
asmFile.close() | |
# assemble the instruction | |
gasProcess = subprocess.Popen(['as', '-o', '%s/out.o' % os.getcwd(), '%s/code.s' % os.getcwd()], stderr=subprocess.PIPE) | |
gasProcess.wait() | |
if len(gasProcess.stderr.read()) != 0: | |
print 'Invalid instruction' | |
return None | |
# get the bytecode from the object file | |
otoolProcess = subprocess.Popen(['otool', '-tVj', '%s/out.o' % os.getcwd()], stdout=subprocess.PIPE) | |
otoolProcess.wait() | |
# delete the files created | |
os.remove('%s/code.s' % os.getcwd()) | |
os.remove('%s/out.o' % os.getcwd()) | |
# extract the instruction's bytecode from the output | |
cocks = re.compile('\d+\s+([0-9a-fA-F]+)') | |
matches = cocks.search(otoolProcess.stdout.read()) | |
if len(matches.groups()) < 1: | |
print 'Couldn\'t locate bytecode in otool output' | |
return None | |
else: | |
return matches.groups(1)[0] | |
sys.stdout.write('>> ') | |
userInput = raw_input() | |
while userInput != 'q': | |
if userInput == 'intel': | |
intelSyntax = True | |
print 'Changed to Intel syntax' | |
elif userInput == 'att': | |
intelSyntax = False | |
print 'Changed to AT&T Syntax' | |
else: | |
bytecode = getBytecode(userInput) | |
if bytecode != None: | |
print bytecode | |
sys.stdout.write('>> ') | |
userInput = raw_input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.text
.file "kernel.c"
.globl kmain // -- Begin function kmain
.p2align 2
.type kmain,@function
kmain: // @kmain
.cfi_startproc
// %bb.0:
sub sp, sp, #16
.cfi_def_cfa_offset 16
mov x8, #32768 // =0x8000
movk x8, #11, lsl #16
str x8, [sp, #8]
ldr x9, [sp, #8]
mov w8, #68 // =0x44
strb w8, [x9]
ldr x9, [sp, #8]
mov w8, #15 // =0xf
strb w8, [x9, #1]
add sp, sp, #16
.cfi_def_cfa_offset 0
ret
.Lfunc_end0:
.size kmain, .Lfunc_end0-kmain
.cfi_endproc
// -- End function
.ident "clang version 17.0.6"
.section ".note.GNU-stack","",@progbits