Sign in to follow this  
VanillaSnake21

Unity when do you use assembly?

Recommended Posts

VanillaSnake21    175
Hi, after spending much time writing my game in c++, I decided to learn how to write in assembly, since many books/sites said it would be a helpful co-language. I was wondering where and how often does assembly comes into play in game development. I've seen a few example where chunks are thrown in c++ code within _asm{} and even that only used standard mov, push, pop to get the job done. Is it always limited to that in game dev community or is there a broader range where it's used. It would be nice to hear from someone who is professionally involved in the game dev business and how they use assembly in their jobs, Thanks, Tim.

Share this post


Link to post
Share on other sites
Palidine    1315
Almost always only to optimize super low level things: matrix multiply, vector operations, etc. It's basically the last ditch optimization effort when all algorithmic options have been exhausted.

It's also very helpful for debugging in release. In release the debugger always lies, but the assembly never does. So jumping to assembly mode can tell you where you actually are in the callstack, show you which variables are in which registers so you can debug them in the watch window, etc.

-me

Share this post


Link to post
Share on other sites
Driv3MeFar    1080
The most ASM I've ever used on a project was a GBC game, so I didn't have much choice. Day to day, I use it very rarely. I use it in a release-mode assert macro to cause an interupt, and I read it when debugging release-mode only bugs, but that's about it.

[Edited by - Driv3MeFar on May 4, 2007 4:22:55 PM]

Share this post


Link to post
Share on other sites
superpig    1825
You almost never write code in assembly on PC. The closest you tend to get is using intrinsics for things like SSE.

However, knowing assembly to the extent that you can read it can be helpful. It gives you a better knowledge of what's happening under the hood, and it can occasionally be useful when solving some problems. I've faced bugs before that only appear when the optimizer is turned on, and being able to look at the optimized code and see how it corresponds to my original source was instrumental in figuring out what was happening.

Share this post


Link to post
Share on other sites
Crypter    748
Assembly should only be used when neccessary, and no more.

Assembly may be used for checking processor-specific features (SSE,SSE2,
3dNow!,etc), and for some optomizing. Note that if you use assembly, you should
always, if possible, provide a C++ alternitave as not to break portability.

Assembly is useually used in systems programming, not as much in game
development.

I second learning how to read assembly, as it can be very usefull in debugging.

Share this post


Link to post
Share on other sites
speedie    140
in the days of the first 3D games, ASM was used quite often especially for the renderer, i belive the source for quake uses it extensivly. but now a days compilers are usually good enough at optimizing code on their own.

Share this post


Link to post
Share on other sites
Omid Ghavami    1007
Quote:
Original post by speedie
in the days of the first 3D games, ASM was used quite often especially for the renderer, i belive the source for quake uses it extensivly. but now a days compilers are usually good enough at optimizing code on their own.


The reason it isn't used for the renderer is not that compilers are better at optimizing (which they probably are), but that we don't use software renderers anymore now that we have GPUs

Share this post


Link to post
Share on other sites
David Neubelt    866
We use assembly to extend the C++ programming language. We've stolen a few concepts from LISP and integrated it in a post-build step to patch the executable. This allows us to use concepts that aren't easily or effeciently done using the language natively.

Most simulation systems and rendering systems at our company are optimized in assembly. We've always pushed the console to its max so we typically don't have any spare cpu cycles.

Share this post


Link to post
Share on other sites
Ralph Trickey    230
Quote:
I second learning how to read assembly, as it can be very usefull in debugging.

I'll 'third' that one. It's a rare debugging session when I don't have to drop into assembly. Visual Studio seems to be horrible about debugging release code. It's also useful for determing exactly what if statements are doing, and which call is returning which value. I've several large scenarios that I can't run in debug mode because it's way too slow, and debugging them in release mode can be 'interesting.'

--
Ralph Trickey
TOAW III Programmer

Share this post


Link to post
Share on other sites
VanillaSnake21    175
Thanks for the replies, so basically everyone is saying that assembly is almost dead in games, except for debuggin purposes. I think that overall, the language is becoming extinct as a main production language, the only places it seems to be usefull is in boot-sectors for OSs and console games. I still plan on learning it though, for me it seems so much easier than high-level langauges, mainly because i now know about everything that the computer does and how it does it. I don't know why schools don't teach this as a first and primary langauge. When students know that processesor takes a certain amount of commands, and these command are represented with 1s and 0s, so when a prossessor sees 1001 coming at it knows it needs to ADD the next two varibales, I think that explains it so much better than the way high level langs are teaching us.

Share this post


