1.C语言调用汇编
程序的入口是main,在main里调用汇编的函数。
在C语言中,要extern 一个函数声明即可,然后这个函数在汇编里面实现。
在汇编里面,用EXPORT 把C语言定义的函数名引进来,再开始编写函数名开始的段
#includeextern int sum(int a,int b,int c,int d,int e,int f); int main(){ int result = sum(1,2,3,4,5,6); return 0; }
汇编代码可新建一个sum.asm文件,在工程中添加这个文件即可。
AREA EXAMPLE,CODE,READONLY
EXPORT sum ENTRY sum ADD R0,R0,R1 ADD R2,R2,R3 ADD R0,R0,R2 LDR R4,[SP] LDR R5,[SP,#4] ADD R4,R4,R5 ADD R0,R4,R0 BX LR END
函数传参:
4个以内的参数,直接存放在R0~R3 这4个寄存器里面。
4个以后的参数放在堆栈里。
如果函数有返回值,那么返回值放在R0里。
Debug看看编译器是怎么处理的,我们就怎样把参数取出来。
还是比较易懂
首先是
R0 =6 R1=5 R2=4 R3=3
然后把R1的值放在堆栈里,R13=5,然后R1=2
然后吧R0的值放在堆栈的下一个位置,R13+4=6,然后R0=1.
之后就是:
R0=1,R1=2,R2=3,R3=4
堆栈里面:
所以取参数的时候就是:
LDR R4,[SP];R4=5 LDR R5,[SP,#4];R5=6
把相加的结果放在R0,然后BX LR返回
可以看到:
可以验证的确R0是存放返回值的。
例子:
;C语言中extern void macRM68090_WR_CMD(int cmd); R0 为cmd
AREA EXAMPLE,CODE,READONLY
EXPORT macRM68090_WR_CMD
ENTRY
macRM68090_WR_CMD
;macRM68090_RS_CLR;macRM68090_CS_CLR
LDR R1, =0x30
ldr r4, =0x40010C14
STR R1, [r4] ; GPIOB->BRR = 0x10; GPIOB->BRR = 0x20;
;macRM68090_8BIT_DATAOUT(cmd&0xFF00);
AND R1, R0, #0xFF00
LDR R3, =0x40010C0C
AND R3, R3, #0X00FF
ORR R2, R1, R3
ldr r4, =0x40010C0C
STR R2, [r4]
;macRM68090_WR_CLR;macRM68090_WR_SET
LDR R1, =0x08
ldr r4, =0x40010C14
STR R1, [r4] ;GPIOB->BRR = 0x08;
ldr r4, =0x40010C10
STR R1, [r4] ;GPIOB->BSRR = 0x08;
;macRM68090_8BIT_DATAOUT(((cmd<<8)&0xFF00));
MOV R2, R0, LSL#8
AND R2, R2, #0xFF00
LDR R3, =0x40010C0C
AND R3, R3, #0x00FF
ORR R2, R0, R3
ldr r4, =0x40010C0C
STR R2, [r4]
;macRM68090_WR_CLR ;
ldr r4, =0x40010C14
STR R1, [r4] ; GPIOB->BRR = 0x08;
;macRM68090_WR_SET ;macRM68090_CS_SET
LDR R1, =0x28
ldr r4, =0x40010C10
STR R1, [r4] ; GPIOB->BSRR = 0x08; GPIOB->BSRR = 0x20;
BX LR
NOP
END
;C语言中extern void macRM68090_WR_DATA(int data); R0 为data
AREA EXAMPLE,CODE,READONLY
EXPORT macRM68090_WR_DATA
ENTRY
macRM68090_WR_DATA
;macRM68090_RS_SET
LDR R1, =0x10
ldr r4, =0x40010C10
STR R1, [r4] ; GPIOB->BSRR = 0x10;
;macRM68090_CS_CLR
LDR R1, =0x20
ldr r4, =0x40010C14
STR R1, [r4] ; GPIOB->BRR = 0x20;
;macRM68090_8BIT_DATAOUT(data&0xFF00);
AND R1, R0, #0xFF00
LDR R3, =0x40010C0C
AND R3, R3, #0X00FF
ORR R2, R1, R3
ldr r4, =0x40010C0C
STR R2, [R4]
;macRM68090_WR_CLR;macRM68090_WR_SET
LDR R1, =0x08
ldr r4, =0x40010C14
STR R1, [R4] ;GPIOB->BRR = 0x08;
ldr r4, =0x40010C10
STR R1, [R4] ;GPIOB->BSRR = 0x08;
;macRM68090_8BIT_DATAOUT(((data<<8) &0xFF00));
MOV R2, R0, LSL#8
AND R2, R2, #0xFF00
LDR R3, =0x40010C0C
AND R3, R3, #0x00FF
ORR R2, R0, R3
ldr r4, =0x40010C0C
STR R2, [R4]
;macRM68090_WR_CLR ;
ldr r4, =0x40010C14
STR R1, [r4] ; GPIOB->BRR = 0x08;
;macRM68090_WR_SET ;macRM68090_CS_SET
LDR R1, =0x28
ldr r4, =0x40010C10
STR R1, [R4] ; GPIOB->BSRR = 0x08; GPIOB->BSRR = 0x20;
BX LR
NOP
END