Assembly Language Question...

Started by
6 comments, last by DevFred 15 years, 4 months ago
Am trying to learn some assembly so i was wondering.... Why is assembly language used in games? For what is it most used for? specific examples of this, please. What should every game programmer/programmer know how to do in assembly?
Advertisement
Quote:Original post by gameplayprogammer
Why is assembly language used in games?

Because it can access hardware features not exploited by the compiler and makes you able to have kind of full control over the hw and code executed. This way you can optimize some algorithms or parts of an algorithm if you KNOW VERY WELL what you're doing.
Quote:
For what is it most used for? specific examples of this, please.

I know that some people use assembly to access CPU extensions like SSE or to heavily optimize the most used functions (i.e. ray/tri intersection in a raytracer). In most cases (i.e. SSE) there are better way to do things (intrinsics).

Quote:
What should every game programmer/programmer know how to do in assembly?


I don't think that asm knowledge is so important today in most of the tasks. I tried to lear it once, but then I never used it simply because the compiler would have been smarter than me anyway. Some asm might be useful here and there, but don't expect that it will make the difference, since there are many other things to take into account ( i.e. choosing the right algorithms).

Quote:Original post by gameplayprogammer
Am trying to learn some assembly so i was wondering....
Why is assembly language used in games?
For what is it most used for? specific examples of this, please.
What should every game programmer/programmer know how to do in assembly?

1) It isn't, realistically. Assembly is effectively an endeavor that will be educational, but most certainly inapplicable in any situation you are likely to run into. Compiler intrinsics are *less* useless, but only marginally so unless you are dealing with atomic primitives like interlocked compare exchange [and some would argue certain intrinsics that expose certain SSE instructions, but compilers can emit those just fine. It really is questionable what you are saving in those regards beyond what the compiler would have produced]

2) SSE instructions typically find their way into vector math. Atomic primitives find their way into inter-thread synchronization primitives. There are intrinsics for both in any self-respecting compiler these days.

3) Every programmer should know that the further away from assembly you can get without loosing expressiveness, the better off you are. Instances where assembly, and even intrinsics, are used should be burried deep under a mountain of code so that nobody ever has to even think that it might be there.

**This MAY not be the case if you wander into a compiler that does not expose your architecture's special features. x86-based intrinsics are on pretty much everything, so it isn't an issue there. It becomes slightly more interesting if you go to other platforms or work with experimental systems. Even then, 3) applies soundly.
I thought it was usefull for doing multiplication/division, where you saving processing power by doing multiple adds or moves. Or is that in normal code where you do bit shifts instead of power multiply?
Quote:Original post by gameplayprogammer
What should every game programmer/programmer know how to do in assembly?


Every (game) programmer should know how to read assembly.

Very few programmers should write assembly.

Knowing assembly at least to the point of being able to read it is useful for tracking down retail build bugs where you have no reliable debug symbols, for instance.
Quote:
[and some would argue certain intrinsics that expose certain SSE instructions, but compilers can emit those just fine. It really is questionable what you are saving in those regards beyond what the compiler would have produced]

As far as I know, most compilers are not all that good when it comes to vectorize code suitable for SSE. In addition, most compilers take some time to implement support to latest extensions...

I would like to add that many (most?) high level languages doen't let you write asm code (I know only C/C++, but there may be others) and that many modern languages are often interpreted/compiled into a bytecode different that x86 one. So, what you're asking only matters if you're working with C++, I suppose.
Quote:Original post by gameplayprogammer
What should every game programmer/programmer know how to do in assembly?

You should be able to interpret the assembly generated by the C compiler. This greatly helped me understand the difference between arrays and pointers! Here is a very simple example C program:
int main(void){    char array[100];    char *pointer = array;    array  [10] = 7;    pointer[10] = 8;    return 0;}

And here is the relevant assembly output, together with the C lines that generated them:
char *pointer = array;    leal    -120(%ebp), %eax    movl    %eax, -124(%ebp)array  [10] = 7;    movb    $7, -110(%ebp)pointer[10] = 8;    movl    -124(%ebp), %eax    addl    $10, %eax    movb    $8, (%eax)

As you can see, accessing the 10th array element is very simple, because the adress array[10] can be resolved at compile time (-110 relative to the base pointer).

On the other hand, accessing the array via the pointer is more complex, because only the address of pointer is a compile time constant (-124 relative to the base pointer), but what it points to must be loaded from that address at runtime - and then we need to go 10 further before we can access the 10th array element.
Quote:Original post by gameplayprogammer
I thought it was usefull for doing multiplication/division, where you saving processing power by doing multiple adds or moves.

It is totally useless to do that kind of things yourself, that's what the compiler is supposed to do (and does on my system):
i = i * 5;    movl    %edx, %eax   // edx = 1 * i    sall    $2, %eax     // eax = 4 * i    addl    %edx, %eax   // eax = 5 * i

I must say I'm a little suprised, I thought multiplications were just as fast as shifting these days... whatever, let the compiler do that low-level optimization stuff!

This topic is closed to new replies.

Advertisement