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;}

汇编语言-019(汇编程序与c\c++相互调用)相关推荐

  1. C语言与汇编语言相互调用原理以及实例

    下面两个分别是一个foo.asm(汇编语言文件),bar.c(c语言文件) 首先来了解C语言为什么能调用汇编语言,以及汇编语言为什么能调用C语言.其实不管是C语言还是汇编语言想要执行都是最终编译链接成 ...

  2. 嵌入式:ARM内嵌汇编及C和ARM汇编相互调用

    内嵌汇编 在C程序中嵌入汇编程序可以实现一些高级语言没有的功能,并可以提高执行效率.armcc和armcpp内嵌汇编器支持完整的ARM指令集:tcc和tcpp用于Thumb指集.但是内嵌汇编器并不支持 ...

  3. python模块--如何相互调用自己写的模块

    一.模块相互调用同级目录调用时的两种方法 1 import module 2 print(module.add(3,8)) 3 4 from module import add 5 print(add ...

  4. 转载 iOS js oc相互调用(JavaScriptCore) --iOS调用js

    iOS js oc相互调用(JavaScriptCore) 从iOS7开始 苹果公布了JavaScriptCore.framework 它使得JS与OC的交互更加方便了. 下面我们就简单了解一下这个框 ...

  5. python和c++的相互调用教程

    日常工作中会遇到需要python与cpp代码之间的相互调用,工作的应用复杂,都是取决于代码的多少,但是总的方法不变,这里用两个简单例子说明下,有兴趣的筒子可以探讨下~~ 我的测试环境:ubuntu16 ...

  6. Objective-C学习笔记(十九)——对象方法和类方法的相互调用

    事实上在OC的对象方法(减号方法)和类方法(加号方法)并非相互独立的,它们也能够发生千丝万缕的关系,今天我们来研究下它们两者相互调用的问题.该样例还是以People类为基础. (一)对象方法调用类方法 ...

  7. 关于cocos2d-x 和安卓之间的相互调用

    最近在研究cocos2d游戏移植安卓需要调用很多方法,所以在研究之中写下它们之间相互调用 首先,cocos2d调用安卓 在一个.h文件中添加头文件 #include <jni.h> #in ...

  8. iOS架构-多工程联编及framework之间的相互调用(19)

    对于大公司,大工程来说,业务线很多,也时刻在变,功能模块要求能随时下线,或者业务不再需要了,就需要从主工程中移除相关工程或者库.以减小包的大小.多工程联编是一种多业务合作的一种方法. 有篇文章写的很详 ...

  9. Python实例浅谈之三Python与C/C++相互调用

    参考:http://blog.csdn.net/taiyang1987912/article/details/44779719 Python实例浅谈之三Python与C/C++相互调用 二.Pytho ...

最新文章

  1. 三菱工业机器人rv6s_FANUC机器人控制器—维护三要素
  2. Tomcat7.0.26的连接数控制bug的问题排查
  3. 【C/C++开发】C++实现简单的线程池
  4. 二进制bit0是什么意思_模拟信号是什么 模拟信号数字传输原理介绍【图文】
  5. 教你从零搭建Web漏洞靶场OWASP Benchmark
  6. “五一”档总票房破13亿 《你的婚礼》高居榜首
  7. SQL Server 2005 14420 14421错误
  8. 深圳手机厂家逐渐倾向谷歌Android手机
  9. python工资一般多少西安-西安Python和人工智能的薪资前景到底怎么样?
  10. 51 Nod 1005 大数加法【Java大数乱搞,python大数乱搞】
  11. 鲁大师最新笔记本排行榜,微星 Raider GE76性能最强
  12. Alink(1):Alink概述
  13. 手机Root与刷机教程
  14. 《Python基础教程》PDF
  15. 天气预报API接口大全
  16. python通过手机拍摄的视频图片进行人脸头像采集
  17. python退出交互_python交互界面的退出方法
  18. 使用Python扩展库numpy中的piecewise()函数实现分段函数模拟兔子的行走轨迹,然后使用matplotlib.pyplot中的plot函数绘制折线图表示兔子和乌龟的时间位移图,并添加坐标
  19. JustLaws 法律文库贡献指南
  20. table表头和首列的表格固定-CSS实现的Table表头固定

热门文章

  1. Css fade()函数降低颜色变量透明度
  2. initial、inherit、unset、revert和all
  3. NativeScript - JS 构建跨平台的原生 APP
  4. 游戏大厅 从基础开始(6)--绕回来细说聊天室(中)之女仆编年史1
  5. 题解 P2598 【[ZJOI2009]狼和羊的故事】
  6. STL sector 应用
  7. Ubuntu中输入输出重定向及管道技术简述
  8. 更改密码 sp_password
  9. Notepad++高亮AS文件
  10. 更改span标签样式_CSS 内嵌样式