組合語言-019(彙編程式與c\c++相互呼叫)

萬事亨通發表於2020-12-27

1:在C++程式中使用__asm塊插入彙編程式碼程式(不能用LENGTHOF與SIZEOF運算子,而是LENGTH和SIZE)

struct Package
{
	long originZip;   //4
	long destinationzip;//4
	float shippingPrice; //4
};

int main(int argcount,char* args[])
{
	char myChar;
	bool myBool;
	short myShort;
	int myInt;
	long myLong;
	float myFloat;
	double myDouble;
	Package myPackage;

	long double myLongDouble;
	long myLongArray[10];

	__asm{
		mov eax, myPackage.destinationzip; 

		mov eax, LENGTH myInt;   //1
		mov eax, LENGTH myLongArray; / 10

	    mov eax, TYPE myChar;   //1
		mov eax, TYPE myBool;   //1
		mov eax, TYPE myShort;  //2

		mov eax, TYPE myInt;  //4
		mov eax, TYPE myLong; //4
		mov eax, TYPE myFloat;//4
		mov eax, TYPE myDouble;//8
		mov eax, TYPE myPackage;//12
		mov eax, TYPE myLongDouble; //8
		mov eax, TYPE myLongArray;//4
		

		mov eax, SIZE myLong;//4
		mov eax, SIZE myPackage;//12
		mov eax, SIZE myLongArray;//40
	}
	return 0;

}

2:使用c++程式呼叫匯編模組,在一個陣列中順序查詢一個元素

IndexOf.asm

.586
.model flat,C


IndexOf PROTO,
    srchVal:DWORD,arrayPtr:PTR DWORD,count:DWORD

.code
;32位整數陣列執行線性搜尋
;尋找指定的數值,如果發現匹配數值
;用EAX返回索引位置,否則返回-1
IndexOf PROC USES ecx esi edi,
    srchVal:DWORD,arrayPtr:PTR DWORD,count:DWORD

   NOT_FOUND = -1

   mov eax,srchVal  ;搜尋數值
   mov ecx,count ;陣列大小
   mov esi,arrayPtr;陣列指標
   mov edi,0  ;索引

L1:
  cmp [esi+edi*4],eax
  je found
  inc edi
  loop L1

notFound:
  mov eax,NOT_FOUND
  jmp exit
found:
  mov eax,edi

exit:
  ret 
IndexOf ENDP


END 

indexOf.h

extern "C" long IndexOf(long n, long array[], unsigned count);

main.cpp

#include <iostream>
#include <time.h>
#include "indexof.h"
using namespace std;

int main()
{
	//偽隨機數數填充陣列
	const unsigned ARRAY_SIZE = 100000;
	const unsigned LOOP_SIZE = 100000;
	char* boolstr[] = { "false", "true" };

	long array[ARRAY_SIZE];
	for (unsigned i = 0; i < ARRAY_SIZE; i++)
	{
		array[i] = rand();
	}

	long searchVal;
	time_t startTime, endTime;
	cout << "Enter an integer value to find: ";
	cin >> searchVal;
	cout << "Please wait...\n";

	//測試彙編函式
	time(&startTime);
	int count = 0;

	for (unsigned n = 0; n < LOOP_SIZE; n++)
		count = IndexOf(searchVal, array, ARRAY_SIZE);

	bool found = count != -1;

	time(&endTime);
	cout << "Elapsed ASM time:" << long(endTime - startTime)
		<< "seconds . Found = " << boolstr[found] << endl;
	return 0;

	
}

3: 在c++程式中呼叫匯編過程,彙編過程又呼叫C++函式, 實現提示使用者輸入整數,通過移位方式將其與2的冪相乘

ASM.asm

include Irvine32.inc

;外部c++函式
askForInteger PROTO C
showInt PROTO C,value:SDWORD,outWidth:DWORD
newLine PROTO C

OUT_WIDTH = 8
ENDING_POWER = 10

.data
intVal DWORD ?

