When to Use Assembly Code
- Although asm statements can be abused, they allow your programs to access the computer hardware directly, and they can produce programs that execute quickly.
- You can use them when writing operating system code that directly needs to interact with hardware.
- For example, /usr/include/asm/io.h contains assembly instructions to access input/output ports directly.
- The Linux source code file /usr/src/linux/arch/i386/kernel/process.c provides another example, using hlt in idle loop code.
- You should use inline assembly to speed up code only as a last resort. Current compilers are quite sophisticated and know a lot about the details of the processors for which they generate code.
- Unless you understand the instruction set and scheduling attributes of your target processor very well, you're probably better off letting the compiler's optimizers generate assembly code for you for most operations.
- Occasionally, one or two assembly instructions can replace several lines of higher level language code.
- For example, determining the position of the most significant nonzero bit of a nonzero integer using the C programming languages requires a loop or floating-point computations.
- Many architectures, including the x86, have a single assembly instruction (bsr) to compute this bit position.
2006-04-27