转换的值可以存储在内存或寄存器中。这取决于您的硬件和编译器以及编译选项。考虑在 cygwin 64 位 gcc 上使用 g++ -O0 -c -g cast_code.cpp 编译 sn-p 的结果:
[...]
14: c7 45 fc 05 00 00 00 movl $0x5,-0x4(%rbp)
int i2 = 2;
1b: c7 45 f8 02 00 00 00 movl $0x2,-0x8(%rbp)
float f = i1/(float)i2;
22: f3 0f 2a 45 fc cvtsi2ssl -0x4(%rbp),%xmm0
27: f3 0f 2a 4d f8 cvtsi2ssl -0x8(%rbp),%xmm1
2c: f3 0f 5e c1 divss %xmm1,%xmm0
30: f3 0f 11 45 f4 movss %xmm0,-0xc(%rbp)
[...]
整数被移动到堆栈上,然后转换为存储在 mmx 寄存器中的浮点数。新对象?有争议的;在内存中:而不是(取决于什么是内存;对我来说内存应该是可寻址的)。
如果我们指示编译器正确存储变量(例如,为了避免更精确的寄存器出现精度问题),我们会得到以下结果:
g++ -O0 -c -g -ffloat-store cast_code.cpp 结果
// identical to above
14: c7 45 fc 05 00 00 00 movl $0x5,-0x4(%rbp)
int i2 = 2;
1b: c7 45 f8 02 00 00 00 movl $0x2,-0x8(%rbp)
float f = i1/(float)i2;
// same conversion
22: f3 0f 2a 45 fc cvtsi2ssl -0x4(%rbp),%xmm0
// but then the result is stored on the stack.
27: f3 0f 11 45 f4 movss %xmm0,-0xc(%rbp)
// same for the second value (which undergoes an implicit conversion).
2c: f3 0f 2a 45 f8 cvtsi2ssl -0x8(%rbp),%xmm0
31: f3 0f 11 45 f0 movss %xmm0,-0x10(%rbp)
36: f3 0f 10 45 f4 movss -0xc(%rbp),%xmm0
3b: f3 0f 5e 45 f0 divss -0x10(%rbp),%xmm0
40: f3 0f 11 45 ec movss %xmm0,-0x14(%rbp)
看到 i1 如何在 27 处从寄存器移动到内存,然后在 36 处返回到寄存器,以便在 3b 处执行除法,这有点痛苦。
无论如何,希望对您有所帮助。