[C++/C/ASM][tutorial] __asm in C++/C. Basics.

Results 1 to 8 of 8
  1. #1
    Enthusiast Charlie is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    [C++/C/ASM][tutorial] __asm in C++/C. Basics.

    Written by me (Matek,registered as Charlie on RaGEZONE) on 25th June 2008 ;)

    Here it is, my first tutorial.

    Most of you have seen a lot of codes and scripts containing '__asm'. You
    could wonder what it is for in a C++ code.
    The answer is simple, it's called 'inline assembly' and is used to extend
    power and abilities of regular C++ applications. That's enough for an introduction I guess.
    MSDN says it all:
    Because the assembly code can be mixed inline with C or C++ statements, it
    can do tasks that are cumbersome or impossible in C or C++.

    I'm not going to explain assembly in no way here. I want to show you the
    way to use asm in C++ code and how to mix them up.

    Let's move on.

    1. Syntax.

    As you can see below there's nothing much to do. Type __asm and put assembly
    code between curly braces { }.

    Code:
    __asm {
    
    ...code...
    
    }
    You could start every line (or instruction) with __asm but it's senseless
    unless you're using a single instruction ( mov eax, ebx ). It would look
    like this:

    Code:
    __asm ...code... 
    __asm ...code...
    
    //or...
    
    __asm ...code...  __asm ...code... 
    
    //senseless no?
    
    
    
    //The code below is fine
    
    __asm add eax,ebx
    
    cout << "We used a single instruction.";
    It's not hard to notice that doing it this way is just a waste of your time
    and there's a possibility that you forget to start every line with it.

    2. Mixing C++/C with __asm.

    In __asm block of code you can use ALMOST all C++/C symbols. This means
    that you can use variable names, function names and labels within __asm block.
    However you have to keep in mind that symbols' names cannot be the same as
    MASM reserved words such as instruction names (eg PUSH). All functions
    you are referring to must be declared before the beginning of __asm block.

    3. C++/C data in __asm.

    A great convenience of inline assembly is the ability to refer to C or C++ v
    ariables by name. The easiest way to describe it is using an example so here it is:

    Code:
    int VariableStoringMyAge;
    VariableStoringMyAge = 15;
    
    //Here a short one-line __asm block starts and look! Our int variable is 
    right there. It adds VariableStoringMyAge's value (15) to eax and stores 
    the sum in it.
    
    __asm add eax,VariableStoringMyAge
    I hope it's clear and easy to understand.

    NOTE: However (nothing can be wonderful ;( ) if any class or structure
    share the same member name (it's not unique because
    there are two of them), you must place a variable or typedef name right
    before the period (.) operator. No example because it's only a note! (:p)

    I think it's enough for my first tutorial and it explains the basics.
    Now go learn assembly and use it in your C++/C codes :)

    yours nefykenny/Matek.

    Credits:
    Me.
    MSDN.
    Google.

    Feel free to evaluate but don't you dare to
    flame me!


  2. #2
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [C++/C/ASM][tutorial] __asm in C++/C. Basics.

    Nice work ;), added the [tutorial] tag.

    I'm getting into ASM too, lately. Pretty hard to grasp it in the beginning, but you can build some efficient things with it ;)

  3. #3
    Enthusiast Charlie is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    Re: [C++/C/ASM][tutorial] __asm in C++/C. Basics.

    Thanks ;)

    That's true. It's very good on its own but added to C++ works as a nice addon and gives the C language more abilities as mentioned in the tut.

  4. #4
    Omega penihop is offline
    MemberRank
    Sep 2006 Join Date
    SpamzoneLocation
    5,569Posts

    Re: [C++/C/ASM][tutorial] __asm in C++/C. Basics.

    some more asm xD:

    JMP - go to offset / jump
    MOV- save value
    What are the most used Registers ?
    EAX,EBX,ECX,EDX,EBP,ESI,EDI
    what is EIP ? - Program Entry Point (where program starts)
    saving value example:
    MOV EAX,1
    now eax is 1
    MOV EAX,5 - eax is 5
    MOV EBX,EAX - now EBX is 5
    MOV ECX,EBX - now ECX is 5 ;D
    JMP 0x00000001 - example of jumping to offset

    note - i dont know if this will work with the inline asm in VC++

  5. #5
    Enthusiast Charlie is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    Re: [C++/C/ASM][tutorial] __asm in C++/C. Basics.

    Such basics should do.

    You are partially wrong. MOV isn't 'save value' only.

    Code:
    mov eax,ebx
    // it moves ebx to eax and saves it in eax.

  6. #6
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [C++/C/ASM][tutorial] __asm in C++/C. Basics.

    Quote Originally Posted by penihop View Post
    some more asm xD:

    JMP - go to offset / jump
    MOV- save value
    What are the most used Registers ?
    EAX,EBX,ECX,EDX,EBP,ESI,EDI
    what is EIP ? - Program Entry Point (where program starts)
    saving value example:
    MOV EAX,1
    now eax is 1
    MOV EAX,5 - eax is 5
    MOV EBX,EAX - now EBX is 5
    MOV ECX,EBX - now ECX is 5 ;D
    JMP 0x00000001 - example of jumping to offset

    note - i dont know if this will work with the inline asm in VC++
    Depends on which compiler and processor you use, using MingW32 I have different commands.

  7. #7
    Omega penihop is offline
    MemberRank
    Sep 2006 Join Date
    SpamzoneLocation
    5,569Posts

    Re: [C++/C/ASM][tutorial] __asm in C++/C. Basics.

    Quote Originally Posted by Charlie View Post
    Such basics should do.

    You are partially wrong. MOV isn't 'save value' only.

    Code:
    mov eax,ebx
    // it moves ebx to eax and saves it in eax.

    btw, why u type 2 times " _ " ? whats the difference between 1 _ and 2" __ "
    i do like this
    _asm
    {
    //code
    }

  8. #8
    Enthusiast Charlie is offline
    MemberRank
    Apr 2008 Join Date
    38Posts

    Re: [C++/C/ASM][tutorial] __asm in C++/C. Basics.

    __asm is the present version of asm. However:
    Quote Originally Posted by MSDN
    For compatibility with previous versions, _asm is a synonym for __asm.
    Simply saying, _asm is an older version of __asm and newer version of asm (without underscores).



Advertisement