Question

I know that exe file contains pure CPU instruction plus extra piece of data. So if I begin running a simple hello world console app or a 32 bit GUI app (exe file) then the OS will load the instruction given in the exe file into memory to get processed by CPU. So if I run that app, it should only follow instruction as it is, that is to display hello world only (in a complete blank screen with only the word 'hello world'). But it is not happening so. It is somewhat controlled by OS to display in a windowed environment of command prompt. So what is actually happening there.

edit: To be precise my question is that i want to know what are all the instruction that an exe(a simple 16bit dos app in windows OS) file contains(considering the confusion above I have)?

Was it helpful?

Solution

If you compile a program on a particular OS, that it is that OS that you should run it on. For example, if you compile a command line program for Windows, it will work on Windows, but it will not work on Ubuntu. You said yourself that the content of the executable file differs from OS to OS.

What you can do is to use an emulator for a particular system on another system. For example, if you are on Linux, you can use Wine to run Windows programs. In this way you emulate the Windows environment for your program even though it is on Linux.

Of course, the CPU should follow the instructions as they are. If you, e.g., want to print a hello world line, than your program contains code for that. But not just for that. It contains the code for other stuff that are OS dependent, and this is where the problem is. For example, your Windows compiled program might use some Windows API to perform the printing, and this API is not to be found on Linux. It is than the called API, and not your program that directly performs the output.

As Jongware mentioned it in the comment below, what you could do is to cross-compile your code. In that case you can compile your code on Windows so that it can run on Linux, but only if you compile it with the libraries needed for the specific Linux. In that case, however, you will not be able to run you code on Windows.

OTHER TIPS

By default, software are compiled for the same type of machine that you are using. However, you can also install a cross-compiler, to compile for some other type of machine.

When you develop a desktop or server application, almost always the development platform (the machine that runs your compiler) and the target platform (the machine that runs your application) are the same. By "platform" I mean the combination of CPU architecture, and Operating System. The process of building executable binaries on one machine, and run them on another machine when the CPU architecture or the Operating System are different is called "cross compilation". A special compiler is needed for doing cross compilation that is called "cross compiler", and sometimes just "toolchain".

have a look here and here.

So if I run that app, it should only follow instruction as it is, that is to display hello world only (in a complete blank screen with only the word 'hello world').

Why "complete blank screen"? It completely depends on how your application is designed:

  • In a console application, it will output the string to stdout, which is then displayed inside the console you started the program out of. If you started it directly via a shortcut or by double-clicking the .exe file, it will open a new console window.
  • In a windowed application, it will output the string wherever you defined it to be output.

Alas, your question is by far too vague to become more concrete here.

But it is not happening so. It is somewhat controlled by OS to display in a windowed environment of command prompt.

What does that mean?

If it means what I think, i. e. you indeed get a black window with the string output, well, it is exactly how it is supposed to work, if it is a console application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top