2011-04-05 94 views
0
char greet[] = "hello mate"; 

__asm__("\n\ 
movl foo, %eax\n\ 
"); 

如何將greet[0]移動到%eax等註冊表中?如何在asm中訪問數組?

我的猜測:

char greet[] = "hello mate"; 

__asm__("\n\ 
movl $_greet, %ebx\n\ 
movl (%ebx), %eax\n\ 
"); 

但是,我發現了一個內存錯誤。

+0

'greet [1]'或'greet [0]'?爲什麼它是一個'int []',而不是'char []'? – kennytm 2011-04-05 06:04:50

+0

啊!錯誤。我的不好,讓我解決它。我的腦袋從思想中朦朧起來。 – Strawberry 2011-04-05 06:05:45

回答

1

如果greet是本地變量__asm__將無法​​自動引用它。您可能需要使用匯編模板:

int main() { 
    char greet[] = "hello mate"; 

    __asm__(
     "movzbl (%0), %%eax\n" 
     : : "r"(greet) : "%eax" 
    // ^  ^do not touch %eax 
    //  '- set %0 to a register storing `greet` 
    ); 

    // now %eax should store 'h' (0x68). 

    return 0; 
}