Home > Enterprise >  Low level languages and their dependencies
Low level languages and their dependencies

Time:09-24

I am trying to understand exactly what it means that low-level languages are machine-dependent.

Let's take for example C, well if it is machine-dependent does it mean that if it was compiled on one computer it might not be able to run on another?

CodePudding user response:

Machine-dependent means the program only works on specific hardwares. The best example I can give is Assembly. Let's consider your example, C is machine-independent language. You can execute the same program on different systems.

CodePudding user response:

High-Level languages are portable, meaning every architecture can run high-level programs but, compared to low-level programs (like written in Assembly or even machine code), they are less efficient and consume more memory.

Low-level programs are known as "closer to the hardware" and so they are optimized for a certain type of hardware architecture/processor, being faster programs, but relatively machine-dependant or not-very-portable. So, a program compiled for a type of processor it's not valid for other types; it needs to be recompiled.

CodePudding user response:

In the before

When the first processors came out, there was no programming language whatsoever, you had a very long and very complicated documentation with a list of "opcodes": the code you had to put into memory for a given operation to be executed in your processor. To create a program, you had to put a long string of number in memory, and hope everything worked as documented.

Later came Assembly languages. The point wasn't really to make algorithms easier to implement or to make the program readable by any human without any experience on the specific processor model you were working with, it was created to save you from spending days and days looking up things in a documentation. For this reason, there isn't "an assembly language" but thousands of them, one per instruction set (which, at the time, basically meant one per CPU model)

At this point in time, all languages were platform-dependent. If you decided to switch CPUs, you'd have to rewrite a significant portion (if not all) of your code. Recognizing that as a bit of a problem, someone created a the first platform-independent language (according to this SE question it was FORTRAN in 1954) that could be compiled to run on any CPU architecture as long as someone made a compiler for it.

Fast forward a bit and C was invented. C is a platform-independent programming language, in the sense that any C program (as long as it conforms with the standard) can be compiled to run on any CPU (as long as this CPU has a C compiler). Once a C program has been compiled, the resulting file is a platform-dependent binary and will only be able to run on the architecture it was compiled for.

C is platform-dependent

There's an issue though: a processor is more than just a list of opcodes. Most processors have hardware control devices like watchdogs or timers that can be completely different from one architecture to another, even the way to talk to other devices can change completely. As such, if you want to actually run a program on a CPU, you have to include things that make it platform-dependent.

A real life example of this is the Linux kernel. The majority of the kernel is written in C but there's still around 1% written in different kinds of assembly. This assembly is required to do things such as initialize the CPU or use timers. Using this hack means Linux can run on your desktop x86_64 CPU, your ARM Android phone or a RISCV SoC but adding any new architecture isn't as simple as just "compile it with your architecture's compiler".

So... Did I just say the only way to run a platform-independent on an actual processor is to use platform-dependent code? Yes, for most architectures, you have to.

Or is it?

But there's a catch! That's only true if you want to run you code on bare metal (meaning: without an OS). One of the great things of using an OS is how abstracted everything is: you don't need to know how the kernel initializes the CPU, nor do you need to know how it gets its clock, you just need to know how to access those abstracted resources.

But the way of accessing resources dependent on the OS, aren't we back to square one? We could be, if not for the standard library! This library is used to access functions like printf in a defined way. It doesn't matter if you're working on a Linux running on PowerPC or on an ARM Windows, printf will always print things on the standard output the same way.

If you write standard C using only the standard library (and intend for your program to run in an OS) C is completely platform-independent!

CodePudding user response:

In the end processors executes machine code which is basicly a collection of binary numbers. The processor decode each binary number to figure out what it is supposed to do. One binary number could mean "Add register X to register Y and store the result in register Z". Another binary number could mean "Store the content of register X into the memory address held by register Y". And so on...

The complete description of these decoding rules (i.e. binary number into operation) represents the processors instruction set (aka ISA).

A low level language is a language where the code you can write maps very closely to the specific processors instruction set. Assembly is one obvious example. Since different processor may have different instruction sets, it's clear that an assembly program written for one processors ISA can't be used on a processor weth a different ISA.

Let's take for example C, well if it is machine-dependent does it mean that if it was compiled on one computer it might not be able to run on another?

Correct. A program compiled for one processor (family) can't run on another processor with (completely) different ISA. The program needs to be recompiled.

Also notice that the target OS also plays a role. If you use the same processor but use different OS you'll also need to recompile.

There are at least 3 different kind of languages.

  1. A languages that is so close to the target systems ISA that the source code can only be used on that specific target. Example: Assembly

  2. A language that allows you to write code that can be used on many different targets using a target specific compilation. Example: C

  3. A language that allows you to write code that can be used on many different targets without a target specific compilation. These still require some kind of target specific runtime environment to be installed. Example: Java.

  • Related