r/computerscience • u/ShadowGuyinRealLife • 2d ago
General Does Replacing Instructions that Do Nothing with NOPs Change Anything?
Suppose in the executable, instructions N to N+6 do some load instructions from memory, then N+7 to N+30 are some integer operations like add, subtract, multiply, bitwise operations and so on. the results are not stored to memory. Then Instruction N+31 to N+37 loads new values in the registers and none of the results of the previous calculations do anything. A different version of the executable has instructions N to N+30 all be "NOP" and N+31 to N+37 are the same as before. Does anything change? My inspiration for this is the game Mario 64 had been dissected and if you look at the assembly of the NTCS version, there are plenty of instructions that do some calculations that aren't used and some weird things like loading from the same address to the same register when the register's value could not possibly be changed. At the same time these instructions couldn't outright be removed or the alignment of all the JMPs would be thrown off. My friend said perhaps some NOPs might be done with a lower power consumption than some of the multiplication instructions, but otherwise if the later instructions don't use the results of the extraneous instructions and are unaffected by their flags nothing should change.
22
u/nuclear_splines PhD, Data Science 2d ago
Well, it depends on what you mean by "change anything".
If you perform a calculation and throw the results away, that should be equivalent to not performing the calculation in abstract logic land
If you read a value from memory it may be placed in cache, or could be kept around in cache for longer, even if the value isn't used. This could incidentally change the performance of future instructions
Depending on the hardware architecture, some instructions may take much longer to run than others. If the rest of the program is timing-sensitive, replacing 'unused' instructions with NOPs could upset that timing
Changing the instructions will of course change the bytes of the executable and its hash, which could theoretically impact behavior (such as copy protection checking whether the program has been altered)