Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode: running ASM

Tags:

xcode

assembly

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?

like image 797
Unheilig Avatar asked Jan 09 '14 20:01

Unheilig


People also ask

Is ASM still used?

Though considered lower level languages compared to more advanced languages, assembly languages are still used. Assembly language is used to directly manipulate hardware, access specialized processor instructions, or evaluate critical performance issues.

What is ASM function?

The asm statement allows you to include assembly instructions directly within C code. This may help you to maximize performance in time-sensitive code or to access assembly instructions that are not readily available to C programs. Note that extended asm statements must be inside a function.


1 Answers

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));
like image 110
Ross Ridge Avatar answered Oct 03 '22 20:10

Ross Ridge