Mixing C/C++ Code with x86 Assembly Code in Visual C++

When you need to perform low-level programming or speed demanding projects, you can consider mixing x86 ASM code within your C/C++ source code by using the __asm keyword. Using ASM in Visual C++ has advantages over using assembler such as the Microsoft assembler, such as:

The __asm keyword can be used in two ways:

__asm mov ax,bx
__asm add ax,cx

or

__asm {
	mov ax,bx
	add ax,cx

}

Here is an example of using C variables in the ASM code:

#include <windows.h>
#include <stdio.h>
#ifndef NULL
#define NULL 0
#endif
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
short int i;
char * txt;
txt = (char *)malloc(256);
i = 0;
sprintf(txt,"i = %d",i);
MessageBox(0,txt,"",MB_OK);

__asm {
pusha
mov ax,i
add ax,10
mov i,ax
popa
} sprintf(txt,"i = %d",i); MessageBox(0,txt,"",MB_OK); return 0; }
: : go back : :