Link to post
Share on other sites
Promit    13246
Quote:
Original post by Vampyre_Dark
Never have, never will.
Why not? Is intentionally crippling yourself as a programmer just amusing?

Share this post


Link to post
Share on other sites
daviangel    604
Quote:
Original post by VanillaSnake21
Thanks for the replies, so basically everyone is saying that assembly is almost dead in games, except for debuggin purposes. I think that overall, the language is becoming extinct as a main production language, the only places it seems to be usefull is in boot-sectors for OSs and console games. I still plan on learning it though, for me it seems so much easier than high-level langauges, mainly because i now know about everything that the computer does and how it does it. I don't know why schools don't teach this as a first and primary langauge. When students know that processesor takes a certain amount of commands, and these command are represented with 1s and 0s, so when a prossessor sees 1001 coming at it knows it needs to ADD the next two varibales, I think that explains it so much better than the way high level langs are teaching us.

Yup in the pre directx days it was still used quite a bit as you can see if you pick up one of Andre Lamothe's first game programming books where he used it to program a sound driver for a game as one example. Also look up Mike Abrash.
Also ,the master programmer Peter Norton of norton utilites fame, wrote the first disk doctor,etc in assembly and if you want to see how he did it pick up his old book where he teaches you assembly. That's how I learned and all his code still works except for the direct disk access code. You also need to use a 16bit linker since it's 16bit assembly code.
But like everyone I've moved on to higher languages as Peter Norton did for later versions of Norton Utilities and everyone else that wants to be more productive.
Only use it now for debugging like everyone else and when using custom console game machines that doesn't even come with basic c compiler so I'm forced to use it.

Share this post


Link to post
Share on other sites
Daaark    3553
Quote:
Original post by Promit
Quote:
Original post by Vampyre_Dark
Never have, never will.
Why not? Is intentionally crippling yourself as a programmer just amusing?
? [lol] I certainly don't view it as intentionally crippling myself. Is a guitar player who doesn't want to play the bagpipes too limiting himself musically?

Everyone was offering their own take, and I was offering my own. Never have / Never will comes from me being a hobbyist who writes small games for fun, and learning asm to get some speed boosts in certain functions has 0 appeal to me.

Everyone wants and gets something different out of programming. I keep my designs small and simple, so worrying about frame rate or other little effiency concerns never really comes into play. I don't push the limits of anything, or require huge amount of resources, so my stuff always runs fast enough, that I don't have to go hunting around and obbssessing over optimizations. (Not to say that I don't go back and rewrite some of my ugly placeholder code I sometimes quickly write, but no need to resort to ASM...)

I'm more concerned with the look, feel, and functionality of my projects, rather than if I write the fastest / most efficient possible functions for every little part of it. I'm sure ASM could speed up some of my stuff, but that's not my goal. I get several hundred FPS on all my stuff on my ancient rig, more than enough for me.

Share this post


Link to post
Share on other sites
Harry Hunt    542
I highly recommend learning assembly because it will give you a much better understanding of how high-level languages like C++ actually work and help you to write code that is more efficient.

I discourage using assembly to optimize your C++ code though, unless you have strong evidence that the code generated by your compiler is what's causing your performance issues. In most cases when certain bits of code become a bottleneck, the reason is on the algorithm- and not on the compilation level.

Share this post


Link to post
Share on other sites
mattnewport    1038
On the PC, PS3 and Xbox 360 optimizing by writing inline assembly directly is rarely a good idea. Using intrinsics is generally better because it gives the compiler a lot more freedom to optimize effectively - using inline assembly prevents the compiler from performing a lot of useful optimizations. The only reason to write assembly on those platforms is if for some reason you can't get the compiler to generate the code exactly the way you want, it's really a last resort.

As others have said though, knowing enough assembly on your target platform(s) to be able to follow what's going on in the debugger is very useful and a working knowledge of assembly is generally useful to help you write efficient code in C++.

In the game code I've seen, assembly was used in a few key places on the PS2 and to a lesser extent the Xbox and popped up occasionally on the PC (often to access particular instructions like rdtsc directly rather than as an optimization) but is very rarely used on the 360, PS3 or newer PC code. Intrinsics are generally used now where assembly might have been used in the past.

Share this post


Link to post
Share on other sites
VanillaSnake21    175
Thanks for the replies, I'm currently studying Java in my school, and I came upon a topic of stacks, before I used to think it's only used for memory management but a chapter in the book explains thats it's used in parsing and compiler development. For example to transform the infix to postfix. They gave some example code in Java, which came out to be a couple of hundred of lines long, which is pretty short for a high-language app. But then I realized how easily this could be done in assembly where I don't have to declare any classes, don't have to extend/implement anything, but simply say push this on stack, pop this off, move the ebx to a new position, rertieve the variable 8 bytes from ebx etc. Working with stacks seems almost native to assembly, is that something it could be used for? Thanks

Share this post


Link to post
Share on other sites
TheAdmiral    1122
Quote:
Original post by VanillaSnake21
Working with stacks seems almost native to assembly, is that something it could be used for?

Not really. While there are a couple of clever stack tricks floating around (many of which will corrode your soul), you have to understand that the stack is there for a good reason and is constantly doing its job - it isn't just a convenient general-purpose data structure for the programmer. If you need a stack to store some user data, make a stack, 'cause the thread's stack won't help you.

As well as keeping track of function calls, the stack is responsible for structured exception handling (SEH). Throwing into the mix temporary storage around linear code, we have a very overworked data structure. Assembly programmers who have tried to rely on the stack for anything other than its well-defined and strictly policed purposes (mainly those involving ESP, EBP and FS:[0]) have lived to regret it. Some didn't even live*.

If you want to use the stack for something clean and appropriate, such as recursion, then you're free to do so in whatever procedural language you choose. From another perspective, if you find yourself needing assembly language to work your magic, then you're almost certainly breaking the rules and will most likely pay the consequences.

Admiral

* I think I made that bit up.

Share this post


Link to post
Share on other sites
mattnewport    1038
x86 assembly happens to have explicit instructions for stack manipulation but that's not true of assembly languages in general. PowerPC assembly for example (as used on the Xbox 360 and PS3) doesn't have explicit stack instructions or registers, general purpose registers and ordinary load, store and add instructions are used for stack manipulation.

Share this post


Link to post
Share on other sites
Promit    13246
Quote:
Original post by Vampyre_Dark
Everyone was offering their own take, and I was offering my own. Never have / Never will comes from me being a hobbyist who writes small games for fun, and learning asm to get some speed boosts in certain functions has 0 appeal to me.
Quote:
Original post by Harry Hunt
I highly recommend learning assembly because it will give you a much better understanding of how high-level languages like C++ actually work and help you to write code that is more efficient.
Quote:
As others have said though, knowing enough assembly on your target platform(s) to be able to follow what's going on in the debugger is very useful and a working knowledge of assembly is generally useful to help you write efficient code in C++.
Even dropping the efficiency point, it's a really good idea to know what's going on under the hood, and to be able to take a look if necessary.

On a sidenote, I tend to assume that everyone here is looking to eventually develop some type of software professionally.

Share this post


Link to post
Share on other sites
Rockoon1    104
Assembly isnt for one-liners anymore. As has already been mentioned, small asm functions that dont contain big loops will break your compilers optimization with no benefits to compensate.

My rule of thumb these days:

If I cannot link the assembly language routine from an external .obj, .lib, or .dll because the calling overhead will obviously outweight the benefits.. then its also not worthy of being inline assembler.

Share this post


Link to post
Share on other sites
exwonder    100
Assembly as a written language is dying, and inline assembly is being replaced with intrinsics. However, the usage of the two can be very similar.

There are a bunch of reasons why knowing an assembly language can be helpful:

- It teaches you how to think about code at the lowest practical level.
- It familiarizes you with concepts that are helpful for writing code for coprocessors (MMX, SSE, VMX, etc).
- It can be a great aid while debugging, especially in code that's not yours. ("What does blr mean?")

The third point is reason enough to learn it. However, it should be noted that a very high percentage of game industry coders will never have to write a line of assembly or touch an intrinsic. It's not an essential skill to succeed, but it's a very very large advantage to have if you're doing any sort of engine or graphics work.

Share this post


Link to post
Share on other sites
outRider    852
Quote:
Original post by VanillaSnake21
Thanks for the replies, I'm currently studying Java in my school, and I came upon a topic of stacks, before I used to think it's only used for memory management but a chapter in the book explains thats it's used in parsing and compiler development. For example to transform the infix to postfix. They gave some example code in Java, which came out to be a couple of hundred of lines long, which is pretty short for a high-language app. But then I realized how easily this could be done in assembly where I don't have to declare any classes, don't have to extend/implement anything, but simply say push this on stack, pop this off, move the ebx to a new position, rertieve the variable 8 bytes from ebx etc. Working with stacks seems almost native to assembly, is that something it could be used for? Thanks


That's a general stack data structure you're talking about, which is similar to and yet very different from the stack a processor uses while running. You don't usually use the processor's stack like you would a stack from your library for various reasons, though I suppose you could in some sort of convoluted way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this