Aussie AI
Assembler
-
Last Updated 20 September, 2024
-
by David Spuler, Ph.D.
Assembly language, or "assembler", is the low-level language for CPU machine instructions. Like C++, it is still a symbolic human-readable language, but unlike C++, it translates mostly one-to-one to machine code instructions. The syntax for assembler is much simpler than C++, and more obscure, but it's also very, very fast.
Most C++ compilers support features allowing you to specify assembly language sequences in the middle of a C++ program. You don't need to put assembler into a separate code file, because you can use assembly language directives inside C++ sequences. The directive to use to introduce an assembly language statement into C++ is somewhat compiler-dependent, (such as whether to use ASM or MASM or __ASM), but the whole concept of assembly language is platform-dependent anyway.
The first question to ask yourself before writing assembler in C++ is whether you need to. The use of assembler should only be considered for the most bottlenecking parts of the code, like deep inside the inner loops of a GEMM kernel. Otherwise, you're probably micro-optimizing something that's not that critical.
Another question is whether to use "intrinsics" instead of assembler. Each compiler has literally hundreds of builtin low-level functions called "intrinsics" that are very fast, probably because the compiler-writers have written them in assembler. There are also lots of intrinsics to use for GPU operations and CPU SIMD extensions such as AVX-512. Look through the long list of C++ intrinsics for your platform to see if there's one that does what you need.
Research on Assembler
Research on optimization using assembly language includes:
- Daniel Kusswurm, 2022, Modern Parallel Programming with C++ and Assembly Language: X86 SIMD Development Using AVX, AVX2, and AVX-512, 1st Edition, Apress, https://www.amazon.com/Modern-Parallel-Programming-Assembly-Language/dp/1484279174/, Code: https://github.com/Apress/modern-parallel-programming-cpp-assembly
- Microsoft, August 3rd, 2021, Inline Assembler, Visual Studio 2022 Documentation, https://learn.microsoft.com/en-us/cpp/assembler/inline/inline-assembler
- CPP Reference, Aug 2023, asm declaration, https://en.cppreference.com/w/cpp/language/asm
- Sandeep S, 01 March 2003, GCC-Inline-Assembly-HOWTO, https://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
- Félix Cloutier, 2023, x86 and amd64 instruction reference, https://www.felixcloutier.com/x86/
- David Spuler, March 2024, Chapter 16. Hardware Acceleration, Generative AI in C++: Coding Transformers and LLMs, https://www.amazon.com/dp/B0CXJKCWX9
- J. Armengol-Estapé, J. Woodruff, C. Cummins and M. F. P. O'Boyle, 2024, SLaDe: A Portable Small Language Model Decompiler for Optimized Assembly, 2024 IEEE/ACM International Symposium on Code Generation and Optimization (CGO), Edinburgh, United Kingdom, 2024, pp. 67-80, doi: 10.1109/CGO57630.2024.10444788, https://ieeexplore.ieee.org/abstract/document/10444788
- Z Zhong, January 22nd, 2024, Enhancing SIMD Assembly Language Development with Visualization Techniques, Masters Thesis, Department of Computer Science and Communications Engineering,, Master of Engineering, Waseda University, Japan, https://waseda.repo.nii.ac.jp/record/2001309/files/t5121F099.pdf
- Zeyu Gao, Hao Wang, Yuanda Wang, Chao Zhang, 10 Aug 2024, ViC: Virtual Compiler Is All You Need For Assembly Code Search, https://arxiv.org/abs/2408.06385
- David Spuler, March 2024, Assembly Language versus Intrinsics, in Generative AI in C++, https://www.aussieai.com/book/ch16-assembly-vs-intrinsics
- shikaan, 8 September, 2024, A friendly introduction to assembly for high-level programmers — Hello, https://shikaan.github.io/assembly/x86/guide/2024/09/08/x86-64-introduction-hello.html
More AI Research
Read more about: