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*2的1次方到N*2的10次方的乘法表
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;
}