do you have any explanation of why my by value of var1 is being transformed into a register of DWORD size before being passed into my std::cout function? is it just because it's more efficient?  I will note i have optimization's turned off


#include <iostream>

int main()
{
char Var1 = 'A';

std::cout << Var1;

getchar();
getchar();

return 0;
}



_main PROC ; COMDAT

; 5    : {

push ebp
mov ebp, esp
push ecx

; 6    : char Var1 = 'A';

mov BYTE PTR _Var1$, 65 ; 00000041H

; 7    :
; 8    : std::cout << Var1;

movzx eax, BYTE PTR _Var1$
push eax
mov ecx, DWORD PTR __imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A
push ecx
call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@D@Z ; std::operator<<<std::char_traits<char> >
add esp, 8

; 9    :
; 10  : getchar();

call DWORD PTR __imp__getchar

; 11  : getchar();

call DWORD PTR __imp__getchar

; 12  :
; 13  : return 0;

xor eax, eax

; 14  : }

mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END

Posted on 2011-07-14 14:57:42 by dougfunny
Don't ask performance questions about code compiled without optimizations on :)
Posted on 2011-07-14 15:27:52 by f0dder
Well, it might be a little oversimplified but think of it this way. You have your computer running a 32-bit program so it expects to use 32-bit registers. If you start throwing instructions that use 8-bit size register, the processor pipeline have to do new accommodations. Therefore, there will be stalls a little bit performance-wise. So it would be better to throw your 8-bit value into a 32-bit register and work in it instead.

movzx means move a value into a register of larger size with extended zeros. You can't just move a 8-bit value into a larger size register because your value can be signed or unsigned. If your value is unsigned you use movzx otherwize you have to use movsx which is move sign extended.
Posted on 2011-07-19 10:39:13 by banzemanga
In C, a char is always treated as an int when it is pushed onto the stack on order to preserve stack alignment.
Posted on 2011-07-19 12:13:21 by Synfire