0

Replace specific string (below) in file in Python

I have a text file that is something like the following:

test   al,al
jne    0x514 <asm4+23>
mov    DWORD PTR [ebp-0x8],0x1
jmp    0x587 <asm4+138>
mov    edx,DWORD PTR [ebp-0x8]

I need the result to be like this

test   al,al
jne    asm4+23
mov    DWORD PTR [ebp-0x8],0x1
jmp    asm4+138
mov    edx,DWORD PTR [ebp-0x8]

3 Answers3

1

This could work for your example. I have used python regular expressions

import re

regex = r"(0.+<)(.+)>"
input_string="""
test   al,al
jne    0x514 <asm4+23>
mov    DWORD PTR [ebp-0x8],0x1
jmp    0x587 <asm4+138>
mov    edx,DWORD PTR [ebp-0x8]

"""
output=re.sub(r"(0.+<)(.+)>", r"\2", input_string)
print(output)

Hope this helps

ArunJose_Intel
  • 1,991
  • 1
  • 3
  • 16
0

You can use just ".startswith" method:

test = """
    test   al,al
    jne    0x514 <asm4+23>
    mov    DWORD PTR [ebp-0x8],0x1
    jmp    0x587 <asm4+138>
    mov    edx,DWORD PTR [ebp-0x8]
    """.splitlines()

i = 0
while i < len(test):
    if test[i].startswith("jne"):
        test[i] = "jne    0x514 <asm4+23>"
    elif test[i].startswith("jmp"):
        test[i] = "jmp    0x587 <asm4+138>"
    i += 1

print("\n".join(test))
mind-protector
  • 238
  • 2
  • 12
0

You're converting disassembly into something that can be re-assembled.

Instead of post-processing GDB or objdump -drwC -Mintel output, use a disassembler that outputs in actual GAS or NASM syntax in the first place, with extra info in comments. Specifically for x86, Agner Fog's objconv.

See How to generate assembly code with gcc that can be compiled with nasm for example output.

(I don't think it can target GAS .intel_syntax, only GAS, NASM/YASM, or MASM. But GAS .intel_syntax is MASM-like except for the directives so that may be useful. Or if you don't mind AT&T syntax, it can be assembled directly without installing NASM.)

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606