Thread: basic(possibly insane) question about compilers.

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    91

    basic(possibly insane) question about compilers.

    i don't know if this question is valid or not.tried on google but i didnt get a solution.

    does a compiler generate different machine codes for different cpus?
    like does it generate different binary codes for intel and amd for the same program?

    if so how does the compiler know what cpu the computer has?

    then does a compiler generate different codes for different operating systems?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > does a compiler generate different machine codes for different cpus?
    Yes.

    > like does it generate different binary codes for intel and amd for the same program?
    Possibly.
    GCC for example will generate default code which will run on anything better than a 386.
    Or you can have one of any number of machine specific flags which will generate more optimal code for a particular CPU variant.

    > if so how does the compiler know what cpu the computer has?
    You read the box the compiler came in (or the web page) to see what target(s) it supports.

    > then does a compiler generate different codes for different operating systems?
    The compiler probably doesn't care about your OS.

    All the OS magic is usually hidden inside say libc, where the actual OS specific API's (say to open a file) reside.
    To that end (creating an image that will run on a particular OS) is really a linker issue, not a compiler issue.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by theju112
    i don't know if this question is valid or not.tried on google but i didnt get a solution.

    does a compiler generate different machine codes for different cpus?
    I tried on google and got a few links that looked like they might answer the question. Granted, they did not appear to answer the question directly, but it is pretty easy to infer the answer. (Like, if an alien wanted to know whether every country in the world used US dollar as the primary medium of exchange and after searching found a currency converter tool online, the alien would infer that there are currencies besides the US dollar, even though the currency converter did not provide an FAQ that explicitly stated this.)

    I think you need to be more persistent with your searching. Just because there is no FAQ with your exact question does not mean that you should immediately turn to this forum for help with the excuse that you tried to search but failed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is complicated... The answer is...

    It depends on the compiler.

    Cross-platform compilers will generate different code for different CPUs and different code again for different OSs.... But you have to tell it which. It can't possibly know at run time so it's up to the end user to select software that is compatible with their CPU/OS...

    For Windows...
    On 32 bit OSs Intel and AMD both run on the same x86 instruction sets. That was the AMD's whole idea... to make an Intel compatible CPU and they went right out and did it better than Intel.

    On 64 bit Windows there are 2 code sets... IA64 whitch is specific to the Itanium processor from Intel and X64 (aka amd64) which is for both Intel and AMD 64 bit processors.

    GENERALLY (as in there are exceptions)... Code produced by 16 bit compilers (Turbo C, some early GCC versions, etc) will not run on a 64 bit OS; windows will tell you it's not a valid PE image. 32 bit x86 code will run on both Intel and AMD in both 32 and 64 bit OSs, but this may not always be the case. 64bit x64 code only runs on 64 bit Intel and AMD systems and most compilers will not create 64 bit code on 32 bit systems because there would be no way to test it. 64 bit IA64 code only runs on 64 bit Itanium based server systems.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Quote Originally Posted by laserlight View Post
    I tried on google and got a few links that looked like they might answer the question. Granted, they did not appear to answer the question directly, but it is pretty easy to infer the answer. (Like, if an alien wanted to know whether every country in the world used US dollar as the primary medium of exchange and after searching found a currency converter tool online, the alien would infer that there are currencies besides the US dollar, even though the currency converter did not provide an FAQ that explicitly stated this.)

    I think you need to be more persistent with your searching. Just because there is no FAQ with your exact question does not mean that you should immediately turn to this forum for help with the excuse that you tried to search but failed.
    so you think im an alien(just joking).i know my all my posts sound like out of this world.it'll be that way till i am thrown out of here.

    seriously i searched a lot...just wanted to save some time and wanted to be sure. will be careful next time.

    All the OS magic is usually hidden inside say libc, where the actual OS specific API's (say to open a file) reside.
    To that end (creating an image that will run on a particular OS) is really a linker issue, not a compiler issue.
    thanks salem.i had read that libc contains library routines and headers but i didnt know apis would be in there. im not so familiar with APIs, all i know is that an api is used for performing specific functions on behalf of the programmer.

    but your answer explains enough.
    Last edited by theju112; 08-21-2011 at 09:49 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by theju112 View Post
    so you think im an alien(just joking).i know my all my posts sound like out of this world.it'll be that way till i am thrown out of here.

    Welcome to my ignore list...

  7. #7
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Quote Originally Posted by laserlight View Post
    I tried on google and got a few links that looked like they might answer the question. Granted, they did not appear to answer the question directly, but it is pretty easy to infer the answer. (Like, if an alien wanted to know whether every country in the world used US dollar as the primary medium of exchange and after searching found a currency converter tool online, the alien would infer that there are currencies besides the US dollar, even though the currency converter did not provide an FAQ that explicitly stated this.)

    I think you need to be more persistent with your searching. Just because there is no FAQ with your exact question does not mean that you should immediately turn to this forum for help with the excuse that you tried to search but failed.
    ??? What on earth are you babbling Laser? Last time I checked in here, you were helpfull guy with posts containing knowledge that benefit the readers.

    For me the original post appeared to be a valid question, related to C and containing greatly more interesting things to consider than the "snprintf's returnvalue may vary depending the operating system, and thus you should always handle both the negative and positive (larger than given buffer size) return values" - posts. Furthermore, now when Salem did answer to this question, this thread does benefit reader. It contains now some valuable information, which is much harder to find than the "thou shall not use void main, thou shall not use gets, ..." type of advieces these forums tend to be flooded with.

    Aliens and currencies... For god's sake, couldn't you have used the time writing that took to produce something constructive instead?

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Quote Originally Posted by CommonTater View Post
    This is complicated...
    so is your answer. i am not ready for all this sir.
    this is the first time im hearing IA64.
    2 code sets in windows?is this something related to cisc and risc?

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    Sorry, when I learned assembler, I found that no one use assembler to write driver because each assembler just comparable with some type of processor (for example: you need an assembler A to write program for AMD and assembler B to write program for INTEL), they always write driver on some high language, (maybe the most is C) --->so my answer is: when you write a program, when compile it, each processor will have different binary code.

    And how about different OS, I think obvious that different(and very different too @@)( except language like java that I don't know). for example, when you write on C on Windows, C will use some API functions of windows and if you use C on Linux, of course C will use other function--> so binary code will be different.

    I have read somewhere, they tell that, slogan of java is "write once, run anywhere",It seems that binary code of two different OS are same!!! (but every java program, they run on "Java virtual machine", so maybe, this thing make two programs different

    all information above maybe.... very wrong :">

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by theju112 View Post
    i don't know if this question is valid or not.tried on google but i didnt get a solution.

    does a compiler generate different machine codes for different cpus?
    like does it generate different binary codes for intel and amd for the same program?

    if so how does the compiler know what cpu the computer has?

    then does a compiler generate different codes for different operating systems?
    For future reference, posts such as these belong in the Tech Board. As for your questions, you are going to need to understand the difference between compilation and linking. Additionally, you are going to want to look at Instruction Set Architecture (ISA) and the ELF and PE file formats.

    After reading through this information, you should be able to ask more informed questions, and hopefully have the required knowledge to narrow your search terms to prevent not being able to find the answer by yourself.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Quote Originally Posted by Maz View Post
    ??? What on earth are you babbling Laser? Last time I checked in here, you were helpfull guy with posts containing knowledge that benefit the readers.
    thanks for that maz . but youll probably change your mind if you read my other posts.

    but as you pointed out i asked the question because i had a difficult time searching for the answer.
    Last edited by theju112; 08-21-2011 at 09:58 AM.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Maz View Post
    ??? What on earth are you babbling Laser? Last time I checked in here, you were helpfull guy with posts containing knowledge that benefit the readers.
    I don't know who you are, but your post was neither informative, funny, or even the slightest bit justified.

    Quote Originally Posted by hqt View Post
    [BUNCH OF NONSENSE].....
    all information above maybe.... very wrong :">
    All the information is wrong and should be ignored. Please check your facts more thoroughly before posting replies.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Quote Originally Posted by CommonTater View Post
    Welcome to my ignore list...
    please please wait wait wait......i am just fooling around.

    please continue posting answers.

    though we always end up on the wrong sides,i value your answers.

    if you still want to add me to your ignore list,im helpless.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by hqt View Post
    Sorry, when I learned assembler, I found that no one use assembler to write driver because each assembler just comparable with some type of processor (for example: you need an assembler A to write program for AMD and assembler B to write program for INTEL), they always write driver on some high language, (maybe the most is C) --->so my answer is: when you write a program, when compile it, each processor will have different binary code.
    Not actually true... on 32 bit code, both Intel and AMD run the same basic x86 instruction sets and their code is interchangeable. Assemblers like MASM and POASM see no difference between them.

    On 64 bit code you have two instruction sets... IA64 and x64 (aka amd64)... IA64 is specific to the Intel Itanium server chip... x64 code will run equally well on both Intel and AMD processors, provided the OS is compiled as 64 bit code.

    Windows 64 bit versions run x86 code through an emulator (stupidly named WOW64) but do not run 16 bit IAPX code at all.

    And how about different OS, I think obvious that different(and very different too @@)( except language like java that I don't know). for example, when you write on C on Windows, C will use some API functions of windows and if you use C on Linux, of course C will use other function--> so binary code will be different.

    I have read somewhere, they tell that, slogan of java is "write once, run anywhere",It seems that binary code of two different OS are same!!! (but every java program, they run on "Java virtual machine", so maybe, this thing make two programs different

    all information above maybe.... very wrong :">
    The Java run time environment is OS specific... the token code interpreted by the runtime is not.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    All the information is wrong and should be ignored. Please check your facts more thoroughly before posting replies.
    ah I don't joke anything or tell something just for funny. but after some my stupid post about pointer, I see that I may... very stupid, so I don't trust information that I have learnt by myself

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. possibly a stupid question about inheritance
    By Elkvis in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2010, 03:44 PM
  3. Very simple, possibly trick question.
    By matthayzon89 in forum C Programming
    Replies: 26
    Last Post: 04-23-2010, 06:56 PM
  4. Possibly simple question
    By PAragonxd in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2008, 02:43 AM
  5. Visual Basic Compilers
    By Inept Pig in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-07-2002, 04:17 AM