LinuxÏÂʹÓÃGCC¾ÙÐÐǶÈëʽARM»ã±àÓÅ»¯µÄ³£¼ûÉèÖÃÒªÁì
linuxÏÂʹÓÃgcc¾ÙÐÐǶÈëʽarm»ã±àÓÅ»¯µÄ³£¼ûÉèÖÃÒªÁì
СÐò£º
ǶÈëʽϵͳÖУ¬¹ØÓÚARM¼Ü¹¹µÄ´¦Öóͷ£Æ÷£¬ÍùÍùÐèÒª¾ÙÐиßЧµÄÓÅ»¯£¬ÒÔÖª×ãʵʱÐÔÄܺÍ×ÊÔ´ÏÞÖÆ¡£¶ø»ã±àÓïÑÔÊÇÒ»ÖÖ¿ÉÒÔÖ±½Ó¿ØÖÆÓ²¼þµÄÓïÑÔ£¬¹ØÓÚһЩҪº¦Ëã·¨£¬Ê¹Óûã±à¿ÉÒÔ´ó·ùÌáÉýÐÔÄÜ¡£±¾ÎĽ«ÏÈÈÝÔÚLinuxÇéÐÎÏ£¬Ê¹ÓÃGCC¾ÙÐÐǶÈëʽARM»ã±àÓÅ»¯µÄ³£¼ûÉèÖÃÒªÁ죬²¢¸ø³öÏà¹ØµÄ´úÂëʾÀý¡£
Ò»¡¢±àдARM»ã±à´úÂë
GCC±àÒëÆ÷Ö§³ÖǶÈë»ã±à£¬ÎÒÃÇ¿ÉÒÔÔÚC´úÂëÖÐǶÈëARM»ã±à´úÂ룬ÓÃÓÚÓÅ»¯Òªº¦º¯ÊýµÄÐÔÄÜ¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒª±àдARM»ã±à´úÂë¡£
ÒÔÏÂÊÇÒ»¸öÀý×Ó£¬Õ¹Ê¾ÔõÑùʹÓÃARM»ã±àÀ´ÊµÏÖ¿ìËٳ˷¨£º
.global fast_multiply fast_multiply: LDR r0, [r0] @ load the first operand into r0 LDR r1, [r1] @ load the second operand into r1 MUL r0, r0, r1 @ multiply the two operands BX lr @ return the result
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫Á½¸öÊýÏà³Ë£¬²¢½«Ð§¹û·µ»Ø¡£
¶þ¡¢C´úÂëÖÐǶÈëARM»ã±à
GCC±àÒëÆ÷ÌṩÁËÄÚÁª»ã±àµÄÌØÕ÷£¬¿ÉÒÔÔÚC´úÂëÖÐÖ±½ÓǶÈëARM»ã±à¡£ÏÂÃæµÄʾÀýչʾÁËÔõÑùÔÚC´úÂëÖÐǶÈëÉÏÊöµÄ¿ìËٳ˷¨º¯Êý£º
int main() { int a = 10; int b = 20; int result; asm volatile ( "ldr r0, [%1] " // load the first operand into r0 "ldr r1, [%2] " // load the second operand into r1 "bl fast_multiply "// call the fast_multiply function "mov %0, r0" // save the result to "result" : :"r" (result), "r" (&a), "r" (&b) :"r0", "r1" // clobbered registers ); printf("Result: %d ", result); return 0; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫Á½¸öÊýÏà³Ë£¬²¢½«Ð§¹ûÉúÑÄÔÚ±äÁ¿resultÖС£
Èý¡¢±àÒëÉèÖÃ
ÔÚLinuxÏÂʹÓÃGCC¾ÙÐÐARM»ã±àÓÅ»¯£¬ÐèÒª¾ÙÐÐÏìÓ¦µÄ±àÒëÉèÖá£ÒÔÏÂÊÇһЩ³£¼ûµÄÉèÖÃÒªÁ죺
Ñ¡ÔñARM¼Ü¹¹£ºÊ×ÏÈ£¬ÎÒÃÇÐèÒªÖ¸¶¨GCC±àÒëÆ÷ʹÓÃARM¼Ü¹¹¡£¿ÉÒÔʹÓÃ-marchÑ¡ÏîÀ´Ö¸¶¨ARMµÄ´¦Öóͷ£Æ÷¼Ü¹¹£¬ÀýÈ磺
$ gcc -march=armv7-a -c main.c
µÇ¼ºó¸´ÖÆ
ÆôÓÃÓÅ»¯£ºGCC±àÒëÆ÷ÌṩÁ˸»ºñµÄÓÅ»¯Ñ¡Ï¿ÉÒÔÔÚ±àÒëʱÆôÓöÔARM»ã±àµÄÓÅ»¯¡£Ê¹ÓÃ-OÑ¡Ïî¿ÉÒÔ¿ªÆôÒ»¶¨Ë®Æ½ÉϵÄÓÅ»¯£¬ÀýÈ磺
$ gcc -O2 -march=armv7-a -c main.c
µÇ¼ºó¸´ÖÆ
¹Ø±Õ¸¡µãÔËË㣺¹ØÓÚһЩǶÈëʽϵͳ£¬¿ÉÄÜûÓи¡µãÔËË㵥룬Òò´ËÐèÒªÖ¸¶¨±àÒëÆ÷²»ÒªÊ¹Óø¡µãÔËË㣬¿ÉÒÔʹÓÃ-mfpuºÍ-mfloat-abiÑ¡ÏÀýÈ磺
$ gcc -march=armv7-a -mfpu=none -mfloat-abi=softfp -c main.c
µÇ¼ºó¸´ÖÆ
ËÄ¡¢»ã±àÓÅ»¯Ê¾Àý
ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룬չʾÁËÔõÑùÔÚC´úÂëÖÐǶÈëARM»ã±à£¬²¢¾ÙÐÐÓÅ»¯£º
#include int main() { int a = 10; int b = 20; int result; asm volatile ( "ldr r0, [%1] " // load the first operand into r0 "ldr r1, [%2] " // load the second operand into r1 "bl fast_multiply "// call the fast_multiply function "mov %0, r0" // save the result to "result" : :"r" (result), "r" (&a), "r" (&b) :"r0", "r1" // clobbered registers ); printf("Result: %d ", result); return 0; } .global fast_multiply fast_multiply: LDR r0, [r0] // load the first operand into r0 LDR r1, [r1] // load the second operand into r1 MUL r0, r0, r1 // multiply the two operands BX lr // return the result
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫Á½¸öÊýÏà³Ë£¬²¢½«Ð§¹û·µ»Ø¡£
½áÂÛ£º
±¾ÎÄÏÈÈÝÁËÔÚLinuxÇéÐÎÏÂʹÓÃGCC¾ÙÐÐǶÈëʽARM»ã±àÓÅ»¯µÄ³£¼ûÉèÖÃÒªÁ죬²¢¸ø³öÁËÏà¹ØµÄ´úÂëʾÀý¡£Í¨¹ýʹÓÃGCC±àÒëÆ÷µÄÄÚÁª»ã±àÌØÕ÷£¬ÎÒÃÇ¿ÉÒÔÔÚC´úÂëÖÐǶÈëARM»ã±à£¬ÒÔʵÏÖÕë¶ÔARM¼Ü¹¹µÄ¸ßЧÓÅ»¯¡£ÕâЩÓÅ»¯¿ÉÒÔ´ó·ùÌáÉýǶÈëʽϵͳµÄÐÔÄܺÍЧÂÊ¡£
²Î¿¼ÎÄÏ×£º
GNU Compiler Collection (GCC) – Using the GNU Compiler Collection (GCC), https://gcc.gnu.org/onlinedocs/
ARM Limited – ARM Architecture Reference Manual, https://developer.arm.com/documentation/ddi0487/latest/
ÒÔÉϾÍÊÇLinuxÏÂʹÓÃGCC¾ÙÐÐǶÈëʽARM»ã±àÓÅ»¯µÄ³£¼ûÉèÖÃÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