在VC中使用組語
只需要在程式碼中加入下列的程式區塊
	__asm{
	...
	...
	}
注意:是雙底線(underline)

基本上這只是一種使用方法,並不建議如此使用,因為使得程式不具有portable。
除非真得迫切需要使用到這種方法,不然的話,還是利用C/C++語言本身去完成所需要的工作。

舉個例子:
::::::::::::::::::::::::::::::::::::::::::::::
//統計10000000次中,數值0~19出現的次數
#include <stdio.h> 
#include <stdlib.h>
#include <time.h>
unsigned long LongRandom();
int main()
{ 
	unsigned long index;
	unsigned long num[20] = { 0 } ;

	for(index=0;index<10000000;++index)
	{
		num[LongRandom() % 20]++;
	}

	for(index=0;index<20;++index)
	{
		printf("num [%2d] = %lu \n",index,num[index]);
	}
	return 0;
}

unsigned long LongRandom()
{
	static unsigned long seed = (unsigned long)time(NULL) ;
	unsigned long rand_number;
	__asm{
	mov eax,343FDh
	imul seed
	add eax,269EC3h
	mov seed,eax
	ror eax,8
	mov rand_number,eax;
	}
	return rand_number;
} 
::::::::::::::::::::::::::::::::::::::::::::::
1.__asm{...}
這一段程式是從《Assembly Language For Intel-Based Computers 4/E》中
P.446 
12.3.3 Example : Large Random Integers
擷取下來,此範例的內容是說,在C/C++中,rand()產生數值的範圍是0RAND_MAX(32,767)
,而這個LongRandom()函數可產生04,294,967,295。作者曾經透過這本書籍的作者所開設的討論區KIP R. IRVINE,如何產生這個亂數產生器呢?因為書中完全沒有說明,只有程式碼。而作者是這
麼說的:
Regarding books, I recommend Donald Knuth's famous book: The Art of Computer
Programming, Volume 2. He has a whole chapter on random number generation.

如果有興趣的話,可以去翻看看。
但注意一點,若在__asm{...}使用interrupt的話,會產生exception,可能是因為protected mode的原因
也可能是因為無法正確執行interrupt的關係,這目前作者仍不清楚。
請參考:
C++ Language Reference:Microsoft Specific Inline Assembler 
回目錄
Written By James On 2006/12/02 

Hosted by www.Geocities.ws

1