2.5 OpenEuler 中C与汇编的混合编程

在X86_64架构下实践2.5中的内容:

## 1.在Ubuntu系统下实践2.5中的内容 ##

(1)将C代码编译成汇编代码

代码如下:a.c

#include<stdio.h>
extern int B();
int A(int x,int y)
{
int d,e,f;
d=4;e=5;f=6;
f=B(d,e);
}

汇编代码如下:a.s

![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205230454941-1401739817.jpg)

(2)用汇编语言实现函数

1.获取CPU寄存器

s.s
![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205230615359-318359246.png)

hello.c
![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205230827138-1221625878.png)

makefile
hello:hello.o s.o
gcc -o hello hello.o s.o
hello.o:hello.c
gcc -c hello.c
s.o:s.s
gcc -c s.s
clean:
rm hello hello.o s.o

编译运行截图

![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205231019591-1693628239.jpg)

## 2.用汇编语言编写sum()函数 ##
mysum.s
![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205231327139-650306971.png)

main.c
![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205231501911-1548366792.png)

makefile
![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205231606102-2013266619.png)
编译运行截图:
![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205231056476-151029161.jpg)

3.从汇编中调用C函数
sub.s sub,c
![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205231815964-806755724.png)

编译运行截图:
![](https://img2020.cnblogs.com/blog/1812365/202112/1812365-20211205231108805-2113786860.jpg)

posted @ 2021-12-05 21:27  20191214  阅读(29)  评论(0编辑  收藏  举报