assemblyx86machine-codeself-modifying

Change from MOV instructions to ADD or SUB instructions


I want to change from mov instructions to SUB instructions (I think ,we can also change to ADD instructions) and I want to adjust the values so that the function of the entire program remains unchanged.

   for_real_programmers:
     mov dx, 0 ; 
     inc dx ;

   for_leet_hackorz:
     mov word ptr [for_real_programmers], 0d929h ; ?
     mov word ptr [for_real_programmers+2], 0d9f7h ; ?
     mov byte ptr [for_leet_hackorz], 0c3h ; ?
     jmp restart

Solution

  • The machine-code byte you want to modify are going to be the same every time for this self-modifying code (assuming it only runs once), so yes it is possible to just add dst, desired - orig_dst instead of mov dst, desired.

    First change the mov mnemonics to add or sub, then assemble and look at a listing or hexdump. That gives you the starting values of the destinations, so you can work out what immediates to use.

    Make those changes in your asm source and rebuild again + test.

    The bytes you're self-modifying are separate from the immediate operands to mov / add / sub. One of the bytes you change is replacing the opcode of the first instruction in for_leet_hackorz (really?) with a C3 ret, which is why you need to change the mnemonics to add or sub before calculating the difference between starting vs. desired.