Skip to main content
GameDev.net gamedev.net
🔒 Locked

Compilation to machine code

Started by maya18222 Sep 17, 2010 at 7:28 AM 2 replies 750+ views
Original Post
maya18222
maya18222
I was wondering how compilation to machine code works, say from the perspective of C++ to Machine code. From my understanding, or from what Ive read, there is a direct mapping from assembly code to machine code. Where assembly code is just a more human readable representation. So would it be safe to say that the assembly Name for an instruction is in effect, similar to a MACRO.

#define EASIER_NAME_TO_REMEMBER 5E

Secondly, there seems to be quite a few different assembly languages. When do they differ?

- Per CPU vendor?
- Per CPU Iteration? ( ie, Intel 2, 3, 4 )

I assume all of the above, as each new iteration will bring new features, and each vendor will do things differently. So how does the compiler know which machine code instructions to use when it compiles an application?

When I use the assembly view in Visual Studio, what assembly language am I viewing?

Does it contains instructions specific to my CPU?


rip-off
rip-off
Quote:

I was wondering how compilation to machine code works, say from the perspective of C++ to Machine code.

The compiler transforms the statements into things the CPU can deal with. This is a broad topic, not one that can easily be explained in a forum post.

You might write x = y * 5, the compiler must figure out whether to fetch these from memory or registers, how to mutliply them (they might need to be in specific registers), where to store the result. A more advanced case might be the compiler figuring out how to skip this instruction, for example if the expression "y * 5" occurred recently then the result will most likely not be computed again. And that is just a really simple example.

Quote:

Secondly, there seems to be quite a few different assembly languages. When do they differ?

- Per CPU vendor?
- Per CPU Iteration? ( ie, Intel 2, 3, 4 )

Typically both. It depends though, both AMD and Intel strive to make compatible CPUs (on the desktop anyway). There are CPU families, which are backward compatible with previous iterations. An example was when 64 bit instructions were added to the X86 architecture, effort was made to allow the same CPUs run non-64 bit code.

The compiler has a setting for which architecture to target. By default it usually targets an early predecessor of your own hardware. Picking a more specific CPU might mean that the compiler produces optimal code, but it means you now have you compile lots of times and offer these to the end user - which will confuse the average end user (who would response with "Windows" or even "Internet Explorer" if you ask them what CPU they have).

In a more advanced case, you can have a "stub" installer that detects the CPU type and then downloads the optimised executable for that architecture.

Quote:

When I use the assembly view in Visual Studio, what assembly language am I viewing?

The same one the project is targetting
Quote:

Does it contains instructions specific to my CPU?

To some extent yes, it will contain instructions that won't run on every CPU. But by default it will target a reasonably generic instruction set. If you have the latest and greatest CPU model, it is unlikely your compiler will generate instructions for that CPU by default (unless you are on Gentoo or something).
maya18222
maya18222
Could you recommend any books on Assembly?

Or would I be best looking at some of the manuals published on the AMD and Intel sites?

Im basically looking for something of an introduction to Assembly.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.