Know your assembly, part 2

It’s interesting to examine code generated by C++ compilers, if only for nostalgia value. It turns out that compiler (well, MSVC) is pretty smart usually and employs same tricks we used when squeezing cycles out of software renderers years ago. For example it’ll almost always use _xor reg, reg _instead of mov reg, 0. Eons ago, on 8086 it was one cycle faster IIRC (sadly, cant find great Agner Fog’s instruction sheet anymore). Nowadays it still pays off because _xor _instruction is only 2 bytes as opposed to 5 for 32-bit mov. What’s more, compiler’ll also abuse _lea _instruction where possible, to optimize multiplications:

int Foo(int k)
{
    return k * 5;
}

results in

mov     eax, DWORD PTR _k$[esp-4]
lea     eax, DWORD PTR [eax+eax*4]

Old comments

DK 2008-06-06 20:46:12

From some point on Intel stated in their CPU programming manual that xor was official “zero”-ing instruction. Still manual alignment and handmade SSE can sometimes help a lot 8)

ed 2008-03-26 21:32:29

Yeah, compiler can be pretty good novadays. Also this code
bool is_true = (value == 10) ? true:false;
will compile to
cmp eax,10
setc cl
without conditional jump. Pretty usefull for comparisions like “is point inside a box” especially when combined with SSE.

More Reading
Older// Breakpoint 2008