2010-04-13 89 views
2

我有一個方法(C++),它返回一個字符並將字符數組作爲其參數。在嵌入式x86彙編中使用數組?

我第一次搞亂大會,只是試圖返回數組的第一個字符在dl寄存器。這是我到目前爲止有:

char returnFirstChar(char arrayOfLetters[]) 
{ 
char max; 

__asm 
{ 
    push eax 
     push ebx 
     push ecx 
    push edx 
    mov dl, 0 

    mov eax, arrayOfLetters[0] 
    xor edx, edx 
    mov dl, al 

    mov max, dl  
    pop edx 
    pop ecx 
    pop ebx 
    pop eax 

} 

return max; 
} 

出於某種原因,怎麼回事這個方法返回一個♀

任何想法?由於

+3

另外,從你的asm語法中,我猜你正在使用Visual Studio。你明白,你不需要推送和彈出你將要使用的寄存器。 Visual Studio自動執行此操作,因此您將使用的堆棧空間加倍... – Goz 2010-04-13 08:28:41

+0

@Mark V. - 僅供參考,您可以使用'pusha'和'popa'來推送* all *和pop * all *寄存器。 – IAbstract 2010-04-15 15:58:38

回答

4

組件的行:

mov eax, arrayOfLetters[0] 

移動一個指針字符數組eax(注意,這不是arrayOfLetters[0]將會用C做,但裝配不C)。

你需要添加下面的右它使您的裝配工作的點點後:

mov al, [eax] 
4

那麼這裏是我怎麼會寫這個函數:

char returnFirstChar(const char arrayOfLetters[]) 
{ 
    char max; 
    __asm 
    { 
     mov eax, arrayOfLetters ; Move the pointer value of arrayOfLetters into eax. 
     mov dl, byte ptr [eax] ; De-reference the pointer and move the byte into eax. 
     mov max, dl    ; Move the value in dl into max. 
    } 
    return max; 
} 

那似乎完美地工作。

注:

1)正如我說我的意見,你不需要推棧上的寄存器,讓MSVC搞定。
2)不要打擾清除edx通過X'or它反對它自己或不設置dl爲0.兩者都會實現相同的事情。所有你甚至不需要這樣做,因爲你可以用你的值覆蓋存儲在dl中的值。

+0

+1:很容易理解。 – IAbstract 2010-04-15 16:03:32