0
1. cout<<34+56; 2. int a; a=34+56; cout<<a;
So is the 1 statement take memory in data block as like the 2 statement take
4 Answers
+ 4
That's right. In case of full optimization both emitted asm codes are just reduced to a simple push (push 5Ah) and ostream call without any additional back and forth to/from memory. Thanks ŠŠøŠŗŠ¾Š»Š° Š¤ŠµŠ“Š¾ŃŃŃŠ² for pointing out.
+ 3
The first statement is just doing the addition on the fly in CPU's arithmetic unit without involving any intermediate loading/fetching operation to/from memory.
In Assembly level the result of addition 5Ah (90) is pushed into the stack, then ostream routine gets called as
push 5Ah
mov ecx, dword ptr [__imp_std::cout (address)]
...
The second one stores the addition result to the memory first, then the value gets fetched to CPU accumulator register (eax), after which, the eax's content gets pushed into the stack, and finally the ostream routine gets called to accomplish the job.
mov dword ptr [ebp-18h], 5Ah // Load integer 90 to the memory address indicated by ebp-18h (a=34+56)
// Load integer 90 from memory to CPU accumulator
mov eax, dword ptr [ebp-18h]
// push the 90 from eax to the top of the stack
push eax
// Begining of the ostream routine call
mov ecx, dword ptr [__imp_std::cout (address)]
...
+ 1
in case of optimizer turned on and there's no more usage of "a", the asm output will be the same for both variants.
0
No , it will only perform addition operation and print it to screen.
Hope this helpsāŗļøāŗļø.