.code
;設定文字顏色,並清除控制檯視窗
;呼叫Irvine32庫函式
SetTextOutColor PROC C,
                 color:DWORD
	mov eax,color
	call SetTextColor
	call Clrscr
	ret
SetTextOutColor ENDP

;輸入一個整數n並顯示範圍為N*21次方到N*210次方的乘法表
DisplayTable PROC C
    INVOKE askForInteger   ;呼叫C++函式
	mov intVal,eax         ;儲存整數
	mov ecx,ENDING_POWER    ;迴圈計數器
L1:
    push ecx          ;儲存計數器
	shl intVal,1  ;乘以2
	INVOKE showInt,intVal,OUT_WIDTH
	;等於INVOKE,
	;push OUT_WIDTH
	;push intVal
	;call showInt
	;add esp,8 C規範主調方一定要恢復棧
	call Crlf
	pop ecx
	loop L1
	ret
DisplayTable ENDP
END 

main.cpp

#include <iostream>
#include <iomanip>
using namespace std;

extern "C"
{
	//外部ASM過程
	void DisplayTable();
	void SetTextOutColor(unsigned color);

	//區域性C++函式
	int askForInteger();
	void showInt(int value,int width);
}

int main()
{
	SetTextOutColor(0x1E); //藍底黃字
	DisplayTable();
	return 0;
}

//提示使用者輸入一個整數
int askForInteger()
{
	int n;
	cout<<"Enter an integer between 1 and 90,000:";
	cin>>n;
	return n;
}

//按特定寬度顯示一個有符號整數
void showInt(int value,int width)
{
	cout<< setw(width)<<value;
}

4: 在彙編程式中呼叫C標準庫函式printf、scanf(必須從C或C++啟動程式中呼叫匯編語言程式碼)

ASM.asm

.386
.model flat,stdcall
.stack 2000

printf PROTO C ,format:PTR BYTE,args:VARARG
scanf PROTO C , format:PTR BYTE,args:VARARG

TAB =9
.data
strSingle BYTE "%lf",0
strDouble BYTE "%lf",0

formatTwo BYTE "%lf",TAB,"%lf",0dh,0ah,0
Single REAL8 ?
Double REAL8 ?

.code
asmMain PROC C
   INVOKE scanf,ADDR strSingle,ADDR Single
   INVOKE scanf,ADDR strDouble,ADDR Double

   ;傳遞給printf的浮點引數應宣告為REAL8,如果是REAL4,這需要相當的程式設計技巧
   INVOKE printf,ADDR formatTwo,Single,Double
    ret
asmMain ENDP
END

main.cpp

#include <iostream>
using namespace std;
extern "C" void asmMain();


int main()
{

	asmMain();
	return 0;
}

5:彙編過程實現一個陣列與一個整數相乘,c++程式傳遞陣列進行運算

ASM.asm

.586
.model flat,C


.code
arrayAdd PROC , array:PTR DWORD,count:DWORD,mulNum:DWORD
   mov esi, array
   mov ecx,count
L1:
   mov eax,[esi]
   mul mulNum
   mov [esi],eax
   add esi,4
   loop L1
   ret
arrayAdd ENDP
END 

main.cpp

#include <iostream>
using namespace std;

extern "C" void arrayAdd(int* array,int count,int mulNum);
int array[] = {1,2,3,4,5,6,7,8};
int main()
{
	arrayAdd(array,8,10);
	for(int i=0;i<8;i++)
	{
		cout<<array[i];
		cout<<endl;
	}
	return 0;
}

6: 編寫彙編子程式,接收陣列偏移量和陣列大小,子程式返回陣列中最長的遞增序列中整數值的個數

ASM.asm

.586
.model flat,C

.code
GetIncrementalList PROC USES ecx edi ebx esi,
               array:PTR SDWORD,count:DWORD

  mov eax,1
  mov edi,1
  mov ecx,count
  dec ecx
  mov esi,array
L0:
  mov ebx,[esi]
  cmp ebx,[esi+4]
  jge L1
  inc edi
  jmp L2
