C语言调用汇编实现字符串对换
1. 前面配置arm交叉编译环境。
2. 配置好qemu-arm
C语言代码string-switch.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <stdio.h> #include <stdlib.h> extern void strswp( char *str1, char *str2); int main( void ) { char str1[10] = "123456789" ; char str2[10] = "abcde" ; strswp(str1, str2); //调汇编函数 printf ( "buf1 = %s\nbuf2 = %s\n" ,str1, str2); return 0; } |
汇编代码 switch.S:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | .global strswp .type strswp,function strswp: ldrb v1, [a1] ldrb v2, [a2] cmp v1, #0 cmpne v2, #0 beq quit strb v1, [a2], #1 strb v2, [a1], #1 b strswp quit: mov pc, lr .end |
终端进入到string_switch.c所在的文件夹执行编译命令:
arm-linux-gcc -static string-switch.c switch.S -o string-switch
生成arm可执行文件string-switch
然后终端执行:
qemu-arm string-switch
输出结果:
buf1 = abcde6789
buf2 = 12345
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步