1. cout<<34+56; 2. int a; a=34+56; cout<<a; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

13th Jul 2018, 7:56 AM
Megha Kannaujia
Megha Kannaujia - avatar
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.
13th Jul 2018, 10:00 AM
Babak
Babak - avatar
+ 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)] ...
13th Jul 2018, 9:06 AM
Babak
Babak - avatar
+ 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.
13th Jul 2018, 9:34 AM
Микола Федосєєв
Микола Федосєєв - avatar
0
No , it will only perform addition operation and print it to screen. Hope this helps☺️☺️.
13th Jul 2018, 8:01 AM
Meet Mehta
Meet Mehta - avatar