同时编译 C++ 和 MASM

问题描述 投票:0回答:0

我正在做一个关于 MASM 的在线课程的练习,特别是一个同时使用 C++ 和 MASM 的项目。

C++文件的代码是这样的:

#include <stdlib.h>
#include <iostream>

extern "C" void reverser(int *y, const int *x, int n);

int main() {
    using std::cout;
    using std::endl;

    const int n = 10;
    int x[n], y[n];

    for(int i = 0; i < n; i++)
        x[i] = i;

    reverser(y, x, n);

    for(int i = 0x0; i < n; i++) {
        printf("x: %5d     y: %5d", x[i], y[i]);
    }

    return 0;
}

MASM文件的代码是这样的:

.386
.model flat, c
.code

reverser    proc
            push ebp
            mov ebp,esp
            push esi
            push edi        ; Function Prologue

            xor eax,eax
            mov edi,[ebp+8]
            mov esi,[ebp+12]
            mov ecx,[ebp+16]
            test ecx,ecx

            lea esi,[esi+ecx*4-4]
            pushfd
            std

@@:         lodsd
            mov [edi], eax
            add edi,4
            dec ecx
            jnz @B

            popfd
            mov eax,1

            pop edi
            pop esi
            pop ebp

            ret

reverser endp
         end

在在线课程中,讲者使用 Visual Studio 编译它没有任何问题,但是如果我尝试使用 GCC 编译 .cpp 文件,我得到一个错误:

对“反向器”的未定义引用

所以我尝试添加

#include "reverser.asm"

代码,但后来我得到错误:

reverser.asm:1:error: expected unqualified-id before numeric constant
reverser.asm:1:error: expected ',' or ';' before numeric constant
reverser.asm:9:error: 'Prologo' does not name a type
reverser.asm:21:error: stray '@' in program

所以我有以下问题:

  1. 我该如何解决这个问题?
  2. 如何在 Windows 上使用命令行将这两个文件编译在一起?喜欢用masm.exegcc.exe来编译
  3. 为什么在课程中编译没有问题
c++ gcc x86 masm
© www.soinside.com 2019 - 2024. All rights reserved.