Here's the C program, I use Visual Studio Community 2022 with MSVC compiler. What are all those mov instructions, I don't use any compiler optimization flags.
#include <stdio.h>
int main(void) {
int a = 11;
int b = 13;
a += b;
a += 15;
a += 17;
a += 23;
return 0;
}
Here's the disassembly view of the running program.
int a = 11;
00007FF75AB9175D mov dword ptr [a],0Bh
int b = 13;
00007FF75AB91764 mov dword ptr [b],0Dh
a += b;
00007FF75AB9176B mov eax,dword ptr [b]
00007FF75AB9176E mov ecx,dword ptr [a]
00007FF75AB91771 add ecx,eax
00007FF75AB91773 mov eax,ecx
00007FF75AB91775 mov dword ptr [a],eax
a += 15;
00007FF75AB91778 mov eax,dword ptr [a]
00007FF75AB9177B add eax,0Fh
00007FF75AB9177E mov dword ptr [a],eax
a += 17;
00007FF75AB91781 mov eax,dword ptr [a]
00007FF75AB91784 add eax,11h
00007FF75AB91787 mov dword ptr [a],eax
a += 23;
00007FF75AB9178A mov eax,dword ptr [a]
00007FF75AB9178D add eax,17h
00007FF75AB91790 mov dword ptr [a],eax
a += b
it gets more confused with an extramov
.return 0;
. So zero instructions per addition...a
after each addition, the optimizer will likely do all the additions at compile time and just print the constant values. To avoid this, you should get the initial values of a and b from user input.int foo(int a, int b){ return a+b; }
should compile to one instruction plus aret
(with optimization enabled). Or two if building legacy 32-bit code where the calling convention passes args in stack memory instead of registers.