Traditionally, 64-bit computing is a mixed bag. You gain additional precision and the ability to address more memory, but in many cases those advantages are outweighed by the fact that the 64-bit code requires more memory and cache and the actual individual operations are slower.
X86-64 is somewhat different in this respect, because when AMD took it upon themselves to add 64-bit capabilities to x86, they also made other enhancements including, notably, doubling the number of general purpose registers from a paltry 8 to a more manageable 16. The caveat is that only 64-bit code can have access to these new registers.
Finding a list of prime numbers is one of those benchmarks that’s pretty easy to code in any language. The trial division method won’t win any prizes for speed or creativity, but it’s a decent test of some basic hardware operations. Having written just such a prime checking programming in C, I was anxious to see whether a hand-coded assembly version would be any faster.
The short answer is that in my implementation, the 64-bit assembly version ran modestly faster on my machine (64-bit Intel Core i7) than the C code compiled in 64-bit mode with full optimizations. Note that this program simply spits out the total number of primes between 2 and MAX, not the actually primes themselves (often a rather long list).
x86 assembly– written for nasm, should run on Linux and MacOS X
C code – should run anywhere
Here’s a simple example demonstrating some of the major syntax differences between Microsoft’s assembler and the free nasm assembler. It mainly implements the suggestions here.
Nasm is a cross-platform x86 assembler and functions largely the same on MacOS X as it does on Linux. However as I discovered porting some of my code, there are some quirks and slight changes required, compared to working with nasm in Linux.
Kip Irvine’s x86 assembly book is a good introduction to assembly. However, it requires using Visual Studio on Windows, both for the syntax and more importantly for the Irvine32 support library that the examples require. Happily, some folks have put together a Linux port of that library called Along32 and so using the crossplatform nasm assembler, one can make nearly full use of the book on Linux.
Below are the basic instructions for using nasm and this library.
While 64-bit x86 processors have now been on the market for more than 5 years, software support is only slowly catching on. 64-bit x86, or x86-64 as its inventors at AMD called it, not only offers programmers the ability to manipulate and address data in larger chunks, but added some other niceties like an additional 8 general purpose registers.
Transitioning assembly code from x86 to x86-64 is pretty straightforward, but there are some changes worth noting.
Masm, the Microsoft assembler, is the most commonly taught x86 assembler. Unfortunately, its use is limited to Windows. nasm is a free cross-platform x86 assembler which supports all the common x86 operating systems – Linux, MacOS X and Windows. Unlike the GNU assembler, it uses the same Intel syntax that masm does. Still, there are some differences.
What follow are my notes on converting x86 assembly code from use with mas to nasm.
I have also posted a simple example highlighting some of those changes.