Reputation: 513
I have some assembly I want to clean up. It has all caps, inconsistent spacing and lots of unneeded newlines.
How do I beautify this x86_64 assembly code?
Upvotes: 3
Views: 826
Reputation: 947
I don't know of anything specific to assembly, but the things you've mentioned can be accomplished with sed.
A couple of things to note:
[A-Za-z0-9]+
. Off the top of my head I can't think of any mnemonics that include other characters.r(8|9|1[0-5])(b|w|d)
?[abcd](l|h)|(sp|bp|si|di)l
[er]?([abcd]x|sp|bp|si|di)
xmm(1[0-5]?|[0,2-9])
For example:
# Replace tabs with spaces, then clean up lines of the form "op reg/imm, ..."
# N.B. without the /I option the match will be case-sensitive
sed 's/\t/ /g' <test.s | sed 's/^\s*\([a-z0-9][a-z0-9]*\)\s\s*\([a-z0-9][a-z0-9]*\)\s*,\s*/\t\1\t\2, /I'
# Lowercase all GPRs and SSE vector registers"
# I have chosen not to use the more compact patterns above in the interest of readability.
... | sed '/\([^a-z]\)\(AL|AH|AX|EAX|RAX|...XMM0|XMM1|...|XMM15\)/\1\L\2/gI'
# Lowercase all instruction mnemonics. More specifically, matches the first thing on every line except when it is followed by a colon.
... | sed '/^\s*\([a-z0-9][a-z0-9]*\)\([^:]\)/\L\1\2/I
Upvotes: 5