Unheilig
Unheilig

Reputation: 16302

Xcode: running ASM

I have this ASM that I have been trying to run in Xcode without success:

_asm
{
    push eax
    push ebx
    push ecx
    mov eax,[A]
    mov ebx,[B]
    xor eax,ebx
    mov ecx,eax
    xor ecx,ebx
    mov ebx,ecx
    xor eax,ebx
    mov [A],eax 
    mov [B],ebx 
    pop eax 
    pop ebx 
    pop ecx 
}

I have tried changing it to __asm__ but the error is still persistent:

inline asm:6:2: Unknown use of instruction mnemonic without a size suffix

I have searched online for hours but none seems to answer my question.

I am running Xcode 5.0.2.

Does anyone know what I may need to set in Xcode in order to run this?

Upvotes: 13

Views: 4449

Answers (1)

Ross Ridge
Ross Ridge

Reputation: 39621

The equivalent GCC-style asm statement would be:

__asm__(
    "push %%eax\n\t"
    "push %%ebx\n\t"
    "push %%ecx\n\t"
    "mov %0,%%eax\n\t"
    "mov %1,%%ebx\n\t"
    "xor %%ebx,%%eax\n\t"
    "mov %%eax,%%ecx\n\t"
    "xor %%ebx,%%ecx\n\t"
    "mov %%ecx,%%ebx\n\t"
    "xor %%ebx,%%eax\n\t"
    "mov %%eax,%0\n\t"
    "mov %%ebx,%0\n\t"
    "pop %%ecx\n\t"
    "pop %%ebx\n\t"
    "pop %%eax"
    : "+m" (A), "+m" (B));

(I've corrected an apparent bug by popping the saved values on the stack back into their original registers.)

I'm not sure what the point of this assembly statement is though. It goes to a lot of effort to do something that could be done much more efficiently in three lines of C/C++ code:

temp = A;
A = B;
B = temp;

If I were to pretend though that it did something useful, and not just swap two variables, then I would suggest getting rid of the apparently unimportant PUSH/POP instructions like this:

__asm__(
    "xor %1,%0\n\t"
    "mov %0,%2\n\t"
    "xor %1,%2\n\t"
    "mov %2,%1\n\t"
    "xor %1,%0"
    : "+r" (A), "+r" (B), "=r" (temp));

Upvotes: 10

Related Questions