Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How un-portable is assembly language, /really/?

I understand that writing anything in assembly, or adding assembly to any program harms its portability. But, how bad? I mean, basically all PC's are x86 or x64 these days, right? So, if I embed assembly into a C program, why wouldn't it still compile no matter where it went?

Does this notion of un-portability just refer to when you really dig in to the specific quirks of a specific processor, to squeeze out every drop of performance from a piece of code?

The PC game "Roller Coaster Tycoon" was written almost entirely in assembly language if I remember correctly. So... How un-portably could it really be?

like image 813
Carson Myers Avatar asked Sep 18 '09 06:09

Carson Myers


People also ask

Is assembly language is portable or not?

Is Assembly Language Portable? o A language whose source programs can be compiled and run on a wide variety of computer systems is said to be portable. C++ programs, for example. o Assembly language is not portable because it is designed for a specific processor family.

What is portable assembly language?

A portable assembly language provides access to machine-level features like memory addresses, ma- chine words, code addresses, and modulo arith- metics, like assembly language, but abstracts away differences between architectures like the assembly language syntax, instruction encoding, register set size, and addressing ...

Why is C more portable than assembly?

The code which was written in c could be easily reused on a different platform, beside it Assembly does not provide the portability and source code specific to a processor because assembly instruction depends on the processor architecture. Software which has written in assembly perform well as compared to C.

How is assembly language written?

A program written in assembly language consists of a series of mnemonic processor instructions and meta-statements (known variously as declarative operations, directives, pseudo-instructions, pseudo-operations and pseudo-ops), comments and data.


2 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 (say: API) functions? Which registers may be clobbered by the callee? Which are guaranteed to be preserved for the caller? How to do a system call? What's the memory layout prepared for you by the OS upon process start?

like image 111
Dirk Avatar answered Oct 25 '22 01:10

Dirk


Porting assembly, there is also the problem of the ABI, that varies from OS to OS. Porting a C program from Unix to Windows (or even from Linux to OpenBSD) may be a straightforward recompilation, but for an assembly program, you may find that some callee-save registers become caller-save, or that the floating-point parameters are passed differently.

And this is not only theoretical, viz. register r2 of the PowerPC versions of Linux and Mac OS X. In practice the problem may not be too bad, for instance AMD published a "recommended" ABI at the same time as its 64-bit instruction set.

like image 34
Pascal Cuoq Avatar answered Oct 25 '22 02:10

Pascal Cuoq