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 { }.
You could start every line (or instruction) with __asm but it's senselessCode:__asm { ...code... }
unless you're using a single instruction ( mov eax, ebx ). It would look
like this:
It's not hard to notice that doing it this way is just a waste of your timeCode:__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.";
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:
I hope it's clear and easy to understand.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
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!



Reply With Quote![[C++/C/ASM][tutorial] __asm in C++/C. Basics.](http://ragezone.com/hyper728.png)


