-2

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Would you please rephrase your question? Do you want to convert `mov` to equivalent `add` or `sub` instructions? – JCWasmx86 Aug 07 '20 at 08:31
  • don't post texts as image. Just copy and paste them here – phuclv Aug 07 '20 at 09:10
  • Do not post pictures of code. I have for this reason downvoted your question and will take back my downvote once you [edit] your question and replace the picture with text. – fuz Aug 07 '20 at 09:24
  • @fuz ,I think the post is more better more :) .Thank you for your advice – Roméo Tia Aug 07 '20 at 12:42
  • You can set a variable to zero by subtracting it from itself. You can increase a variable by 1 by subtracting -1 ... – Sebastian Aug 15 '20 at 02:18

1 Answers1

1

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • OK, but when you say :add dst, desired - orig_dst instead of mov dst, desired.,do you mean what exactly ? I know add cx,2ah and I work with assembler 8086 and not with asm @peter Cordes – Roméo Tia Aug 07 '20 at 17:31
  • I mean a memory-destination `add`, instead of a memory-destination `mov`. Literally change just the `mov` to `add` in the source. So the destination is the same as it was for `mov`, but instead of replacing it without caring about the old value of those bytes, you're adding or subtracting to/from it. – Peter Cordes Aug 07 '20 at 17:37
  • @RoméoTia: "asm" is an abbreviation for "assembly language". I'm not talking about any specific tool. That would be like saying "I went to an automated teller machine to get cash, not an ATM". – Peter Cordes Aug 07 '20 at 17:38
  • Thank you for your reaction .Where I have to put c3 ret exactly? I put it like you notifice ,but it don´t work .I put it on teh first line of For_leet_hackorz .Thank you – Roméo Tia Aug 07 '20 at 17:52