ARM C / C++ calling ASM

参考 ARM官方文档实现 C inline assemly code, 运行环境 ARM DS-5 (arm6 compiler):

1. Writing inline assembly code

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.100748_0606_00_en/ddx1471430827125.html

#include <stdio.h>

int add(int i, int j)
{
  int res = 0;
  __asm ("ADD %[result], %[input_i], %[input_j]"
    : [result] "=r" (res)
    : [input_i] "r" (i), [input_j] "r" (j)
  );
  return res;
}

int main(void)
{
  int a = 1;
  int b = 2;
  int c = 0;

  c = add(a,b);

  printf("Result of %d + %d = %d\n", a, b, c);
}

 2. Calling assembly functions from C and C++

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.100748_0606_00_en/ddx1471430827125.html

helloARM.c

#include <stdio.h>

extern int myadd(int a, int b);

int main()
{
	int a = 4;
	int b = 5;
	printf("Adding %d and %d results in %d\n", a, b, myadd(a, b));
	return (0);
}

 myadd.s

.globl   myadd
     .p2align 2
	.type    myadd,%function

myadd:                     // Function "myadd" entry point.
	.fnstart
	add      r0, r0, r1   // Function arguments are in R0 and R1. Add together and put the result in R0.
	bx       lr           // Return by branching to the address in the link register.
	.fnend

make all 
Building file: ../src/helloARM.c
Invoking: Arm C Compiler 6
armclang --target=arm-arm-none-eabi -mcpu=cortex-a8 -mfpu=none -mfloat-abi=soft -marm -O0 -g -MD -MP -c -o "src/helloARM.o" "../src/helloARM.c"
armclang: warning: Your license for feature ult_armcompiler will expire in 26 days [-Wlicense-management]
Finished building: ../src/helloARM.c
 
Building file: ../src/myadd.s
Invoking: Arm Assembler 6
armclang --target=arm-arm-none-eabi -mcpu=cortex-a8 -mfpu=none -mfloat-abi=soft -marm -g -c -o "src/myadd.o" "../src/myadd.s"
armclang: warning: Your license for feature ult_armcompiler will expire in 26 days [-Wlicense-management]
Finished building: ../src/myadd.s
 
Building target: helloARM.axf
Invoking: Arm Linker 6
armlink --ro_base=0x80000000 --info=sizes -o "helloARM.axf"  ./src/helloARM.o ./src/myadd.o  

 

3. 用汇编实现 strcpy

helloARM.c

#include <stdio.h>

extern void strcopy(char* d, const char* s);

int main()
{
	const char* srcstr = "First String";
	char dststr[] = "Second String";
	printf("Before strcopy: srcstr = %s, dststr = %s \n", srcstr, dststr);
	strcopy(dststr, srcstr);
	printf("After strcopy: srcstr = %s, dststr = %s \n", srcstr, dststr);
	return (0);
}

scopy.s

.globl strcopy

strcopy:
	.fnstart
	LDRB R2, [R1], #1
	STRB R2, [R0], #1
	CMP R2, #0
	BNE strcopy
	MOV pc, lr
	.fnend

make all 
Building file: ../src/helloARM.c
Invoking: Arm C Compiler 6
armclang --target=arm-arm-none-eabi -mcpu=cortex-a8 -mfpu=none -mfloat-abi=soft -marm -O0 -g -MD -MP -c -o "src/helloARM.o" "../src/helloARM.c"
Finished building: ../src/helloARM.c
 
Building file: ../src/scopy.s
Invoking: Arm Assembler 6
armclang --target=arm-arm-none-eabi -mcpu=cortex-a8 -mfpu=none -mfloat-abi=soft -marm -g -c -o "src/scopy.o" "../src/scopy.s"
Finished building: ../src/scopy.s
 
Building target: helloARM.axf
Invoking: Arm Linker 6
armlink --ro_base=0x80000000 --info=sizes -o "helloARM.axf"  ./src/helloARM.o ./src/scopy.o   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值