我不知道如何參考它,如果我使用宏,并且引數似乎是一個字串,并且如果我對其使用尋址模式,那么宏就可以獲取字串,例如(test), $test,.etc。
下面的代碼只是例子!!
.macro newcat str
newcat:
.string "\str"
.equ newcat_len, .-newcat
.endm
.section .data
test:
.string "abc"
# expect newcat has string "abc", but it got string "test"
newcat test
.section .text
.globl _start
_start:
movq $1, %rax
movq $1, %rdi
movq $newcat, %rsi
movq $newcat_len, %rdx
syscall
movq $60, %rax
syscall
uj5u.com熱心網友回復:
無論有沒有宏,您都無法在匯編時讀取記憶體內容。您不能將位元組組裝到某個部分,然后讓另一個指令將這些位元組復制到其他地方。
您可以做的是定義一個與另一個符號具有相同地址的新符號,這對于只讀資料(.section .rodata)顯然更有效。您只想將 ASCII 位元組實際復制為 mutable 的初始化程式.data,或者在極少數情況下,即使資料恰好相同,您也需要兩件事的唯一地址。(也許作為 astruct { int i; char s[4];} table[16];或某物的 LUT 條目。)
.section .rodata
str1:
str2: # you can have 2 labels that happen to have the same address
.asciz "Hello"
# at any point later
.set str3, str1 # an alias, just like if you'd put str3: before/after str2:
希望這個例子也能更清楚地說明標簽只是碰巧后面跟著資料,它們不像 C 變數,它是參考該位置的唯一方法。
如果您想要兩次相同的字串資料而不讓它再次出現在您的檔案中,則字串本身需要是一個宏。并且您需要參考該宏名稱,而不是某個標簽,該標簽恰好后面跟著一些在那里發出一些資料的偽指令。
.macro foo
.asciz "the text"
.endm
.data
str1: foo
str2: foo
如果需要,您可以讓宏將符號名稱作為引數并包含這些標簽,.equ如果需要,甚至可以使用從該名稱派生的另一個名稱定義 a。
或者使用 C 前處理器(命名檔案并使用而不是 plainfoo.S構建)。我不確定 GAS 自己的宏語言是否允許像這樣的宏擴展為單個令牌,而不是整行。與 NASM 相比,我覺得它很麻煩。gcc -cas
#define foo "the text"
.data
str1: .asciz foo
str2: .asciz foo
其他匯編器的作業方式相同,例如 NASM:Assemble-time read the value of a data variable
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/498465.html
