彙編和c函數交叉混合調用

有轉載的話希望能尊重原創,謝謝各位!

以下運行在Ubuntu環境下,需要安裝qemu-user,使用arm-linux-gcc編譯。

////////////////////////////////////////////////////////

////////c文件中調用匯編文件中的彙編“函數”

////////////////////////////////////////////////////////

//b.c

#include<stdio.h>

extern int sum(int a,int b);

int main()
{
	int n;
	n=sum(10,12);
	printf("--%d--\n",n);
}

//a.s

.text

.global sum

sum:

mov r3,r0
add r3,r1
mov r0,r3
mov pc,lr	;#  lr即r14

.end


//編譯和執行結果
ubuntu@ubuntu:~/Documents$ 
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o a.o a.s
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o b.o b.c
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -static -o xx a.o b.o
ubuntu@ubuntu:~/Documents$ qemu-arm xx
--22--
ubuntu@ubuntu:~/Documents$ 





////////////////////////////////////////////////////////
////////彙編文件調用c文件中的函數
////////////////////////////////////////////////////////

//a.s

.text

.global main	;#主函數入口
.global fun1
.global fun2

main:
	bl fun1
	bl fun2

halt_loop:
	b halt_loop

.end

//b.c

#include<stdio.h>

extern void fun1(void);
extern void fun2(void);

void fun1(void)
{
	printf("zhong\n");
}

void fun2(void)
{
	printf("kun jiang\n");
}

//編譯和執行結果
ubuntu@ubuntu:~/Documents$ 
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o a.o a.s
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o b.o b.c
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -static -o xx a.o b.o
ubuntu@ubuntu:~/Documents$ qemu-arm xx
zhong
kun jiang
^C
ubuntu@ubuntu:~/Documents$ 




///////////////////////////////////////////////////////////////////
////////c文件中定義全局(外部的)變量,彙編文件中調用該變量
///////////////////////////////////////////////////////////////////

//a.s

.text

.global main	;#主函數入口
.global fun1
.global fun2

.global glbval


main:
	mov r0,#100
	ldr r1,=glbval
	str r0,[r1]
	bl fun1
	
	mov r0,#200
	ldr r1,=glbval
	str r0,[r1]
	bl fun2

halt_loop:
	b halt_loop

.end

//b.c

#include<stdio.h>

extern void fun1(void);
extern void fun2(void);

extern int glbval;
int glbval;

void fun1(void)
{
	printf("fun1():glbval=%d\n",glbval);
}

void fun2(void)
{
	printf("fun2():glbval=%d\n",glbval);
}

//編譯和執行結果
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o a.o a.s
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o b.o b.c
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -static -o xx a.o b.o
ubuntu@ubuntu:~/Documents$ qemu-arm xx
fun1():glbval=100
fun2():glbval=200
^C
ubuntu@ubuntu:~/Documents$ 


////////////////////////////////////////////////////////////////////////////
///彙編文件調用c文件中的函數,c文件中的函數帶參數,實現彙編到c的參數傳遞
////////////////////////////////////////////////////////////////////////////

//a.s

.text

.global main	;#主函數入口
.extern fun1
.global fun2

.global glbval


main:
	mov r0,#100
	ldr r1,=glbval
	str r0,[r1]
	bl fun1
	
	mov r0,#200
	ldr r1,=glbval
	str r0,[r1]
	bl fun2

	mov r0,#10	;#r0:a
	mov r1,#12	;#r1:b
	mov r2,#14	;#r2:c
	bl sum
	
	ldr r1,=glbval
	str r0,[r1]
	bl fun1
	
halt_loop:
	b halt_loop

.end

//b.c

#include<stdio.h>

extern void fun1(void);
extern void fun2(void);
extern int sum(int a,int b,int c);

extern int glbval;
int glbval;

void fun1(void)
{
	printf("fun1():glbval=%d\n",glbval);
}

void fun2(void)
{
	printf("fun2():glbval=%d\n",glbval);
}

int sum(int a,int b,int c)
{
	int n;
	n=a+b+c;
	return n;
}

//編譯和執行結果
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o a.o a.s
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -c -o b.o b.c
ubuntu@ubuntu:~/Documents$ arm-linux-gcc -static -o xx a.o b.o
ubuntu@ubuntu:~/Documents$ qemu-arm xx
fun1():glbval=100
fun2():glbval=200
fun1():glbval=36
^C
ubuntu@ubuntu:~/Documents$ 







發佈了35 篇原創文章 · 獲贊 26 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章