天天看点

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. 编译运行即可