-3

As we know each processor has its own instruction set (ex-8085 processor has its own INSTRUCTION SET).So to execute any instruction by using that processor the instruction must be one of from its instruction set (means we can execute only that instruction on 8085 processor which belongs from 8085 instruction set).

so here my question is if we write any program (assume we write program using c language) then after compiling using compiler( as we know compiler generates code which is executable on machine ex..gcc or turbo c) it generates code which is used to run on machine i.e the code (set of instruction which belongs from that processor INSTRUCTION SET) to execute on processor.so how our compiler knows which processor we are using and converts the whole program to instructions which are executable by our processor.

example- 1) if we install turbo c on machine which has Pentium processor.so if we execute our c program on this machine it must convert whole program to set of instruction which belong from Pentium processor INSTRUCTION SET And if we install turbo c on machine which has i5 processor then it must convert the whole program to set of instruction which belongs from i7 processor INSTRUCTION SET.

so how the compiler knows which processor we are using and why the compiler is different for OS it must be different for the processor according to above explanation?

(i'm finding an answer to this question from long time and SORRY FOR lengthy statement)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • "As we all know, each language has its own vocabulary. Words in English are not the same as words in French. It is the job of a dictionary to convert words from one language to another. How does the dictionary know which language we are using and convert words to the correct language?" – Raymond Chen Apr 07 '18 at 06:21
  • 2
    This looks like you've copied your homework question. Have you tried any research of your own? – Dragonthoughts Apr 07 '18 at 06:27
  • 1
    Primarily it boils down to what you have installed. When you install a compiler (on any modern OS), it installs a [*Toolchain*](https://en.wikipedia.org/wiki/Toolchain) for that machine. You are also free to install any other *Toolchain* available allowing you to cross-compile for a different platform on that machine. – David C. Rankin Apr 07 '18 at 06:30
  • @Dragonthoughts Even if this is homework, there is nothing wrong with homework questions on SO. The important thing is (only) whether it is a good question. I would however expect this to be dup. – Support Ukraine Apr 07 '18 at 06:31
  • 1
    These links could be of interest. https://en.wikipedia.org/wiki/Application_binary_interface and https://en.wikipedia.org/wiki/Binary-code_compatibility – Support Ukraine Apr 07 '18 at 06:40
  • @Dragonthoughts this is not my homework question. i just give brief explanation because sometime people don't get the exact question. i am just curious and i will be happy if u give ans.. – Shital kallole Apr 07 '18 at 06:51
  • @Raymond Chen yes similer to this.. – Shital kallole Apr 07 '18 at 07:01
  • If you use Turbo-C ([please don't](https://stackoverflow.com/tags/turbo-c/info)) it generates instructions for the processor found in the original IBM PC. And even though the Pentium, i5, and i7 processors have gotten many new instructions, they still also know the ones used by the first PC. – Bo Persson Apr 07 '18 at 13:15
  • How the compiler knows is very simple. The human (programmer) uses the right compiler with the right command line. Its no more complicated than that. You on an x86 and want to compile for an 8085 you get an 8085 (cross) compiler. No more complicated than that. – old_timer Apr 07 '18 at 14:18

2 Answers2

1

Some compilers can only generate machine code for a specific system. If you want to compile for different systems, you have to get different versions of the compiler, or a different compiler entirely.

Other compilers know how to compile for many different processors. But unless you tell it otherwise, it assumes that you want to generate code for the same type of machine you're running on. So if you run the compiler on 80x86 Linux, it generates machine code for 80x86 Linux.

Compiling one one type of system and producing code for another type is called cross compiling. You have to tell the compiler what the target architecture is when you do this.

The way you do this depends on the specific compiler. It might be command-line options if you compile from the CLI, or it might be in a settings dialogue if you use a GUI.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

The instruction sets of different processor models in the same architecture family are not completely different. The instruction set for the architecture has various subsets, and different processors will have different selections of subsets. Generally, there is a subset (or several subsets) of base instructions that grows over time as processors become more complex and various extra subsets that contain instructions that are currently optional, depending on the processor model.

When a compiler compiles, it compiles for some target, which includes a set of instruction subsets it is allowed to use. Typically, a default target is the base instructions. The resulting compiled program can run on any processor with those base instructions, but it may not get the performance possible if some of the optional instructions had been used.

Compilers for such architectures typically have switches you can use to tell them which processor model to target or which instruction subsets the compiler may use. The resulting program can run only on processor models with the specified instructions. It may perform better because of the instructions it uses but will not run on processor models without those instructions.

A compiler might automatically adapt its target based on the system it is running on. There are instructions and system calls that report information about the instruction subsets available on the current processor and about the operating system version and features, so a compiler can use these to tailor its target. So a compiler by default might compile for the system you execute it on but could compile for other targets if you give it switches requesting that.

Ultimately, a compiler is just a computer program. It runs, reads input, and writes output. That output can be a program for any computer the compiler is designed to support.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312