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 –