C++ to Assembly
A simple code:
compile it and check its assembly:
ebp saves current function (main function in this code) stack base address. When call another function, the process needs to use that function’s stack space. So it will save current stack base address and assign esp to ebp. use [ebp +??] to access function’s aguments and use [ebp - ??] to access function’s variable.
1 2 3 4 5 6 7 8
sub esp, 0C0h will allocate a stack space,
1 2 3
save these values.
1 2 3 4
rep is repeat these instructions. ECX is the time of these actions and assign these value as ccccc…, since this is debug version.
0x1 Data Type
Visual Studio 2015 assembly code:
movss: , Move Scalar Single-Precision Floating-Point Values.
cvtsi2ss: Convert one signed doubleword integer from r/m32 to one single-precision floating-point number in xmm.
cvtss2sd: Convert Scalar Single-Precision Floating-Point Value to Scalar Double-Precision Floating-Point Value
cvttss2si: Convert with Truncation Scalar Single-Precision Floating-Point Value to Integer
VS 2015 assembly:
wchar_t is intended for representing text in fixed-width, multi-byte encodings; since wchar_t is usually 2 bytes in size it can be used to represent text in any 2-byte encoding. It can also be used for representing text in variable-width multi-byte encodings of which the most common is UTF-16.
search memory window
VS 2015 assembly:
Pointer is 32-bits, thats why its type is dword ptr. But in the above, since data type is different, it uses dword ptr, byte ptr and word ptr.
In C++, pointer can only use add and sub. (because it stands for address).
different types of pointer add 1 depends on its type:
Here nVarType saves nVar’s address
check Add function:
eax saves nVar’s address.
compiler will replace NUMBER_ONE to number 10:
Looks like compiler did some work.
If we change the compiler options:
The final result will be much clear:
compiler will try to use shift instruction if the argument is 2 4 8 etc. Otherwise it will use mul or imul
If there is a mixture of add and mul, compiler will use LEA instruction:
cdq copies the sign (bit 31) of the value in the EAX register into every bit position in the EDX register.