You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GNU汇编器宏中实现参数后追加字符串?

在GNU汇编器宏参数后追加字符串的解决方法

修改后的宏代码

GNU汇编器(GAS)中,若要在宏参数后直接追加字符串,必须用{}包裹参数名以明确边界,避免汇编器将参数名与后续字符误认为一个完整的未定义参数名。以下是调整后的add_resource宏:

.macro add_resource resource_name, file_name
    .global \resource_name
    .type \resource_name, @object
    .balign 4
\resource_name:
    .incbin "\file_name"
\resource_name{}_end:
    .global \resource_name{}_size
    .type \resource_name{}_size, @object
    .balign 4
\resource_name{}_size:
    .int \resource_name{}_end - \resource_name
.endm

.section .rodata

add_resource icon, "icon.png"

对应的C语言代码修改

将原来引用的size_icon标识符改为后缀形式的icon_size:

#include <stdio.h>
extern unsigned char icon[];
extern unsigned int icon_size;
int main() {
    printf("magic bytes: %.3s, size: %u", icon+1, icon_size);
    return 0;
}

验证结果

执行nm命令后,会生成符合需求的后缀式标识符:

0000000000f99b9d r icon_end
0000000000002020 R icon
0000000000f99ba0 R icon_size

程序运行后输出正常:magic bytes: PNG, size: 16350077

内容的提问来源于stack exchange,提问作者God I Am Clown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:43:22