2012-04-29 31 views
1

以下代碼應該在Linux上運行。我想將以下代碼移植到Visual Studio 2008中,但我對asm代碼不是很熟悉。你可以幫幫我嗎?如何將ASM代碼移植到VS 2008

#include <stdint.h> 
static inline uint32_t log2(const uint32_t x) { 
    uint32_t y; 
    asm ("\tbsr %1, %0\n" 
    : "=r"(y) 
    : "r" (x) 
    ); 
    return y; 
} 

回答

2

GCC使用AT&T syntax。 MSVC使用英特爾語法。這個函數會是這個樣子(編譯與2010年MSVC/16.00.40219.01,但我不明白爲什麼它會失敗於2008年):

static inline uint32_t log2(const uint32_t x) { 
    uint32_t y; 
    __asm { 
    bsr eax, x 
    mov y, eax 
    } 
    return y; 
} 
+0

你並不需要保存/恢復'eax'在ASM塊在MSVC中:http://msdn.microsoft.com/en-us/library/k1a8ss06%28v=vs.90%29.aspx –

+0

@MichaelBurr:謝謝,不知道。 – DCoder