Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question for Programmers and the Like
#9
Hazzy Wrote:I have often wondered the same thing as the OP, at least how I am reading it, but I find these answers kind of vague. They touch on the general concepts of binary and on/off or 1/0.

How does a compiler translate "int i = 0; while (true) { i++; }" into an infinite process of summation? "A compiler does it" does not suffice for me.

A compiler is a program, and it compiles written code into a program. That creates a loop, so how did the first compiler exist? How do you go from basic binary circuity akin to Minecraft's Red dust to the complex systems used everyday by billions?

I suppose, ultimately, what I want to know is this: How could a person manually compile a program? I realise it would be incredibly complex for most programs and there is no reason to do it other than knowing for the sake of knowing.
Clearly, someone, at some point in history, had to write an assembler in binary. An assembler is a "compiler" for assembly language, which is basically a human-readable language that translates directly into binrary, unlike C or C++ where there's more than one way to translate the code into binrary. They would do it by looking at a manual of the instruction set for the CPU and seeing how to encode an ADD instruction into binary, a MOVE instruction, etc and manually putting together the bytes of binary code.

This doesn't need to be done for every new kind of processor or operating system. Once you have a compiler on one system, you could rewrite it in the higher level language and use the first compiler to compile the second compiler. Then you can use the second compiler to compile future versions of itself. This would make it easier to extend the compiler to compile to binary code for other processors and operating systems, called cross-compiling.

There is no compiler that runs on the Nintendo DS, for example. Developers write the code on their computers and use a cross-compiler.

Hazzy Wrote:On a mildly related subject, why will a binary that works on Windows not work on Linux and vise-versa? (I have no idea how Mac uses binaries.)

I might be diverting from the OP's train of thought, though.

You're right in that you're still on the same kind of CPU so the binary instruction set is the same. But there are two reasons why a windows binary will not work in Linux and vice-versa.

1. A Windows .exe file or a Linux executable file do not just contain CPU instructions. They can also contain metadata such as the program version and program author. They also contain initialized global data and the initial memory layout of the program. See the Wikipedia article on Object File for more. Windows uses the Portable Executable (PE) format while Linux uses the Executable and Linkable Format (ELF). There is a part of an operating system called a loader that takes that executable format and sets up the program in memory before handing the program to the CPU.

2. For a program to do anything useful, it will need to use some system calls, functions that are executed by the operating system. Programs (aside from operating systems and device drivers) are not given direct access to devices like the hard drive. Instead they must ask the operating system to open a file and write these 200 bytes. Windows and Linux have different functions for opening and writing to files.
Reply


Messages In This Thread
Question for Programmers and the Like - by Khoi - 2010-10-29, 01:51 PM
Question for Programmers and the Like - by larmie - 2010-10-29, 02:06 PM
Question for Programmers and the Like - by Spaz - 2010-10-29, 03:34 PM
Question for Programmers and the Like - by Hazzy - 2010-10-29, 03:49 PM
Question for Programmers and the Like - by Fiel - 2010-10-29, 04:02 PM
Question for Programmers and the Like - by Spaz - 2010-10-29, 04:45 PM
Question for Programmers and the Like - by Nikkey - 2010-10-29, 05:23 PM
Question for Programmers and the Like - by Hazzy - 2010-10-29, 07:44 PM
Question for Programmers and the Like - by Eos - 2010-10-29, 09:56 PM
Question for Programmers and the Like - by Spaz - 2010-10-29, 10:28 PM
Question for Programmers and the Like - by Eos - 2010-10-29, 10:48 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)