L1:
  cmp edi,eax
  jbe L2
  mov eax,edi
  mov edi,1
L2:
  add esi,4
  loop L0
 
 
  ret
GetIncrementalList ENDP
END 

main.cpp

#include <iostream>
using namespace std;

extern "C" int GetIncrementalList(int* array,int count);

int array[] ={-5,10,20,14,17,26,42,22,19,-5};
int main()
{
	int n =GetIncrementalList(array,10);
	return 0;
}

7:編寫彙編子程式,接收三個同樣大小的陣列,將第二個陣列與第三個陣列加到第一個陣列中

ASM.asm

.586
.model flat,C

.code
arraysAdd PROC ,array1:PTR DWORD,array2:PTR DWORD,array3 :PTR DWORD,count: DWORD
  
  mov ebx,array1
  mov esi,array2
  mov edi,array3
  mov ecx,count
L1:  
  mov eax,[esi]
  add eax,DWORD PTR[edi]
  mov [ebx],eax
  add esi,4
  add edi,4
  add ebx,4
  loop L1
  ret
arraysAdd ENDP
END 

main.cpp

#include <iostream>
using namespace std;

extern "C" void arraysAdd(int* array1,int* array2,int* array3,int count); 
int array1[10];
int array2[10]={1,2,3,4,5,6,7,8,9,10};
int array3[10]={1,2,3,4,5,6,7,8,9,10};
int main()
{
	arraysAdd(array1,array2,array3,10);
	for(int i =0;i<10;i++)
	{
		cout<<array1[i]<<endl;
	}
	return 0;
}

8:編寫彙編過程實現判斷是一個數是否是一個質數,是返回1否則返回0,C++將陣列每個元素進行判斷

isPrimeNumber.asm

.586
.model flat,C

.code
isPrimeNumber PROC USES ebx ecx edx, number:DWORD
  mov ebx,2
  cmp ebx,number
  je L2

  mov ecx,number
  jmp Start
L0:
  mov eax,ecx
  mov edx,0
  div ebx
  cmp edx,0
  je L1
  inc ebx
Start:
  cmp ebx,ecx
  jb L0
L2:
  mov eax,1
  jmp quit
L1:
  mov eax,0
quit:
  ret
isPrimeNumber ENDP
END 

main.cpp

#include <iostream>
using namespace std;

extern "C" int isPrimeNumber(int number);

int array[] = {2,3,4,5,6,7,8,9,10,11,12,13,500,967,968};
int main()
{
	for (int i = 0; i < 15; i++)
	{
		int result = isPrimeNumber(array[i]);
		if(result)
			cout<<array[i]<<"is a prime number."<<endl;
		else
			cout<<array[i]<<"is not a prime number."<<endl;
	}
	return 0;
}

9:編寫彙編過程從一個陣列的尾部開始順序查詢一個元素,返回其索引否則返回-1

myLastIndexOf.asm

.586
.model flat,C

.code
;32位整數陣列執行線性搜尋
;尋找指定的數值,如果發現匹配數值
;用EAX返回索引位置,否則返回-1
myLastIndexOf PROC USES ecx esi edi,
    srchVal:DWORD,arrayPtr:PTR DWORD,count:DWORD

   NOT_FOUND = -1

   mov eax,srchVal  ;搜尋數值
   mov ecx,count ;陣列大小
   mov esi,arrayPtr;陣列指標
   lea edi,[ecx - 1]  ;索引

L1:
  cmp [esi+edi*4],eax
  je found
  dec edi
  loop L1

notFound:
  mov eax,NOT_FOUND
  jmp exit
found:
  mov eax,edi

exit:
  ret 
myLastIndexOf ENDP


END 

myLastIndexOf.h

extern "C" long myLastIndexOf(long n, long array[], unsigned count);

main.cpp

#include <iostream>
#include "myLastIndexOf.h"
using namespace std;


long array[10] ={1,2,3,4,5,6,7,8,9,10};

int main()
{

    int index = myLastIndexOf(3, array, 10);
	cout<<index;
	return 0;

	
}

相關文章