天天看點

VisualStudio C++使用彙編函數

Visual Studio編寫C++代碼使用彙編函數

  1. 建立空項目
  2. 建立如下源檔案
    ;test.asm
    ;測試函數   三個數相加  
    ;.386
    .model flat, c
    ;public test_
    
    .code
    
    test_ proc
    
    ;初始化棧幀指針
        push ebp
        mov ebp,esp
    ;加載參數值
        mov eax,[ebp+8]
        mov ecx,[ebp+12]
        mov edx,[ebp+16]
    
    ;求和
        add eax,ecx
        add eax,edx
    
    ;恢複父函數的棧幀指針
    
        pop ebp
        ret
    
    
    test_ endp
    end
               
    //main.cpp
    #include <stdio.h>
    #include <stdlib.h>
    
    extern "C" int test_(int a, int b, int c);
    
    int main() {
    	int a = 17;
    	int b = 20;
    	int c = 19;
    	int sum = test_(a, b, c);
    	printf("c = %d\n", sum);
    
    	system("pause");
    	return 0;
    }
               
  3. 更改

    asm

    檔案的屬性如下
VisualStudio C++使用彙編函數
  1. 繼續更改自定義生成工具
    VisualStudio C++使用彙編函數
  2. 指令行與輸出添加如下條件語句

    指令行填寫:

    ml /c /coff %(fileName).asm

    ml

    其實是指 編譯器

    ml.exe

    後邊跟的是指令

    \c

    隻編譯,不連結

    \coff

    生成這種格式的目标檔案

    輸出填寫:

    %(fileName).obj;%(OutPuts)

  3. 編譯運作即可