The difference between n = 0 and n = n - n

When I read this question, I remembered how someone once told me (many years ago) that from the point of view of assembler, these two operations are very different:

n = 0;

n = n - n;

This is true, and if so, why is this so?

EDIT: As noted by some answers, I think it would be pretty easy for the compiler to optimize to the same thing. But what I find interesting is why they will be different if the compiler has a common approach.

+3
source share
7 answers

. " ". :

move.l #0, d0

moveq.l #0, d0

sub.l a0,a0

op-, (0). , , ( ). Sloooow.

moveq.l , op-, 0 7 . , . , ( op-. Bad.).

, , , . C,

n = n - n;

n ( ).

+3

, :

xor eax, eax

mov eax, 0

, . 1 ( 2). , - ( sub).

+10

V++ 6.0 :

4:        n = 0;
0040102F   mov         dword ptr [ebp-4],0
5:
6:        n = n - n;
00401036   mov         eax,dword ptr [ebp-4]
00401039   sub         eax,dword ptr [ebp-4]
0040103C   mov         dword ptr [ebp-4],eax
+7

.

+4

, n volatile .

+4

XORing , C.

C , , - .

+2

Source: https://habr.com/ru/post/1708404/


All Articles