+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: Intro to Intel Assembly Language: Part 1

  1. #1
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Intro to Intel Assembly Language: Part 1

    Just a quick little tutorial on getting started writing assembly language for Intel and Intel-compatible (i.e. AMD) processors. This will cover IA-32 only for now.

    First thing you need to know about the processor is that you don't really have a lot to work with - you can't just create your own variables and use them at will. Fortunately, you're given some very fast little variables called registers. Some of them are general-purpose, some are dedicated to performing a specific task. There are six 32-bit registers you can use for almost anything, as well as two more that are dedicated to use on the stack (I'll get to that later):

    EAX, EBX, ECX, EDX, ESI, EDI

    To retain backwards compatibility with 16-bit predecessors, the above 32-bit registers have the low 16 bits aliased to these registers:

    AX, BX, CX, DX, SI, DI

    If you were to store 0xCAFEBABE* in EAX, then the value of AX would be 0xBABE. Changing AX to 0xDEAD means that EAX now contains the value 0xCAFEDEAD.

    The registers AX, BX, CX, and DX are further subdivided into two registers each. So AX is composed of AH and AL, aliased to the high and low bytes of the word respectively. (E)SI and (E)DI don't do this because they're intended for use as pointer registers, but you can use them for anything, really.

    Perhaps an example in C++ would help:
    Code:
    uint32_t eax;
    
    //AX points to low word of EAX. Since Intel processors
    //are little-endian, we don't have to do any pointer
    //arithmetic.
    uint16_t& ax = (uint16_t *)&eax;
    
    //same for AL
    uint8_t& al = (uint8_t *)&ax;
    
    //AH points to the HIGH byte, so we have to add one
    //byte to get the proper offset into AX.
    uint8_t& ah = ((uint8_t *)&ax) + 1;
    Well, this is all well and good, but how does one do anything to these registers? The most common command is mov. Take a look:

    (By the way, a semicolon starts a comment, just like // in C++.)
    Code:
    ;eax = 0xCAFEBABE
    mov    eax, 0xCAFEBABE
    
    ;ecx = eax
    mov    ecx, eax
    
    ;edx = ax
    ;WRONG - DIFFERENT SIZE
    mov    edx, ax
    
    ;al = 0x0BAD
    ;WRONG - DIFFERENT SIZE
    mov    ax, 0x0BAD
    Unlike higher-level programming languages, you are not allowed to assign a small register to a larger one. The source and destination must be the same size.

    There is a trick to getting around this by using the movzx and movsx instructions. movzx clears all the upper bits in the target register, and movsz copies the sign bit over.

    Code:
    unsigned long a;
    unsigned short b = 5;
    //movzx    a, b
    //now a = 5.
    
    signed long c;
    signed short d = -4;
    // movsx    c, d
    //now c = -4.
    //if we were to use movzx then c would equal 65,532 - oops.
    Right now you can't accomplish much like this, but later on I'll show you how to read and write from RAM, and do some arithmetic.

    *The magic number for compiled Java class files.

    Next In This Series
    Intro to Intel Assembly Language: Part 2
    Last edited by dargueta; 11-30-2010 at 12:18 PM.
    sudo rm -rf /

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Intro to Intel Assembly Language: Part 1

    A nice, gentle introduction! Perfect. +rep

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Intro to Intel Assembly Language: Part 1

    Thanks! I've got more coming soon. I think.
    sudo rm -rf /

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Intro to Intel Assembly Language: Part 1

    Nicely done. From the bits I've seen, assembly isn't hard, but you really don't get all the comforts you're used to in "high-level" languages like C
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Intro to Intel Assembly Language: Part 1

    No, you don't, but I like to ride on the edge anyway.
    sudo rm -rf /

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Intro to Intel Assembly Language: Part 1

    speed and power. Gotta love it.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Intro to Intel Assembly Language: Part 1

    ...premature hair loss and long nights in with a cup of Ramen...
    Last edited by dargueta; 11-30-2010 at 12:19 PM.
    sudo rm -rf /

  9. #8
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Intro to Intel Assembly Language: Part 1

    I cant wait for the next one, Assembly is one of those things that have always been on the end of my to-do list.

    Is Assembly different per processor?

  10. #9
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Intro to Intel Assembly Language: Part 1

    Yes and no. The instruction set for the Intel 8086 was incredibly small, only about 256 instructions or so. The modern Intel instruction set has over 400. Changes in architecture also affect the language, i.e. adding new registers, new memory addressing modes, changing the behavior of functions, etc. Because it's so highly reliant on the processor architecture, there can't be a single assembly language. Due to backwards compatibility and legacy emulation modes, you can write a program for an 80386 and it'll (probably) run on a Pentium IV with no problems (disregarding the operating system, of course). The language is also typically compatible across a generation (sometimes even more), so you can write a program for a Xeon and expect it to run on a Celeron unmodified. The differences, if any, are usually minor, and usually just involves adding an instruction or three.
    sudo rm -rf /

  11. #10
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: Intro to Intel Assembly Language: Part 1

    I would dare to say that assigning variables is the most basic and important statement you can do in C#. Therefore the fact that your tutorial is based on mov instructions seems to be... a very good idea. +rep

    Do you think that writing most desktop applications and even operating systems will be become a good idea? .NET and Mono assemblies are always compiled into native code eventually, with the highest (best) instruction set available.

+ Reply to Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intro to Intel Assembly Language: Part 9A
    By dargueta in forum Assembly Tutorials
    Replies: 4
    Last Post: 08-16-2010, 09:06 AM
  2. Intro to Intel Assembly Language: Part 8
    By dargueta in forum Assembly Tutorials
    Replies: 6
    Last Post: 07-15-2010, 09:21 AM
  3. Intro to Intel Assembly Language: Part 5
    By dargueta in forum Assembly Tutorials
    Replies: 6
    Last Post: 03-07-2010, 12:45 PM
  4. Intro to Intel Assembly Language: Part 7
    By dargueta in forum Assembly Tutorials
    Replies: 5
    Last Post: 12-30-2009, 02:17 PM
  5. Intro to Intel Assembly Language: Part 6
    By dargueta in forum Assembly Tutorials
    Replies: 3
    Last Post: 12-14-2009, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts