Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an executable to run on a certain processor architecture (instead of certain OS)?

So I take my C++ program in Visual studio, compile, and it'll spit out a nice little EXE file. But EXEs will only run on windows, and I hear a lot about how C/C++ compiles into assembly language, which is runs directly on a processor. The EXE runs with the help of windows, or I could have a program that makes an executable that runs on a mac. But aren't I compiling C++ code into assembly language, which is processor specific?

My Insights:

  1. I'm guessing I'm probably not. I know there's an Intel C++ compiler, so would it make processor-specific assembly code? EXEs run on windows, so they advantage of tons of things already set up, from graphics packages to the massive .NET framework. A processor-specific executable would be literally starting from scratch, with just the instruction set of the processor.

  2. Would this executable be a file-type? We could be running windows and open it, but then would control switch to processor only? I assume this executable would be something like an operating system, in that it would have to be run before anything else was booted up, and have only the processor instruction set to "use".

like image 386
Gordon Gustafson Avatar asked Aug 28 '09 01:08

Gordon Gustafson


People also ask

How do I create an executable source code?

A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file). Another specialized program, called a linker, combines this object file with other previously compiled object files (in particular run-time modules) to create an executable file.

How executable files are created?

When a user or other event triggers an executable file, the computer runs the code that the file contains. Executable files contain binary machine code that has been compiled from source code. This low-level code instructs a computer's central processing unit on how to run a program.


1 Answers

Let's think about what "run" means...

Something has to load the binary codes into memory. That's an OS feature. The .EXE or binary executable file or bundle or whatever, is formatted in a very OS-specific way so that the OS can load it into memory.

Something has to turn control over to those binary codes. There's the OS, again.

The I/O routines (in C++, but this is true in most places) are just a library that encapsulate OS API's. Drat that OS, it's everywhere.

Reminiscing.

In the olden days (yes, I'm this old) I worked on machines that didn't have OS's. We also didn't have C.

We wrote machine codes using tools like "assemblers" and "linkers" to create big binary images that we could load into the machine. We had to load these binary images through a painful bootstrap process.

We'd use front panel keys to load enough code into memory to read a handy device like a punched paper-tape reader. This would load a small piece of fairly standard boot linking loader software. (We used mylar tape so it wouldn't wear out.)

Then, when we had this linking loader in memory, we could feed the tape we'd prepared earlier with the assembler.

We wrote our own device drivers. Or we used library routines that were in source form, punched on paper tapes.

A "patch" was actually patched pieces of paper tape. Plus, since there were also little bugs, we'd have to adjust the memory image based on hand-written instructions -- patches that hadn't been put into the tape.

Later, we had simple OS's that had simple API's, simple device drivers, and a few utilities like a "file system", an "editor" and a "compiler". It was for a language called Jovial, but we also used Fortran sometimes.

We had to solder serial interface boards so we could plug in a device. We had to write device drivers.

Bottom Line.

You can easily write C++ programs that don't require an OS.

  1. Learn about the hardware BIOS (or BIOS-like) facilities that are part of your processor's chipset. Most modern hardware has a simple OS wired into ROM that does power-on self-test (POST), loads a few simple drivers, and locates boot blocks.

  2. Learn how to write your own boot block. That is the first proper "software" thing that's loaded after POST. This isn't all that hard. You can use various partitioning tools to force your boot block program onto a disk and you'll have complete control over the hardware. No OS.

  3. Learn how GRUB, LILO or BootCamp launch an OS. It's not complicated. Once they're booted, they can load your program and you're off and running. This is slightly simpler because you create the kind of partition that a boot loader wants to load. Base yours on the Linux kernel and you'll be happier. Don't try to figure out how Windows boots -- it's too complicated.

  4. Read up on ELF. http://en.wikipedia.org/wiki/Executable_and_Linkable_Format

  5. Learn how device drivers are written. If you don't use an OS, you'll need to write device drivers.

like image 187
S.Lott Avatar answered Nov 09 '22 07:11

S.Lott