2013-11-04 71 views
3

我目前試圖理解彙編語言中的宏的概念。我大學的幻燈片如下:彙編語言中的宏(IA32,AT&T語法)

# How to define a macro: 
.macro write string 
    movl string, %esi 
    call printstr 
.endm 

# How to use a macro: 
write aString 

但是,這對我不起作用。我正在使用gcc來編譯我的代碼。

.data 
    msg: .string "The result is %d.\n" 

.text 
.global main 

.macro add_3 n 
    movl n, %eax 
    addl $3, %eax 
.endm 

main: 
    add_3 $39 
    pushl %eax 
    pushl $msg 
    call printf 
    popl %eax 
    popl %eax 
    movl $1, %eax 
    int $0x80 

當我嘗試編譯,我得到以下錯誤:

undefined reference to `n' 

我究竟在做什麼錯?

+7

嘗試使用宏內部時,它們與前置反斜槓參數名。即'movl \ n,%eax' – Michael

+0

這確實有用,非常感謝! – lambdarookie

+0

哪裏投票? – johnfound

回答

5

在宏中使用它們時,嘗試在反斜槓前加上參數名稱。例如:

movl \n, %eax


來源:Michael