Error C2432 illegal reference to 16-bit data in 'second operand' of __asm

两盒软妹~` 提交于 2020-01-30 13:11:50

问题


In Visual Studio, I get this error when I compile my __asm in C. Does anybody know what is wrong with this code? I tried everything, but nothing works. I am trying to implement the bubble sort in assembly.

unsigned short i = 0;
unsigned short j = 0;
unsigned short max = short(N-2);


unsigned short tab[5];
tab[0] = 54;
tab[1] = 123;
tab[2] = 342;
tab[3] = 5436;
tab[4] = 1234;

unsigned short a = 0;

__asm {
loop1:
    inc i
    mov j, 0

        mov si, tab

        loop2:
            mov ax, [si] // <- Error C2432 on this line 
            mov a, ax

            inc j
            mov ax, j
            mov bx, max
            cmp ax, bx
            jz cdnloop2
        loop loop2
        cdnloop2:
    mov ax, i
    mov bx, max
    cmp ax, bx
    jz endof

    loop loop1  
endof :
}

回答1:


Google the error message. The answer is right there in MS's documentation (the first google hit).

illegal reference to 16-bit data in 'identifier'

A 16-bit register is used as an index or base register. The compiler does not support referencing 16-bit data. 16-bit registers cannot be used as index or base registers when compiling for 32-bit code.

The first paragraph is a bit confusing, because it sounds like the problem is a 16bit operand size, rather than the 16bit address size. But the second paragraph makes it clear: it refuses to use the address-size prefix to assemble something like mov ax, [si], because ignoring the upper16 of an address is not a useful thing to ever do in inline asm.

They've decided it's better to catch typos / errors like that at compile time than to emit code that crashes.

Probably just change the line to mov ax, [tab]. You're not gaining anything from storing the address in esi, since you don't modify it.



来源:https://stackoverflow.com/questions/33836812/error-c2432-illegal-reference-to-16-bit-data-in-second-operand-of-asm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!