What do you still use assembly for?

This is a discussion on What do you still use assembly for? within the Tech Board forums, part of the Community Boards category; I've been taking this assembly class in school and having much fun with it, and it has taught me a ...

What do you still use assembly for?

I've been taking this assembly class in school and having much fun with it, and it has taught me a lot about the computer at a lower level. However I've still been trying to find places that I can use it in real life. Do any of you use it in real life for anything aside from microcontrollers that don't have a C compiler and such?

I had grabbed a few tutorials on inline assembly optimizations for c/c++, but from what I read now, most of those are obsoleted.

Is there really any reason for using ASM aside from C anymore when there is an option? Also any examples of what you still may use assembly for?

You need assembly to boot a computer - boot loaders have to do specific things at specific bytes, and you can't control it to that level in C. I've also had to access specific registers before, but I don't recall the specifics... In all honesty, it was probably horrible design that caused it in the first place.

If I'm not misinformed, games do it a lot when there's heavy graphics work to be done, too.

Most modern games rely on the graphics card to do the heavy maths/graphics work - but sure, if you have a smallish loop that traipses through some array of data and does some relatively simple math on it, rewriting that bit in assembler using SSE will most likely improve the speed by a fair amount (particularly if the data is stored correctly in the first place).

Assembler is mainly used for things that can't be done in C - most of these have been mentioned, but to summarize.

The REASONS to use assembler language.

Accessing specific registers
For example x86's CR0, CR2, CR3 are regularly changed by the OS - and the corresponding registers exist on most processors, with different names.

Using specific instructions
SSE would be the most common, but most processors have special instructions - bswap to change the byte-order of a 32- or 64-bit word in one instruction, disable/enable interrupts/traps, virtual machine specific instructions, etc.

Performance optimization
Often in conjunction with using special instructions (typically SSE, 3DNow!, MMX etc), but sometimes simply using the programmers intelligence/knowledge over the compiler-generated code.

Ensuring SPECIFIC ordering
Since the compiler is allowed to re-order instructions, it is sometimes necessary to write specific ordered instructions in assembly language to enforce the order of operatioons.

Some specific types of code that require assembly code:

Context-switching and interrupt handling.
At a very minimum, some of the registers needs to be saved in some way - in interrupts, the temporary registers must be saved before calling C code. In a synchronous context-switch, the compiler-preserved registers must be saved (usually on the stack) and then the stack pointer is saved in the control block, and the stack of the new context loaded, and followed by the reverse of the save-context operation.

System call interface.
The OS's system calls will involve some sort of user-to-kernel transition. This usually involves using some specific system call instruction (software interrupt or trap instruction) to enter the kernel and to get back to the user-mode code.

Setting up environment for the programming language
Many computer languages require certain things to be in a certain way before the compiler-generated code can work. This is done as part of the language runtime, and is most often done in assembly code.

In most cases, a few instructions are written as inline assembler, whilst large portions of code (e.g. the system call interface) is written as one assembler source file.

There are more reasons to know and understand assembly language:

Understand what the compiler actually does - if you understand how instructions works, then you can better understand what happens when the compiler sees

Code:

a[23] = 11;

Debugging of either compiler bugs (or bugs in code that you have no source-code for).

Oh okay cool Just making sure I wasn't totally wasting my time. It's all very interesting learning about asm, but every book or tutorial that I read through is usually centered around some old 16 bit dos implementation that makes it feel like the majority of what i was learning was useless.

Cool thanks again for the summary, I'll definently be delving further into assembly.

Using specific instructions
SSE would be the most common, but most processors have special instructions - bswap to change the byte-order of a 32- or 64-bit word in one instruction, disable/enable interrupts/traps, virtual machine specific instructions, etc.

Performance optimization
Often in conjunction with using special instructions (typically SSE, 3DNow!, MMX etc), but sometimes simply using the programmers intelligence/knowledge over the compiler-generated code.

As of MSVC 2005 there is a feature hidden in the project properties under advanced that specifically requests that the compiler use whatever SIMD instructions are available for the current CPU. I suspect this is why the recent rash of games are using SSE2 and will not run on my system. They probably do not even realize that the default is SSE2 if your CPU supports it, otherwise it is SSE1 or what it calls SIMD. 3DNow and MMX<x> are used by D3DX since the math portions of D3DX were specifically programmed by AMD and Intel for their respective CPUs. Most games probably do not use assembly anymore since vertex and pixel shaders have become very common. These are assembly programs that execute at the vertex and pixel level right on the GPU. You can't ask for much more power than that.

Of course, using SSE or whatever to do graphics is much rarer today than it was some 5-10 years ago when graphics cards were nowhere near as good as they are today.

I'm not convinced compiler-support for SSE2 will give much - my experience of it is that it uses the SSE2 instructions, but not very efficiently, so you get tied to using instrucitons that aren't in the older models of processors, but you do not get much performance boost - I could be wrong tho - I haven't used VS 2005 or 2008.