views:

160

answers:

4

computers do not understand anything except one's and zero's.But I want to know the details how a source code or an instruction set is converted into 1's and 0's.Is the exe file only contains 1's and 0's?

+3  A: 

You need to research Compilers

Chris Thompson
so please give some sources from where I can research?
Radheshyam Nayak
Click on the link....the word "compilers" links to the wikipedia article about them and is an excellent place to start. A simple google search for "compilers" will also yield a wealth of information.
Chris Thompson
A: 

When you compile your code the compiler creates the equivalent in Assembly which then goes to the processor to run. The assembler does to job of converting the Assembly to binary.

Each high level instruction (in your programming language) as Assembly instructions associated to it. These Assembly instructions can be highly optimize (hence the GCC, and other compilers, optimization flags).

You might want to read http://computer.howstuffworks.com/bytes.htm

pma
The processor doesn't actually convert assembly to binary, that's the job of the assembler which functions in a similar manner to a compiler. Actually if you want to get technical, because these are both programs, the processor does all of the work from writing the code to storing it to converting it to...
Chris Thompson
Yes. You are corrected. I edited. The assembler converts to binary not the processor.
pma
so is it true that compiler may be different for different languages but the assembler is processor dependent?
Radheshyam Nayak
compiler is different for different languages, for same languages different platform (OS, ISA). Assembly is different for different ISA.
Kugel
The assembly code must be generated for a specific architecture to run in the processor. In GCC you have flags to create binaries for PPC, IA32 and other architectures.
pma
+2  A: 

Yes. A .exe is in essence a binary file format which contains 0s and 1s (it also contains other important OS information for your program to run)

A CPU comes with specification of certain basic operations like SUM, MOV etc. These are the only operations the CPU knows about. The compilers job is to interpret something like 2 + 3 and convert it into something that the CPU can interpret (SUM and flags checking for overflow etc..).

So in essence writing high level code like 2 + 3 it's just a shorter way of writing lots of assembly code (which is just a human readable version of binary).

Luca Matteis
so you mean to say assembler are processor dependent while compilers are language dependent?
Radheshyam Nayak
you forgot to mention that assembly language maps to binary operation codes.
Kugel
@Radheshyam yes every CPU has an instruction set such as i386 or x64. These define what and how you use instructions (called assembly language, but really there is no such thing). Instructions map directly to operation codes which are really just 0s and 1s sent to a correct place (instruction decoder in CPU). Compilers compile high level language (such as c++) into operation codes directly. Some other languages use intermediate codes (such as Java's bytecode).
Kugel
+2  A: 

Each type of operation in a microprocessor's instruction set is specified by an opcode, which is represented as a pattern of 1s and 0s. A compiler or interpreter translates code in a source language to machine code made up of those instructions.

For example, take this code, where a variable, x, is assigned the value of 50:

x = 50;

The compiler might translate that to the following assembly language, where the AX register (for the x variable) is set with the value, 50 (in hexidecimal), using the MOV instruction:

mov ax, 0x32

If the opcode for MOV was 0xA0 and the code for the AX register was 0xB then the machine code, in binary, would look like this:

10100000 00001011 00110010
Mark Cidade
An instruction is not implemented as an opcode. An instruction is made of an opcode and some operand identifiers. I think you're confusing the term *instruction* with opcode, or vice versa.
Luca Matteis
I reworded it but originally by "instruction" I just meant the operation denoted by the opcode, as opposed to a specific instance of the operation.
Mark Cidade