-3

Recently I have been learning how to program in C++, and was wandering, if compiler languages are translated to machine code is it possible to just simply run the code as if it was an assembly code? Or in another example I load just the compiled code onto a formatted flash drive and nothing else and plug up that flash drive into a computer with no OS on it what so ever, and boot from the flash drive to make the computer run the compiled code, and nothing else. Is something like this even possible? Is the language not supported directly by the processor or is some sort of interpreter/execution environment for the language needed to run the program?

Sorry if what im asking is a bit abstract, tbh I don't know exactly how to explain it beyond providing examples.

ccpizza
  • 28,968
  • 18
  • 162
  • 169
CORE craftX
  • 71
  • 1
  • 3
  • There is a related question [here](http://stackoverflow.com/questions/4007579/what-about-java-physical-machine) about Java instead of about C++ – SirGuy Dec 08 '15 at 17:57
  • 2
    Processors don't run assembly code either. Assembly is a textual representation of the machine code that processors DO understand in hardware (note that "machine" code might not be the actual encoding that runs, it might be translated/compiled into microcode, but that translation takes place in hardware). – Ben Voigt Dec 08 '15 at 18:00
  • 2
    Yes, it is possible. After all, every time you boot your computer this is exactly what happens. – SergeyA Dec 08 '15 at 18:02
  • You could write a C interpreter and implement it in hardware. There is little reason to. – Yakk - Adam Nevraumont Dec 08 '15 at 18:04
  • The obvious thing you did wrong is not trying it yourself before you asked for help. – Hans Passant Dec 08 '15 at 18:05

1 Answers1

1

Almost.

You will probably need some initialization before you can hand execution over to compiled C++. For example you would maybe need to initialize the stack pointer and other low level initialization that can't be done in C++.

After that you should be aware that there are some initialization that needs to be done before main is being run, but that could normally be done in C++, especially if you want a reasonable set of the features of the language (memory allocation, exception handling etc) available.

You should also be aware that much of the functionality that are taken for granted are normally handled by the operating system. Without an OS the executable would have to have libraries that handles that functionality if needed (like for example stream output functionality, file system etc).

skyking
  • 13,817
  • 1
  • 35
  • 57