Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在NASM和GCC中提供incbin路径作为预定义宏_Gcc_Assembly_Nasm_Gnu Assembler - Fatal编程技术网

在NASM和GCC中提供incbin路径作为预定义宏

在NASM和GCC中提供incbin路径作为预定义宏,gcc,assembly,nasm,gnu-assembler,Gcc,Assembly,Nasm,Gnu Assembler,我想在可执行文件中嵌入一个二进制文件,以一种可移植到Windows和Linux的方式 为了实现这一点,我想使用指令incbin的汇编文件。例如: .intel_syntax noprefix .global _img0_start .global _img0_end .global _img0_size _img0_start: .incbin "img0.bin" _img0_end: _img0_size: .quad $ - _im

我想在可执行文件中嵌入一个二进制文件,以一种可移植到Windows和Linux的方式

为了实现这一点,我想使用指令
incbin
的汇编文件。例如:

.intel_syntax noprefix


      .global _img0_start
      .global _img0_end
      .global _img0_size


_img0_start:   .incbin "img0.bin"
_img0_end:
_img0_size:    .quad  $ -  _img0_start
在编译上述程序集文件之前,我不知道二进制文件将位于何处,因此我想用宏替换
img0.bin
,并在命令行参数中定义它,就像使用
gcc
选项
-D
定义宏常量一样

我已经看到存在
-d选项:预先定义一个宏
,它允许完全执行上述操作,但仅限于数值(我需要提供一个文件路径)。(字体:)


是否有类似于
-d
的东西,但它允许定义字符串,并适用于moth
gas
nasm

我读了更多,我意识到不需要扩展为字符串的宏

相反,可以做的只是为二进制文件提供一个固定的名称,在我的例子中是,
img0.bin
,然后使用
-I
选项为搜索此类文件提供额外的路径

换句话说,使用与上述相同的程序集文件(设为
assembly\u file.s
),只需执行以下操作:


gcc-c assembly\u file.s-I-o

NASM宏和c预处理器都允许使用任意字符串

NASM
eq
常量必须是整数,但
%define foo eax
NASM'-DIMG=“foo.bin”
两者都可以工作

请注意,宏定义包含双引号。CPP使在双引号内展开宏变得很容易,但我没有检查NASM是否可以轻松地做到这一点

通常很容易创建一个双引号字符串,比如Make或其他什么


hi.bin
包含
B8 68 69 00
,是用于
mov eax的32/64位机器代码,“hi”


$gcc-cfoogas.S-DIMG=''hi.bin''
$nasm-felf64 foo-nasm.asm-DIMG=''hi.bin''
$disas foo gas.o
...
0000000000000000 :
0:b8 68 69 00 mov eax,0x6968
$disas foo nasm.o
...
0000000000000000 :
0:b8 68 69 00 mov eax,0x6968

让构建系统将文件复制/链接到预期名称可能会更容易。或者,您可以使用预处理的程序集
gcc
知道调用
.S
文件上的预处理器,或者您可以使用命令行开关。您必须自己在nasm文件上运行预处理器。我认为nasm
-DFOO=bar
适用于任意文本替换。对于非整数,您当然可以在文件中使用NASM
%define
宏;只有
eq
常量必须是数字。
;;; foo-nasm.asm  NASM source
incbin IMG
# foo-gas.S   GAS source (capital S gets GCC to run it through CPP)
.incbin IMG
 $ gcc -c foo-gas.S -DIMG='"hi.bin"'            
 $ nasm -felf64 foo-nasm.asm -DIMG='"hi.bin"'       

$ disas foo-gas.o
...
0000000000000000 <.text>:
   0:   b8 68 69 00 00          mov    eax,0x6968

$ disas foo-nasm.o
...
0000000000000000 <.text>:
   0:   b8 68 69 00 00          mov    eax,0x6968