ARM汇编实现 for循环、strcmp、 调用printf打印字符串示例

utils.S (实现for循环、strcmp)

 AREA FIRE, CODE, READONLY
 EXPORT for
 EXPORT strcmp_asm

for
 PUSH {LR}
FOR_LOOP
 BLX R0
 SUBS R1, R1, #0x1
 BNE FOR_LOOP
 POP {LR}
 BX LR

strcmp_asm
 LDRB R2, [R0], #1
 LDRB R3, [R1], #1
 CMP R2, #0
 CMPNE R3, #0
 BEQ return
 CMP R2, R3
 BEQ strcmp_asm
return
 SUB R0, R2, R3
 MOV PC, LR

 END

clac.S (下面为什么写“PRESERVE8”? https://blog.csdn.net/heyustudent/article/details/39206155)

 PRESERVE8
 AREA CLAC, CODE, READONLY
 EXPORT asm_add
 IMPORT for
 IMPORT strcmp_asm
 IMPORT _printf


ANONYMOUS_FIRE DCB "无名之火-_-!!", 0
ANONYMOUS_FIRE2 DCB "无名之火-_-!!", 0

asm_test PROC
 NOP
 BX LR
 ENDP

asm_printf
 STMFD SP!, {LR}
 BL _printf
 LDMFD SP!, {PC}

asm_add PROC
 STMFD SP!, {LR}

 LDR R0, =asm_test
 MOV R1, #0x3
 ;BL for

 LDR R0, =ANONYMOUS_FIRE
 ;BL asm_printf
 BL _printf                   ;在ARM 里调用printf
 LDR R1, =ANONYMOUS_FIRE2
 BL strcmp_asm

 ;BX LR
 LDMFD SP!, {R1}
 MOV PC, R1
 ENDP

 END

main.c

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>

extern int asm_add(int, int);

int main(int argc, char* argv[]){
	int a = 13;
	int b = 2;
	int result = asm_add(a, b);
	printf("hello, arm7 ! result=%d", result);
	return 0;
}

输出:无名之火-_-!!hello, arm7 ! result=26

参考:

https://blog.csdn.net/denlee/article/details/2445810?utm_source=blogxgwz9

https://blog.csdn.net/heyustudent/article/details/39206155

https://wenku.baidu.com/view/a0306e974028915f804dc265.html

发布了106 篇原创文章 · 获赞 204 · 访问量 128万+

猜你喜欢

转载自blog.csdn.net/ab6326795/article/details/90485365