2016-02-05 73 views
1

工作代碼如下:如何獲得在宏參數變量的值在NASM裝配

%macro ISR_NOERRCODE 1 
    [GLOBAL isr%1] 
    isr%1: 
     ... 
%endmacro 

%assign i 0    ; Initialize the loop Variable 

%rep 8 
    ISR_NOERRCODE i 
    %assign i i+1 
%endrep 

它擴展爲8個代碼塊的名字isr1isr2等 但在GAS語法,給宏觀的看法似乎並沒有擴大。我的代碼是:

.macro ISR_NOERRCODE n 
    .global isr\n 
    isr\n: 
     ... 
.endm 

.set i, 0 

.rept 
    ISR_NOERRCODE $i 
    .set i, $i + 1 
.endr 

導致彙編錯誤:

Error: symbol `isr$i' is already defined 

因爲宏似乎走的是$i參數作爲一個字符串。

這在GAS語法中甚至可能嗎?

回答

2

首先,您需要使用.altmacro指令來啓用備用宏模式。其中一個增加的功能是:

Expression results as strings

You can write %expr to evaluate the expression expr and use the result as a string.

因此,如果我們在前面加上我們的宏參數以%將被評估爲一種表達,變成了一個字符串,這是我們想在這裏。你的代碼可能是這樣的:

.altmacro 

.macro ISR_NOERRCODE n 
    .global isr\n 
    isr\n: 
     ... 

.set i, 0 
.rept 8 
    ISR_NOERRCODE %i 
    .set i, i + 1 
.endr 

相反你可以用.noaltmacro這是默認禁用備用微距模式。

+0

工程就像一個魅力,非常感謝! – Gargamel