How not portable assembly language, / really /?

I understand that writing something in an assembly or adding an assembly to any program harms its portability. But how bad? I mean, basically all PCs are x86 or x64 these days, right? So, if I insert an assembly into a C program, why shouldn't she compile no matter where she went?

Does this concept mean not portability when you really delve into the specific features of a particular processor in order to squeeze every drop of performance from a piece of code?

The PC game "Mag. Convertible" was written almost entirely in assembler, if I remember correctly. So ... How inconvenient could it be?

+6
assembly x86 portability 64bit
source share
4 answers

Besides the processor itself, there are, of course, always other considerations: what are the calling conventions on your target platform? How are struct values ​​passed to other functions (say: an API)? What registers can be knocked down by a cathedral? Which is guaranteed to be saved for the caller? How to make a system call? What memory layout did the OS prepare for you when starting the process?

+16
source share

Porting the assembly, there is also an ABI problem that varies from OS to OS. Porting a C program from Unix to Windows (or even from Linux to OpenBSD) can be a simple recompilation, but for a build program, you may find that some called caller save registers become calling or save floating point options differently.

And this is not only theoretical, namely. register r2 versions of PowerPC Linux and Mac OS X. In practice, the problem may not be so bad, for example, AMD published the “recommended” ABI along with its 64-bit instruction set.

+11
source share

If you consider "PC == Windows", then adding assembler to a C program doesn’t do much harm. If you enter the Unix world, you will have many different processors: PPC on PS3 or XBox, old Macs and many powerful servers. For many small devices you will have an ARM. Embedded devices (which account for the vast majority of installed processors today) usually use their own custom processor with a special set of instructions.

Thus, although many PCs today will be able to run Intel code, which accounts for only a small fraction of all processors.

However, x86 code is not always the same. There are two main reasons for assembly code: you need to access special functions (for example, interrupt registers) or you want to optimize the code. In the first case, the code is quite portable. In the latter case, each processor is slightly different. Some of them have SSE . But SSEs soon replaced SSE2, which was replaced by SSE3 and SSE4. AMD has its own brand. Coming Soon AVX. At the level of the operation code, each of them has slightly different time frames for different versions of the processors.

To aggravate the situation, some operation codes have errors that are fixed in certain processor steppings. In addition, some opcodes work much faster on some processor versions than on others.

Then you will need to associate this assembly code with part C. This usually means that you either need to deal with ABI problems.

So you can see that it can become arbitrarily complex.

+9
source share

the assembly writes instructions directly for a specific processor, which means that if x86 lives forever, your code will be somehow portable.

But even now, the pen processor is returning (i.e. the next generation netbook), and I'm sure that the processor will not change next year.

I would say that assembly language for design is not portable.

+3
source share

All Articles