“What’s your favorite assembly instruction?” No, that’s not a successful pickup line. This is, however, the kind of things I talk about with my friends. And, yes, I am a nerd. About a month ago, this topic of discussion came up. Of course, the PowerPC EIEIO came up. EIEIE stands for Enforce In-Order Execution of I/O, and is actually a necessary instruction when writing things that depend on the order of values written to memory, like device drivers.

My favorite is the BFFFO (which I pronounce BOO-foh) instruction from the Motorola 68000 family. It stands for Find First One in Bit Field. The definitive reference is the 68000 Reference manual (PDF), page 142 (or 4-42 depending on how you view it). Here’s the first line from the description:

Searches the source operand for the most significant bit that is set to a value of one.

Back in my kernel hacking days when I worked at Motorola, this instruction was quite useful in our operating system. We had our own, custom embedded operating system, and I was on the OS team. It was quite an advanced embedded OS at the time, with full memory protection and preemptive multitasking. There were 32 process priorities, and the highest priority process always ran. Within a priority, it was round-robin scheduling. Our implementation naturally consisted of 32 linked lists, one for each priority. During a context switch, we had to determine which process to run next, which means first we had to determine which linked list to look at. Since context switches are generally time critical, it should be efficient as possible. And this is where BFFFO came in. We used one byte (32 bits) as a bit field, where each bit represented a priority level. A 1 meant the priority had at least one ready process, and the linked list was non-empty. Thus we could now use BFFFO to figure out which linked list to use, all in one assembly instruction. While this, of course, is efficient on the 680x0, it posed a problem when porting. I had to write a C version of it, at least once (yes, I had to do it more than once… twice, I think).

I haven’t done much assembly hacking as of late, which is both a blessing and a little unfortunate. But as Wolf says, Don’t Fear the Assembler.