In school we've been taught that compilers compile a computer program to machine language. We've also been taught that the machine language consists of direct instructions to the hardware. Then how can the same compiled program run on several computer configurations with different hardware?
3 Answers
Depends what you mean by 'different hardware' if it is the same processor (or same family eg Intel x86) then the machine code instructions are the same.
If the extra hardware is different peripherals (screens, disks printers etc) then the operating system hides those details by giving you a consistent set of instructions to drive them
If you mean, how can you run a program for an ARM cpu on an Intel x86, then you can't - except by some sort of virtual machine emulator that reads each of the ARM instructions and either translates them into x86 or runs the same functionality as a set of x86 funcs and then returns the same answer that the ARM ones would have done.
Edit: I assume you mean PCs with different hw - ie different peripherals but the same processor family?
Talking to hardware doesn't involve specific instructions as such - it's mostly a matter of moving memory to specific locations where the operating system and/or device driver have specifically reserved for data going to that device. In the old days of DOS and BIOS you would then trigger an interupt to call a specific bit of code in the BIOS to act on that data and send it to the HW.

- 94,801
- 28
- 188
- 263
-
Thanks for the concise answer. I couldn't understand how does a program connect to the API of the OS? I imagine there is an abstraction level that sits between the program and the processor and that executes some of it instructions and passes the others to the processor. And what language (or languages) are these "virtual" instructions written in? Where can I read more about that process? – Jencel May 09 '12 at 18:18
-
It's just moving memory rather than specific instructions. In the old days it was simpler - the screen was at a certain address, you wrote '65' to that address and an 'A' appeared in the topleft corner, you wrote 1 to another location and it set the color of that char to red. – Martin Beckett May 09 '12 at 19:37
-
A C program that has been compiled will be linked to libraries that are on the current machine. Even if the processor architecture is the same, if an executable from computer A gets moved to computer B, wouldn't the executable not run if computer B doesn't have the same libraries as computer A? – blipbloop May 23 '17 at 04:11
With an emulator or a virtual machine, either of which effectively translates the machine language on the fly.

- 267,707
- 33
- 569
- 680
-
-
@BobbyMarinoff: In situations where you've compiled code for one CPU architecture and are running it on a different CPU architecture, something must be doing the translation. – Oliver Charlesworth May 09 '12 at 18:11
I think it is more accurate to say that native compilers compile to a specific instruction set of a processor. Since there are families of processors that keep backwards compatibility: 8086 - 80386 - 80486 - 80586 - Dual Core - Quad Core...; then each processor runs the instructions of its ancestors. If you want to port your code across processor architectures, then you need for sure a virtual machine or emulator, like it was mentioned previously.

- 1,013
- 7
- 14