2013-12-23 44 views
1

你好,我有一個關於lea指令和數組的問題,這工作得很好:內聯彙編LEA與陣列

char *msg = new char[6]; 
msg = "hello"; 

_asm{ 
    push  10h 
    push  0 
    mov   eax, [msg] 
    push  eax 
    push  0 
    call  MessageBox 
    } 

}

可是爲什麼我在這裏得到一個「操作數大小衝突」的錯誤?

char msg2[] = "hey2"; 
_asm{ 
    push  10h 
    push  0 
    mov   eax, [msg2] 
    push  eax 
    push  0 
    call  MessageBox 
} 

爲什麼它會再次與lea一起工作?

char msg2[] = "hey2"; 
_asm{ 
    push  10h 
    push  0 
    lea   eax, [msg2] // <- 
    push  eax 
    push  0 
    call  MessageBox 
} 

回答

1

這裏您試圖取消引用指向char大小的指針,但您嘗試從中加載int。

mov   eax, [msg2] 

不知道這是正確的語法,但你可以使用

mov eax, offset msg3 

這裏,加載地址,或者使用lea指令。

在C這將是類似於:

char msg2[10]; 
char *p = &msg2[0]; 
int x = *p; 

此指令不取消引用指針,它只是需要它的地址,它也可以與某些運營商計算地址。

lea   eax, [msg2] // <- 

是aequivalent的:

char msg2[10]; 
char *p = &msg2[0]; 
+0

謝謝,我認爲這將只是MOV指針。 – Jona

+2

'mov'指令在源和目標之間移動數據。由於大小不匹配字節<>雙字你會得到一個錯誤。 – Devolus