Introducing the compiler

Once we have finished our code, we use a compiler to translate that high-level code into assembly, which in turn will get turned into machine language that our computer can execute. Afterwards, it will translate that program into an executable file that the user can run. Functionally, it looks something like this:

This has several advantages, as it provides abstraction from the hardware details. For example, we don't need to work directly with registers, memory, addresses, and so on anymore. It also makes our code portable in that we can use the same program and have it translated by different assemblers for the different machines that use it. This was one of the reasons C took off and became so popular, because it allowed people to write code once and then have it work everywhere. You may notice that Unity has taken the same thought process to game development, and that's one of the reasons that I feel they've been so successful as of this writing.

This is a much more efficient use of time than writing assembly language code as it allows us to create more complex programs and machines, and modern compilers such as Microsoft's produce some highly efficient assembly code in most circumstances. This is what we have been using over the course of this book.

Writing code in assembly can still have its benefits though. For instance, after you've written your game in a higher-level language, you can start profiling it and seeing what aspects of the game are the bottlenecks, and then determine if rewriting it in assembly will give you a speed boost. The point of using a lower-level language is the fact that you can gain some substantial speed advantages.

For a real-life example as to how assembly was used to optimize a game engine, check out the following article from Intel: https://software.intel.com/en-us/articles/achieving-performance-an-approach-to-optimizing-a-game-engine/.

One of the issues with writing code that needs to be compiled before running is the fact that as projects increase in size, so do compile times. Recompiling a full game can take minutes to hours depending on the size of the project, and while that's going on, you are unable to work on your project, or else you'd need to recompile again. This is one of the reasons why scripting languages can be useful.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.218.221