1:在C++程序中使用__asm块插入汇编代码程序(不能用LENGTHOF与SIZEOF运算符,而是LENGTH和SIZE)
struct Package
{long originZip; //4long destinationzip;//4float 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; //1mov eax, LENGTH myLongArray; / 10mov eax, TYPE myChar; //1mov eax, TYPE myBool; //1mov eax, TYPE myShort; //2mov eax, TYPE myInt; //4mov eax, TYPE myLong; //4mov eax, TYPE myFloat;//4mov eax, TYPE myDouble;//8mov eax, TYPE myPackage;//12mov eax, TYPE myLongDouble; //8mov eax, TYPE myLongArray;//4mov eax, SIZE myLong;//4mov eax, SIZE myPackage;//12mov eax, SIZE myLongArray;//40}return 0;}
2:使用c++程序调用汇编模块,在一个数组中顺序查找一个元素
IndexOf.asm
.586
.model flat,CIndexOf PROTO,srchVal:DWORD,arrayPtr:PTR DWORD,count:DWORD.code
;对32位整数数组执行线性搜索
;寻找指定的数值,如果发现匹配数值
;用EAX返回索引位置,否则返回-1
IndexOf PROC USES ecx esi edi,srchVal:DWORD,arrayPtr:PTR DWORD,count:DWORDNOT_FOUND = -1mov eax,srchVal ;搜索数值mov ecx,count ;数组大小mov esi,arrayPtr;数组指针mov edi,0 ;索引L1:cmp [esi+edi*4],eaxje foundinc ediloop L1notFound:mov eax,NOT_FOUNDjmp exit
found:mov eax,ediexit:ret
IndexOf ENDPEND
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 COUT_WIDTH = 8
ENDING_POWER = 10.data
intVal DWORD ?.code
;设置文本颜色,并清除控制台窗口
;调用Irvine32库函数
SetTextOutColor PROC C,color:DWORDmov eax,colorcall SetTextColorcall Clrscrret
SetTextOutColor ENDP;输入一个整数n并显示范围为N*2的1次方到N*2的10次方的乘法表
DisplayTable PROC CINVOKE askForInteger ;调用C++函数mov intVal,eax ;保存整数mov ecx,ENDING_POWER ;循环计数器
L1:push ecx ;保存计数器shl intVal,1 ;乘以2INVOKE showInt,intVal,OUT_WIDTH;等于INVOKE,;push OUT_WIDTH;push intVal;call showInt;add esp,8 C规范主调方一定要恢复栈call Crlfpop ecxloop L1ret
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 2000printf PROTO C ,format:PTR BYTE,args:VARARG
scanf PROTO C , format:PTR BYTE,args:VARARGTAB =9
.data
strSingle BYTE "%lf",0
strDouble BYTE "%lf",0formatTwo BYTE "%lf",TAB,"%lf",0dh,0ah,0
Single REAL8 ?
Double REAL8 ?.code
asmMain PROC CINVOKE scanf,ADDR strSingle,ADDR SingleINVOKE scanf,ADDR strDouble,ADDR Double;传递给printf的浮点参数应声明为REAL8,如果是REAL4,这需要相当的编程技巧INVOKE printf,ADDR formatTwo,Single,Doubleret
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:DWORDmov esi, arraymov ecx,count
L1:mov eax,[esi]mul mulNummov [esi],eaxadd esi,4loop L1ret
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:DWORDmov eax,1mov edi,1mov ecx,countdec ecxmov esi,array
L0:mov ebx,[esi]cmp ebx,[esi+4]jge L1inc edijmp L2
L1:cmp edi,eaxjbe L2mov eax,edimov edi,1
L2:add esi,4loop L0ret
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: DWORDmov ebx,array1mov esi,array2mov edi,array3mov ecx,count
L1: mov eax,[esi]add eax,DWORD PTR[edi]mov [ebx],eaxadd esi,4add edi,4add ebx,4loop L1ret
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:DWORDmov ebx,2cmp ebx,numberje L2mov ecx,numberjmp Start
L0:mov eax,ecxmov edx,0div ebxcmp edx,0je L1inc ebx
Start:cmp ebx,ecxjb L0
L2:mov eax,1jmp 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;elsecout<<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:DWORDNOT_FOUND = -1mov eax,srchVal ;搜索数值mov ecx,count ;数组大小mov esi,arrayPtr;数组指针lea edi,[ecx - 1] ;索引L1:cmp [esi+edi*4],eaxje founddec ediloop L1notFound:mov eax,NOT_FOUNDjmp exit
found:mov eax,ediexit:ret
myLastIndexOf ENDPEND
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;}