user11224591
user11224591

Reputation:

Isn't that C's assembly the same concept as C#'s IL or Java's bytecode

We know that high level programminhg language like C# or Java use virtual machine and the source codes are compiled into Intermediate Language(C#'s IL or Java's bytecode), so that those program can write once, run anywhere.

But isn't that low level programming language C has the same concept "assembly" which the souce codes are compiled to. So C can also write once, run anywhere, so what are the things that high level programminhg language bring?

Upvotes: 0

Views: 172

Answers (2)

user14644949
user14644949

Reputation: 321

No.

The assembly language emitted by a C compiler is specific to a given machine architecture and a given operating system.

Change either of those things and your assembly language cannot be used.

Upvotes: 1

Jeremy Lakeman
Jeremy Lakeman

Reputation: 11153

No.

Platforms like dot net and Java, specify the exact semantics of each possible instruction. The runtime may then translate a sequence of instructions, to a sequence of assembly with exactly the same semantics.

C is a very different beast. The exact semantics of your C program depend on the C headers, which were provided by your OS vendor. And the rules for how each instruction maps to CPU instructions and registers, also influenced by the OS vendor.

And then there's the wiggle room of undefined behaviour. Parts of the C language definition where the semantics of an instruction are deliberately left up to the compiler to define (or not).

While some compilers will use an intermediate form when translating from source code to machine code. Some target specific decisions may have already been made while parsing system headers. Or by the rules for how argument passing and return values map to registers or the stack.

Of course you could define a fake target OS / CPU, partially compile your program to it, then build a runtime that can map those semantics to your actual OS / CPU. This is exactly the approach used by Chrome's experimental portable native client. But that discards some of the strengths of C.

Upvotes: 1

Related Questions