Inline Assembly Code

Most high skilled developers do not write large-scale applications in MASM, simply because it takes time. It slows down the development honestly. MASM used widely to access hardware devices and of course, for optimization in both space and runtime speed when you have memory restricted condition.

 

Let say you have some memory restricted (Euclid as illustration) in your C++ application like this:

 

int euclid(int x, int y)

{

  int t;

  do

  {

    if (x

    {

            t=x;

            x=y;

            y=t;

    }

    x -= y;

  } while (x);

  return y;

}

 

You can implement the algorithm using ASM as well like below:

 

.386

.model flat, c

.code

 

euclid PROC x:SDWORD, y:SDWORD

  mov ecx, x

  mov eax, y

@@:

  cmp ecx, eax

  jge noswap

  xchg eax, ecx

noswap:

  sub ecx, eax

  jnz @B

  RET

euclid ENDP

end

 

Then you have a choice to replace C/C++ part of code with ASM using inline assembly, or compile ASM to object for later linked to C/C++ main program.

 

In this post, I will give you a tour to see how to write inline assembly code in C++ and later (if I have enough time, awaiting Raffy ask milk), we will see how to link assembly modules to C++ program. The MASM provides you with several advantages over inline assembly in your C/C++ codes. MASM contains a macro language with looping, arithmetic, text string processing, and so on, and MASM supports the instruction sets of the 386, 486, and Pentium processors (including x64, ML64.EXE), providing you with greater direct control over the hardware. You also can avoid extra time and memory overhead when using MASM.

 

Calling assembly language from higher level language needs some general considerations. First, naming convention and the second one is memory model. Calling convention refers to the low-level details about how procedures are called. This will be related to registers, stack, shared memory, argument passing model, stack pointer restore model, etc. In other words, you have to fully understand your assembly codes before interfacing it. A calling program and a called procedure must both use the same memory model whether it is in real-address mode or protected mode with flat memory model.

 

So let start with the inline assembly codes. Inline means in the same file with native C/C++ codes. Most compilers support this features. Writing inline code is for simplicity, no external link issues, naming problems, and parameter passing protocols to worry about. On other side, inline assembly is lack of portability even if you have conditional definitions per processor type. Note that inline ASM is not supported for x64.

 

In VC++, the __asm (two underline) directive can be placed at the beginning of single statement, or asm block. The syntax is :

 

__asm statement

__asm

{

            statement-1

            statement-2

           

            statement-n

}

 

Not everything you can do with inline assembly. You cannot make any assumptions about registers values at the beginning of asm block. You have to avoid using __fastcall and __asm together. Be careful with register modification, because it is impossible for any compiler to fully optimize the C++ code in the same procedure. Optimization requires the use of registers.

 

I will post you a real example later. Now is my time to make 120 ml milk for Raffy.

 

Ciao ! 

Share this post: | | | |
Published Monday, July 31, 2006 8:58 PM by Risman Adnan Mattotorang
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: