Is there a way to insert assembly code into C?

前端 未结 4 543
独厮守ぢ
独厮守ぢ 2020-12-07 15:57

I remember back in the day with the old borland DOS compiler you could do something like this:

asm {
 mov ax,ex
 etc etc...
}

Is there a se

4条回答
  •  粉色の甜心
    2020-12-07 16:20

    A good start would be reading this article which talk about inline assembly in C/C++:

    http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx

    Example from the article:

    #include 
    
    
    int main() {
        /* Add 10 and 20 and store result into register %eax */
        __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "addl %ebx, %eax;"
        );
    
        /* Subtract 20 from 10 and store result into register %eax */
        __asm__ ( "movl $10, %eax;"
                        "movl $20, %ebx;"
                        "subl %ebx, %eax;"
        );
    
        /* Multiply 10 and 20 and store result into register %eax */
        __asm__ ( "movl $10, %eax;"
                        "movl $20, %ebx;"
                        "imull %ebx, %eax;"
        );
    
        return 0 ;
    }
    

提交回复
热议问题