2
$\begingroup$

I've tried to find the answer through Google but I've come across conflicting uses of terminology, so I just wanted to be absolutely clear.

Here is my understanding: High level code written in a high level language (C I think is one of the lowest-level high level languages) gets translated to assembly code by that language's compiler.

That assembly code then gets translated to machine code (aka binary code aka hexadecimal code) by something (not sure what does this translation).

Does that mean that assembly code, but not machine code, is portable across computers? Or is assembly also not portable, and the only thing portable is code written in a high level language?

$\endgroup$
4
$\begingroup$

Assembler code describes instructions for one particular architecture. It is slightly helpful by allowing you to use human-readable names for instructions, names for memory addresses, doing some simple calculations etc. but it is totally useless to produce instructions for any other processor.

With heroic effort you could create a translator that produces code for a different architecture but nobody would thank you for it. The advantage of assembler code is that it lets you write code for things high level software isn’t designed for - like writing the code to boot a computer, handle interrupts, doing a process switch - and that kind of code is often very machine specific; it might not even be usable on a different computer with the same processor.

So yes, for any portability you need a high level language.

$\endgroup$
  • $\begingroup$ Thank you very much for the help! So am I correct in thinking that high level language code gets compiled to assembly (for whatever architecture the system the code was compiled on uses), which then gets compiled to binary (or apparently hexadecimal)? Thank you again for the help, appreciate it! $\endgroup$ – James Ronald Nov 3 '19 at 21:16
1
$\begingroup$

There are at least three different models for translating portable source code written in a high level language into machine code instructions that can be run on a particular type of computer and operating system(the "target platform"). Confusingly, the terms "compiler" and "compilation" are used in all of these models.

  1. The output of the compiler can be a file of machine code instructions that can be loaded into memory and run directly by a specific operating system. In this model, some portion of the compiler (usually called the "back end") will be specific to a particular target platform. To compile source code to run on a different target platform will require a different version of the compiler. So the source code is portable, but the compiler is not.
  2. The output of the compiler can be a file of source code in some lower level language, called an "intermediate representation". This intermediate representation can then be compiled to machine code that runs on a particular target platform. In this model, both source code and compiler are portable, but the high-level comiler is the first stage in a two-stage process. The reason for this two-stage approach is usually to take advantage an existing compiler infrastructure for the lower level language. For example, several high level languages create intermediate representations in C, since compilers already exist which can compile C for a wide range of target platforms.
  3. The output of the compiler can be a file of "virtual code", which is run by a "virtual machine". The virtual machine is a program that runs on the target platform and interprets and executes the virtual code. A different version of the virtual machine is required for each target platform, but the virtual code and the compiler are portable. The advantage of this approach is that the programmer does not need to worry about the differences between target platforms. An example of this approach is Java, where Java code is compiled to Java "bytecode", which is then run on the target platform by a Java virtual machine. Another example is the reference implementation of Python, called CPython.

An assembler is essentially a compiler for a type of language called "assembly language" that is so low level that it is specific to a particular CPU. So when you program in an assembly language, not even the source code is portable (or, at best, it may be portable across a particular family of CPUs, such as x86 processors).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.