{"id":15816,"date":"2024-11-18T12:16:36","date_gmt":"2024-11-18T04:16:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/15816\/"},"modified":"2024-11-18T12:16:36","modified_gmt":"2024-11-18T04:16:36","slug":"linux%e4%b8%8b%e4%bd%bf%e7%94%a8gcc%e8%bf%9b%e8%a1%8c%e5%b5%8c%e5%85%a5%e5%bc%8farm%e6%b1%87%e7%bc%96%e4%bc%98%e5%8c%96%e7%9a%84%e5%b8%b8%e8%a7%81%e9%85%8d%e7%bd%ae%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15816\/","title":{"rendered":"Linux\u4e0b\u4f7f\u7528GCC\u8fdb\u884c\u5d4c\u5165\u5f0fARM\u6c47\u7f16\u4f18\u5316\u7684\u5e38\u89c1\u914d\u7f6e\u65b9\u6cd5"},"content":{"rendered":"<p>linux\u4e0b\u4f7f\u7528gcc\u8fdb\u884c\u5d4c\u5165\u5f0farm\u6c47\u7f16\u4f18\u5316\u7684\u5e38\u89c1\u914d\u7f6e\u65b9\u6cd5<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5bf9\u4e8eARM\u67b6\u6784\u7684\u5904\u7406\u5668\uff0c\u5f80\u5f80\u9700\u8981\u8fdb\u884c\u9ad8\u6548\u7684\u4f18\u5316\uff0c\u4ee5\u6ee1\u8db3\u5b9e\u65f6\u6027\u80fd\u548c\u8d44\u6e90\u9650\u5236\u3002\u800c\u6c47\u7f16\u8bed\u8a00\u662f\u4e00\u79cd\u53ef\u4ee5\u76f4\u63a5\u63a7\u5236\u786c\u4ef6\u7684\u8bed\u8a00\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u5173\u952e\u7b97\u6cd5\uff0c\u4f7f\u7528\u6c47\u7f16\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u6027\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728Linux\u73af\u5883\u4e0b\uff0c\u4f7f\u7528GCC\u8fdb\u884c\u5d4c\u5165\u5f0fARM\u6c47\u7f16\u4f18\u5316\u7684\u5e38\u89c1\u914d\u7f6e\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u76f8\u5173\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u7f16\u5199ARM\u6c47\u7f16\u4ee3\u7801<br \/>GCC\u7f16\u8bd1\u5668\u652f\u6301\u5d4c\u5165\u6c47\u7f16\uff0c\u6211\u4eec\u53ef\u4ee5\u5728C\u4ee3\u7801\u4e2d\u5d4c\u5165ARM\u6c47\u7f16\u4ee3\u7801\uff0c\u7528\u4e8e\u4f18\u5316\u5173\u952e\u51fd\u6570\u7684\u6027\u80fd\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199ARM\u6c47\u7f16\u4ee3\u7801\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528ARM\u6c47\u7f16\u6765\u5b9e\u73b0\u5feb\u901f\u4e58\u6cd5\uff1a<\/p>\n<pre>.global fast_multiply\nfast_multiply:\n    LDR r0, [r0]       @ load the first operand into r0\n    LDR r1, [r1]       @ load the second operand into r1\n    MUL r0, r0, r1     @ multiply the two operands\n    BX  lr             @ return the result<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u4e24\u4e2a\u6570\u76f8\u4e58\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n<p>\u4e8c\u3001C\u4ee3\u7801\u4e2d\u5d4c\u5165ARM\u6c47\u7f16<br \/>GCC\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u5185\u8054\u6c47\u7f16\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u5728C\u4ee3\u7801\u4e2d\u76f4\u63a5\u5d4c\u5165ARM\u6c47\u7f16\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728C\u4ee3\u7801\u4e2d\u5d4c\u5165\u4e0a\u8ff0\u7684\u5feb\u901f\u4e58\u6cd5\u51fd\u6570\uff1a<\/p>\n<pre>int main()\n{\n    int a = 10;\n    int b = 20;\n    int result;\n\n    asm volatile (\n        \"ldr r0, [%1]\n\"    \/\/ load the first operand into r0\n        \"ldr r1, [%2]\n\"    \/\/ load the second operand into r1\n        \"bl fast_multiply\n\"\/\/ call the fast_multiply function\n        \"mov %0, r0\"        \/\/ save the result to \"result\"\n        :\n        :\"r\" (result), \"r\" (&amp;amp;a), \"r\" (&amp;amp;b)\n        :\"r0\", \"r1\"         \/\/ clobbered registers\n    );\n\n    printf(\"Result: %d\n\", result);\n\n    return 0;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u4e24\u4e2a\u6570\u76f8\u4e58\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728\u53d8\u91cfresult\u4e2d\u3002<\/p>\n<p>\u4e09\u3001\u7f16\u8bd1\u914d\u7f6e<br \/>\u5728Linux\u4e0b\u4f7f\u7528GCC\u8fdb\u884cARM\u6c47\u7f16\u4f18\u5316\uff0c\u9700\u8981\u8fdb\u884c\u76f8\u5e94\u7684\u7f16\u8bd1\u914d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u914d\u7f6e\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u9009\u62e9ARM\u67b6\u6784\uff1a\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9aGCC\u7f16\u8bd1\u5668\u4f7f\u7528ARM\u67b6\u6784\u3002\u53ef\u4ee5\u4f7f\u7528-march\u9009\u9879\u6765\u6307\u5b9aARM\u7684\u5904\u7406\u5668\u67b6\u6784\uff0c\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>$ gcc -march=armv7-a -c main.c<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u542f\u7528\u4f18\u5316\uff1aGCC\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u4f18\u5316\u9009\u9879\uff0c\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u542f\u7528\u5bf9ARM\u6c47\u7f16\u7684\u4f18\u5316\u3002\u4f7f\u7528-O\u9009\u9879\u53ef\u4ee5\u5f00\u542f\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u7684\u4f18\u5316\uff0c\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>$ gcc -O2 -march=armv7-a -c main.c<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5173\u95ed\u6d6e\u70b9\u8fd0\u7b97\uff1a\u5bf9\u4e8e\u4e00\u4e9b\u5d4c\u5165\u5f0f\u7cfb\u7edf\uff0c\u53ef\u80fd\u6ca1\u6709\u6d6e\u70b9\u8fd0\u7b97\u5355\u5143\uff0c\u56e0\u6b64\u9700\u8981\u6307\u5b9a\u7f16\u8bd1\u5668\u4e0d\u8981\u4f7f\u7528\u6d6e\u70b9\u8fd0\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528-mfpu\u548c-mfloat-abi\u9009\u9879\uff0c\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>$ gcc -march=armv7-a -mfpu=none -mfloat-abi=softfp -c main.c<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56db\u3001\u6c47\u7f16\u4f18\u5316\u793a\u4f8b<br \/>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728C\u4ee3\u7801\u4e2d\u5d4c\u5165ARM\u6c47\u7f16\uff0c\u5e76\u8fdb\u884c\u4f18\u5316\uff1a<\/p>\n<pre>#include \n  \n   \n\nint main()\n{\n    int a = 10;\n    int b = 20;\n    int result;\n\n    asm volatile (\n        \"ldr r0, [%1]\n\"    \/\/ load the first operand into r0\n        \"ldr r1, [%2]\n\"    \/\/ load the second operand into r1\n        \"bl fast_multiply\n\"\/\/ call the fast_multiply function\n        \"mov %0, r0\"        \/\/ save the result to \"result\"\n        :\n        :\"r\" (result), \"r\" (&amp;amp;a), \"r\" (&amp;amp;b)\n        :\"r0\", \"r1\"         \/\/ clobbered registers\n    );\n\n    printf(\"Result: %d\n\", result);\n\n    return 0;\n}\n\n.global fast_multiply\nfast_multiply:\n    LDR r0, [r0]       \/\/ load the first operand into r0\n    LDR r1, [r1]       \/\/ load the second operand into r1\n    MUL r0, r0, r1     \/\/ multiply the two operands\n    BX  lr             \/\/ return the result\n  <\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u4e24\u4e2a\u6570\u76f8\u4e58\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n<p>\u7ed3\u8bba\uff1a<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86\u5728Linux\u73af\u5883\u4e0b\u4f7f\u7528GCC\u8fdb\u884c\u5d4c\u5165\u5f0fARM\u6c47\u7f16\u4f18\u5316\u7684\u5e38\u89c1\u914d\u7f6e\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u4e86\u76f8\u5173\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u901a\u8fc7\u4f7f\u7528GCC\u7f16\u8bd1\u5668\u7684\u5185\u8054\u6c47\u7f16\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5728C\u4ee3\u7801\u4e2d\u5d4c\u5165ARM\u6c47\u7f16\uff0c\u4ee5\u5b9e\u73b0\u9488\u5bf9ARM\u67b6\u6784\u7684\u9ad8\u6548\u4f18\u5316\u3002\u8fd9\u4e9b\u4f18\u5316\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u5d4c\u5165\u5f0f\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u6548\u7387\u3002<\/p>\n<p>\u53c2\u8003\u6587\u732e\uff1a<\/p>\n<ol>\n<li>GNU Compiler Collection (GCC) &#8211; Using the GNU Compiler Collection (GCC), https:\/\/gcc.gnu.org\/onlinedocs\/<\/li>\n<li>ARM Limited &#8211; ARM Architecture Reference Manual, https:\/\/developer.arm.com\/documentation\/ddi0487\/latest\/<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662fLinux\u4e0b\u4f7f\u7528GCC\u8fdb\u884c\u5d4c\u5165\u5f0fARM\u6c47\u7f16\u4f18\u5316\u7684\u5e38\u89c1\u914d\u7f6e\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux\u4e0b\u4f7f\u7528gcc\u8fdb\u884c\u5d4c\u5165\u5f0farm\u6c47\u7f16\u4f18\u5316\u7684\u5e38\u89c1\u914d\u7f6e\u65b9\u6cd5 \u5f15\u8a00\uff1a\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5bf9\u4e8eARM\u67b6\u6784\u7684\u5904\u7406\u5668\uff0c\u5f80\u5f80\u9700\u8981\u8fdb\u884c\u9ad8\u6548\u7684\u4f18\u5316\uff0c\u4ee5\u6ee1\u8db3\u5b9e\u65f6\u6027\u80fd\u548c\u8d44\u6e90\u9650\u5236\u3002\u800c\u6c47\u7f16\u8bed\u8a00\u662f\u4e00\u79cd\u53ef\u4ee5\u76f4\u63a5\u63a7\u5236\u786c\u4ef6\u7684\u8bed\u8a00\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u5173\u952e\u7b97\u6cd5\uff0c\u4f7f\u7528\u6c47\u7f16\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u6027\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728Linux\u73af\u5883\u4e0b\uff0c\u4f7f\u7528GCC\u8fdb\u884c\u5d4c\u5165\u5f0fARM\u6c47\u7f16\u4f18\u5316\u7684\u5e38\u89c1\u914d\u7f6e\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u76f8\u5173\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u7f16\u5199ARM\u6c47\u7f16\u4ee3\u7801GCC\u7f16\u8bd1\u5668\u652f\u6301\u5d4c\u5165\u6c47\u7f16\uff0c\u6211\u4eec\u53ef\u4ee5\u5728C\u4ee3\u7801\u4e2d\u5d4c\u5165ARM\u6c47\u7f16\u4ee3\u7801\uff0c\u7528\u4e8e\u4f18\u5316\u5173\u952e\u51fd\u6570\u7684\u6027\u80fd\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199ARM\u6c47\u7f16\u4ee3\u7801\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528ARM\u6c47\u7f16\u6765\u5b9e\u73b0\u5feb\u901f\u4e58\u6cd5\uff1a .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 \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u5c06\u4e24\u4e2a\u6570\u76f8\u4e58\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u3002 \u4e8c\u3001C\u4ee3\u7801\u4e2d\u5d4c\u5165ARM\u6c47\u7f16GCC\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u5185\u8054\u6c47\u7f16\u7684\u7279\u6027\uff0c\u53ef\u4ee5\u5728C\u4ee3\u7801\u4e2d\u76f4\u63a5\u5d4c\u5165ARM\u6c47\u7f16\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728C\u4ee3\u7801\u4e2d\u5d4c\u5165\u4e0a\u8ff0\u7684\u5feb\u901f\u4e58\u6cd5\u51fd\u6570\uff1a int main() { int a = 10; int b = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-15816","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=15816"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15816\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}