Reputation: 5333
I know that assembly language is typically not cross-platform. And even with things like NASM you would still need different code for different architectures and platforms which have different runtimes and implementations of things such as interrupts. But if someone wanted to program in assembly language because they liked it, is there any implementation of a cross-platform cross-architecture assembly language?
Edit:
What about not assembly in the traditional sense, but a low-level programming language which looks a lot like assembly?
Upvotes: 9
Views: 9548
Reputation: 1087
You may be looking for C. C is too simple: any attempts to build another cross platform assembly language might end up to another C.
Chips, CPUs are extremely diverse: different numbers of registers, different conventions, different instructions. One chip may get into the door with its left foot first, and other with its right foot first, and their standards or manuals usually as thick as a dictionary. One has to abstract all these up to write cross-platform code.
C, Lisp, Java-C# bytecode and LLVM are all good abstractions, but I am only mentioning C here due to the availability. It is probably that on platforms that the latter three ones are well supported, the first one, C, is also well supported and usually supported much better, here I mean much complete and diverse testing, toolchain and documentation availability etc. However in my memory it is proved that in most of time it is also good to choose the latter three, especially in this modern age where the 'explicit-memory' property of C is well critized and in many environment unacceptable.
Upvotes: 2
Reputation: 10727
LLVM is a low-level language (whose purpose is a compiler backend) that looks a lot like AT&T assembly, if not 10x worse. Here's an example:
define i32 @add_sub(i32 %x, i32 %y, i32 %z) {
entry:
%tmp = add i32 %x, %y
%tmp2 = sub i32 %tmp, %z
ret i32 %tmp2
}
This is roughly equilavent with the following hand-written x86 assembly:
; Body
mov eax, edi
add eax, esi
sub eax, edx
ret
LLVM llc 3.3 generates the following code (indented differently for readability):
.file "add_sub.ll"
.text
.globl add_sub
.align 16, 0x90
.type add_sub,@function
add_sub: # @add_sub
.cfi_startproc
# BB#0: # %entry
lea EAX, DWORD PTR [RDI + RSI]
sub EAX, EDX
ret
.Ltmp0:
.size add_sub, .Ltmp0-add_sub
.cfi_endproc
.section ".note.GNU-stack","",@progbits
The relevant code is this:
lea EAX, DWORD PTR [RDI + RSI]
sub EAX, EDX
ret
As you can see, LLVM has a very powerful optimizer. It is probably the closest that you're going to get.
Upvotes: 11
Reputation: 25863
As you said, assembly is not cross-platform (you can remove the "typically" part). I don't have much information myself, but looks like C-- might interest you as a "portable assembly language" as described in their page.
Upvotes: 1
Reputation: 71506
EDIT:
Assembly language generally has a one to one relation ship with the machine code, or instruction set for that processor/system. Systems are different, by definition, because they have different instruction sets. So, by definition, you cannot have a cross system instruction set, so no cross system assembly language and have it still be called assembly language.
The closest you will find are virtual instruction sets if you will that are close to machine level in that they have properties that are common to many instruction sets, a one to one or one to few relationship with machine code, but not specific to one machine in particular. For example java bytecode, python bytecode, pascal p-code, etc. These are stack based machines, most processors have stacks or can easily implement a stack based machine using loads and stores. Stack based machines use few registers, another way to get cross system and not make it too painful to implement on the various instruction sets. Stack based is at the heart of the small-c backend as well which is why it was so easily ported from one system to another. History repeats itself, these four languages are not the last languages that will boil down to a stack based machine, this will happen again and again.
If you like assembly you may find the java or python backends interesting and perhaps fun. They probably do not have an assembly language just machine code so you would probably need to write your own assembler. Personally I would start with a disassembler to get a feel for the language, then go the other way and write bytecode or create an assembler. Equally fun might be to implement the virtual machine for a particular processor.
Your comments about liking assembly language and then using the word NASM implies x86. x86 is a somewhat unpleasant assembly language, if you have not experienced others there are a number of more beautiful assembly languages out there. You should try them instead of looking for one size fits all (which you wont actually find).
Upvotes: 1
Reputation: 62048
I think Donald Knuth's MMIX is what you may be interested in. Knuth writes programs in his The Art of Computer Programming book in this machine/assembly language. To date no CPU supports it directly. There are only emulators. Oh, someone made an FPGA that can run it. But that's about it.
Upvotes: 7
Reputation: 552
The following link seems to be relevant. Why is programming in bytecode not as popular or prevalent as programming in assembly?
Especially ikegami's answer:
Artificial assembly languages are designed to service the need of the language for which they are designed, and have a very close relationship with them rather than hardware. There's not much than can be done with them that can't just as easily be achieved in the higher-level language.<
Upvotes: 2
Reputation: 18917
No. The C language might be what comes closest to a cross-platform lower-level language.
Upvotes: 5