Thread: What do you still use assembly for?

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    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?

    Cheers.

    P.S. Hope this isn't the wrong category.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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).


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Debugging of either compiler bugs (or bugs in code that you have no source-code for).
    Such as the case recently when Bubba worked out why NFS wouldn't run on his CPU. No SSE3 or something.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    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.

  6. #6
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    At last but not last, patching and cracking.

    Somewhat this may considered illegal, but it's fine to analyze 3rd party executable to find something suspicious for malware processes.
    Just GET it OFF out my mind!!

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Yeah I need to get a better book, too - I have a book on 8086, a book that focusses on DOS interrupts in Assembly, and a book on System/370. Time to update!

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Don't forget computer security industry, which uses assembly for reverse engineering.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Bubba,

    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 05:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM