0
说我有一些通用的x86代码NASM:NASM:在编译时提供恒定的价值
%define Constant 123
mov si, Constant
的问题是恒定值Constant
大会写入时还不知道。通过这个,我的意思是应该在组合文件时提供常量的值。在我的情况下,我需要的常量取决于文本文件的大小。
这是如何实现的?
说我有一些通用的x86代码NASM:NASM:在编译时提供恒定的价值
%define Constant 123
mov si, Constant
的问题是恒定值Constant
大会写入时还不知道。通过这个,我的意思是应该在组合文件时提供常量的值。在我的情况下,我需要的常量取决于文本文件的大小。
这是如何实现的?
查看NASM手册后,我发现汇编器没有执行我所需的命令行选项。我的解决方案非常简单。以下脚本显示了我如何解决问题。
#!/bin/sh
# Could be replaced by any other way of getting the constant value; this gets a file's size
fileSize=`stat --printf="%s" my_file.txt`
# Write the constant definition to a temporary file
printf "%%define FILE_SIZE %s\n" $fileSize > tmp
# Append the rest to the temporary file
cat my_asm.asm >> tmp
# Assemble the file and name the output correctly
nasm tmp -o my_asm.out
# Remove the temporary file
rm tmp
'-D'选项不会做你需要的吗? –
和/或'%include'至少在构建过程中保存'cat'-to-a-temporary。 –
@PeterCordes我喜欢你的想法。您的方式包括一种方法来查看ASM文件内部将追加的内容。 +1 –