I'm disassembling an old (1996) game, that has been compiled with the Watcom 386 compiler. This compiler seems to aggressively reorder instructions to make better use of the processor pipeline, as seen in this chunk of assembly:

enter image description here

The instructions marked with a red dot set up the parameters for the next call; the instructions with a blue dot finish the initialization of the object returned from the previous call. Rearranging them makes the assembly much easier to read:

call    ClassAlloc13680_0FAh
mov     edx, [eax]
mov     [edx+Class180F4.WidgetInputHandler], offset gblHandleTransportDestinationAndCheckForPassengersSpace
mov     edx, [eax]
mov     [edx+Class13680.Paint???], offset ClassVehicleManager__PaintForSomeWidget
mov     dword_A4D88, eax
mov     eax, [eax]
mov     [eax+Class10B8C.MouseInputHandler], offset ClassVehicleManager__MouseInputHandler

push    0
push    2
push    0
push    4Eh
push    5Bh
mov     ebx, 5Ch
mov     ecx, 21h
mov     edx, ebp
mov     eax, ebp
call    ClassAlloc13680_0FAh
push    0
push    2

(Note that i moved the mov reg, xxh instructions even further down, because the compiler's calling convention is ax-dx-cx-bx-stack, so i can see the order of arguments here as well)

Is there a way to accomplish this in IDA? I'm not asking for an algorithm to automatically determine which instructions should be "red" and which should be "blue", and i don't want to patch the original binary file, i'd just like to manually re-arrange instructions in the ida database.

Or is there another way to improve readability of this kind of code in IDA?

  • You can write an IDC/IDAPython script to move the selected instructions around, e.g.: you select a sequence of instructions, invoke script, enter the number of bytes to move these instructions down/up, script reorders them using PatchByte or some similar command.
    – DCoder
    Aug 19, 2014 at 13:06
  • 3
    I wrote an idc script some time ago to do this for arm code: swapinsn.idc. It will rotate a sequence of insns up or down, and fix any relative jumps in that range Aug 19, 2014 at 21:49
  • @WillemHengeveld: Could you make this an answer? It's 90% of what i need, and i'd like to award the rep points to you. Aug 22, 2014 at 5:19

1 Answer 1


Several years ago i wrote swapinsn.idc to do this for ARM code. It will rotate a sequence of insns up or down, and fix any relative jumps in that range.

Note that contrary to the comments in the script, i never actually added x86 support.

For convenience i added hotkey functions HK_ExchangeDown and HK_ExchangeUp as defined in hotkeys.idc. So in can select a range of instructions, type shift-x to move the last selected insn up, and the rest down.

  • 1
    Btw, i don't have my laptop with me right now, next week i could probably add the x86 support Aug 22, 2014 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.