assembly code

Discussion in 'Programming' started by Danimal1209, Nov 3, 2012.

  1. Danimal1209

    Danimal1209 Senior member

    Joined:
    Nov 9, 2011
    Messages:
    355
    Likes Received:
    0
    So, I understand registers, base/stack pointers, and the instructions. I can sort of walk through a program and somewhat tell what it is doing. But I have a terrible time translating this in to another language, like C.

    Any advice?
    Also, I have been using GDB to debug, is there a better debugger that will disassemble?
     
  2. Train

    Train Lifer

    Joined:
    Jun 22, 2000
    Messages:
    13,648
    Likes Received:
    1
    Might be better to start looking at it from the other direction. Write a simple method in C, compile it, and look at it in an Assembler. Sometimes the stuff the compiler comes up with will surprise you, and totally give you a different perspective.

    Try a recursive function, and watch how the assembly uses a stack to trace its steps up and down the function calls.
     
  3. degibson

    degibson Golden Member

    Joined:
    Mar 21, 2008
    Messages:
    1,389
    Likes Received:
    0
    Write more C; over time, you'll start to see the assembly pretty easily through the veneer of C, but only practice will get you there.
     
  4. Ken g6

    Ken g6 Programming Moderator, Elite Member
    Moderator

    Joined:
    Dec 11, 1999
    Messages:
    11,886
    Likes Received:
    70
    Try compiling your C code with gcc -S. That outputs the assembly code equivalent.
     
  5. Kromis

    Kromis Diamond Member

    Joined:
    Mar 2, 2006
    Messages:
    5,213
    Likes Received:
    0
    *Shudder* Assembly...that stuff gave me nightmares, yo! I never learned C either, it was just killing me.
     
  6. lambchops511

    lambchops511 Senior member

    Joined:
    Apr 12, 2005
    Messages:
    659
    Likes Received:
    0
    Learning assembly is useful for specific optimization (think SSE or other vector instructions if you don't like x86-64). If you need to deal with base/stack pointers you are likely coding in the wrong decade.
     
  7. degibson

    degibson Golden Member

    Joined:
    Mar 21, 2008
    Messages:
    1,389
    Likes Received:
    0
    Don't let the naive dissuade you, OP:
    - There's a huge value in understanding the machine at the level of base and stack pointers;
    - Similarly large value in understanding how high-level languages map to assembly-level constructs;
    - Not all platforms afford the luxury of high-level execution environments;
    - Programmers write compilers, don't they?
     
  8. ObscureCaucasian

    ObscureCaucasian Diamond Member

    Joined:
    Jul 23, 2006
    Messages:
    3,934
    Likes Received:
    0
    I very much agree, I personally think that a knowledge of how the code you write will be translated into assembly (at least at some level) is critical. Then again the higher-level language you work in, the less critical this may be, however I love embedded/systems programming, so I can't imagine _not_ understanding code at this level.
     
  9. exdeath

    exdeath Lifer

    Joined:
    Jan 29, 2004
    Messages:
    13,643
    Likes Received:
    0
    Use a IDE that shows interleaved C and corresponding assembly output during debug tracing/stepping. Disable optimizations. Have fun discovering that there are really only a handful of constructs that keep occurring over and over again in assembly (conditionals, for loops, do/while loops, etc).

    When you work in pure assembly, you'll naturally learn to comment everything and all the comments end up actually looking like an entire pseudo C program.

    My current platform of interest is the 6502 / NES. Very unique and challenging platform.

    Some excerpts:

    http://forums.anandtech.com/showthread.php?t=2271963
     
    #9 exdeath, Nov 4, 2012
    Last edited: Nov 4, 2012
  10. exdeath

    exdeath Lifer

    Joined:
    Jan 29, 2004
    Messages:
    13,643
    Likes Received:
    0
    WHAT? You mean I can't have Java on a system with 2k of RAM?! What do you mean I need 2 GB RAM just to write "Hello World!" ?
     
  11. degibson

    degibson Golden Member

    Joined:
    Mar 21, 2008
    Messages:
    1,389
    Likes Received:
    0
    #11 degibson, Nov 5, 2012
    Last edited: Nov 5, 2012
  12. Markbnj

    Markbnj Elite Member <br>Moderator Emeritus
    Moderator

    Joined:
    Sep 16, 2005
    Messages:
    15,684
    Likes Received:
    7
    Major-league agree with this.
     
  13. Cogman

    Cogman Lifer

    Joined:
    Sep 19, 2000
    Messages:
    10,086
    Likes Received:
    14
    Come now, Java isn't THAT bad. I mean, you should have a system with at least 64mb of memory before trying to do anything with it... but it isn't THAT bad. (Though, come to think of it, our current java solution will eat up around 8gb of ram with everything running and doing nothing... Yeah, not great, but at least I can delude myself into believing that it is a huge system).
     
  14. postmortemIA

    postmortemIA Diamond Member

    Joined:
    Jul 11, 2006
    Messages:
    7,513
    Likes Received:
    2
    I wouldn't want to read assembly code of i386 CPUs, compile it with gcc for PowerPC, which has much simpler instruction set.