abless wrote:
Actually, both ways require the same time to enable Protected Mode:
shr register, 1 -> needs 2 clocks
shl register, 1 -> needs 2 clocks
TOTAL = 4 clocks
and reg, value -> needs 4 clocks
TOTAL = 4 clocks
Thank you Abless, these are the sort of figures I was going to look up, though you don't mention which version of the processor those clock cycles are for.
Also, the instruction [tt]AND AL, imm8[/tt] is a special case, and, at least on older CPUs, is faster than the general [tt]AND reg, imm8[/tt]. Similarly, [tt]SH[[R|L]] reg, 1[/tt], is also faster than the more general [tt]SH[[R|L]] reg, n[/tt] case. In both instances, the special cases are in fact different opcodes from the general cases.
Just to give an example of how these things vary, though, I see that my battered copy of the
80386 Programmer's Manual lists 2 cycles for [tt]AND AL, imm8[/tt], versus 3 cycles apiece for the register shifts. While the difference is negligible, there's no question that what is faster on one processor is not necessarily faster on another.
Also, as Abrash points out, the 'official' cycles are ideal figures, and don't take into account memory cycles, cache misses, pipeline misses, etc. In this case, I would expect that, since both the single immediate mode AND and the sequential SHL/SHR pair take up a total of two bytes total (a one byte opcode and a byte argument inline to the instruction sequence vs. two one byte opcodes), these factor will probably affect both approaches more or less equally, though I personally don't know enough to tell for certain.
In the end, only empirical results - i.e., testing it with a zen timer or a profiler, or both - will actually tell you which is faster, and you'd need to test it on multiple machines and configurations to be sure. Since this is a piece of code that is unlikely to be run very frequently (unless you are running a lot of legacy real-mode programs, changing the video settings very, very often, or using BIOS routines for basic system functions), the effort involved is hardly worth bothering with. Knowing when to optimize is at least as important as knowing how to; if the work spent on optimizing a given piece of code is unlikely to have noticable resutls and would be better spent on other things, then leave it unoptimized.