I have text that looks like this:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
0x0000000f 3bbd98607e3c cmp edi,dword [ebp 1014915224] 15
0x00000015 3229 xor ch,byte [ecx] 21
0x00000017 3c01 cmp al,1 23
0x00000019 25040bbe7d and eax,0x7dbe0b04 25
0x0000001e 13fd adc edi,ebp 30
0x00000020 b48d mov ah,141 32
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164 36
0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47
I want to format the mnemonics in the string (3rd position) to be the same space away from the call (2nd position) no matter how large the call is. For example:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
The above is the perfect example of what I would need to do. I've tried using string formatting techniques such as "{} {:<10} {:>10} {}\n"
, however this shows up not exactly as I would expect it too looking something like this:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
0x0000000f 3bbd98607e3c cmp edi,dword [ebp 1014915224] 15
0x00000015 3229 xor ch,byte [ecx] 21
0x00000017 3c01 cmp al,1 23
0x00000019 25040bbe7d and eax,0x7dbe0b04 25
0x0000001e 13fd adc edi,ebp 30
0x00000020 b48d mov ah,141 32
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164 36
0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47
How can I dynamically change the formatting of each string in order to have the mnemonics of the instructions in the same area for each line to improve readability?
CodePudding user response:
This will format a line
as you asked:
"%s %-12s %s" % tuple(line.split(' ',2))
CodePudding user response:
Here is a solution with loops:
s = "0x00000000 ebc4 jmp 0x-0000003a 0\n0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2\n0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8\n0x0000000d b182 mov cl,130 13\n0x0000000f 3bbd98607e3c cmp edi,dword [ebp 1014915224] 15\n0x00000015 3229 xor ch,byte [ecx] 21\n0x00000017 3c01 cmp al,1 23\n0x00000019 25040bbe7d and eax,0x7dbe0b04 25\n0x0000001e 13fd adc edi,ebp 30\n0x00000020 b48d mov ah,141 32\n0x00000022 af scasd 34\n0x00000023 2f das 35\n0x00000024 34a4 xor al,164 36\n0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38\n0x0000002c 9c pushfd 44\n0x0000002d 90 nop 45\n0x0000002e 90 nop 46\n0x0000002f 90 nop 47"
lines = s.split("\n")
split_lines = [line.split() for line in lines]
new_lines = [split[0] " " split[1] for split in split_lines]
for i, line in enumerate(new_lines):
while len(line) < 24:
line = " "
line = split_lines[i][2] " " split_lines[i][3]
new_lines[i] = line
for line in new_lines:
print(line)
Output:
0x00000000 ebc4 jmp 0x-0000003a
0x00000002 30b9aaad03f0 xor byte
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb
0x0000000d b182 mov cl,130
0x0000000f 3bbd98607e3c cmp edi,dword
0x00000015 3229 xor ch,byte
0x00000017 3c01 cmp al,1
0x00000019 25040bbe7d and eax,0x7dbe0b04
0x0000001e 13fd adc edi,ebp
0x00000020 b48d mov ah,141
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164
0x00000026 02929d1302a3 add dl,byte
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47