使用 MASM 和 Visual Studio 2019 链接多个文件

Linking multiple files with MASM and Visual Studio 2019

我正在尝试编写一个程序,在一定程度上吐出斐波那契序列,但使用递归和多个 linked 文件。到目前为止,我的方法是在单个文件中编写程序并使其运行,然后将其转换为多个文件。这一直有效,直到我尝试使用 invokeproto 指令来 link 文件。我什至不知道从哪里开始。

这是我在单个文件中尝试过并知道有效的方法:

INCLUDE Irvine32.inc

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data
deg DWORD ?
prompt BYTE "Enter the degree of Fibonacci sequence: ",0

.code
main PROC
    mov edx,OFFSET prompt
    call WriteString
    call ReadInt
    mov deg,eax
    mov ecx,0
    push deg
    call Fibonacci
    add esp,4

    call WriteDec
    call Crlf

    INVOKE ExitProcess,0
main ENDP

Fibonacci PROC
    add ecx,1
    push ebp
    mov ebp,esp
    sub esp,4
    mov eax,[ebp+8]

    cmp eax,2
    je L1
    cmp eax,1
    je L1

    dec eax
    push eax
    call Fibonacci
    mov [ebp-4],eax

    dec DWORD PTR [esp]
    call Fibonacci
    add esp,4

    add eax,[ebp-4]

    jmp EndFib

L1:
    mov eax,1

EndFib:
    mov esp,ebp
    pop ebp
    ret

Fibonacci ENDP

END main

我知道上面的代码可以编译,但是尝试将其拆分为多个文件总是会导致构建错误。这是我试图开始工作的 3 个文件:

main.asm

INCLUDE Irvine32.inc
INCLUDE external.inc

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD

.data


.code
main PROC
    mov ecx,0
    push 3
    invoke Fibonacci
    add esp,4

    call WriteDec
    call Crlf

    INVOKE ExitProcess,0
main ENDP

END main

fib.asm

INCLUDE Irvine32.inc
INCLUDE external.inc

.code
Fibonacci PROC

    add ecx,1
    push ebp
    mov ebp,esp
    sub esp,4
    mov eax,[ebp+8]

    cmp eax,2
    je L1
    cmp eax,1
    je L1

    dec eax
    push eax
    call Fibonacci
    mov [ebp-4],eax

    dec DWORD PTR [esp]
    call Fibonacci
    add esp,4

    add eax,[ebp-4]

    jmp EndFib

L1:
    mov eax,1
    ; dec eax

EndFib:
    mov esp,ebp
    pop ebp
    ret

Fibonacci ENDP
END

external.inc

INCLUDE Irvine32.inc

Fibonacci PROTO

当这失败时,我得到一个window,上面写着

There were build errors. Would you like to continue and run the last successful build?

我的朋友告诉我,我应该收到错误消息,告诉我程序在哪一行出错,但我只看到这个和我将放在下面的构建输出。这正常吗?

1>------ Build started: Project: ModSum, Configuration: Debug Win32 ------
1>Assembling main.asm...
1>c:\irvine\SmallWin.inc(11): warning A4011: multiple .MODEL directives found : .MODEL ignored
1>c:\irvine\SmallWin.inc(245): error A2163: non-benign structure redefinition: incorrect initializers : CONSOLE_SCREEN_BUFFER_INFO
1>c:\irvine\SmallWin.inc(246): error A2163: non-benign structure redefinition: incorrect initializers : CONSOLE_SCREEN_BUFFER_INFO
1>c:\irvine\SmallWin.inc(248): error A2163: non-benign structure redefinition: incorrect initializers : CONSOLE_SCREEN_BUFFER_INFO
1>c:\irvine\SmallWin.inc(249): error A2163: non-benign structure redefinition: incorrect initializers : CONSOLE_SCREEN_BUFFER_INFO
1>c:\irvine\SmallWin.inc(258): error A2163: non-benign structure redefinition: incorrect initializers : KEY_EVENT_RECORD
1>c:\irvine\SmallWin.inc(261): error A2163: non-benign structure redefinition: incorrect initializers : KEY_EVENT_RECORD
1>c:\irvine\SmallWin.inc(262): error A2161: non-benign structure redefinition: too few labels : KEY_EVENT_RECORD
1>c:\irvine\SmallWin.inc(262): error A2163: non-benign structure redefinition: incorrect initializers : KEY_EVENT_RECORD
1>c:\irvine\SmallWin.inc(265): error A2163: non-benign structure redefinition: incorrect initializers : MOUSE_EVENT_RECORD
1>c:\irvine\SmallWin.inc(272): error A2163: non-benign structure redefinition: incorrect initializers : WINDOW_BUFFER_SIZE_RECORD
1>c:\irvine\SmallWin.inc(287): error A2163: non-benign structure redefinition: incorrect initializers : INPUT_RECORD
1>c:\irvine\SmallWin.inc(288): error A2163: non-benign structure redefinition: incorrect initializers : INPUT_RECORD
1>c:\irvine\SmallWin.inc(289): error A2163: non-benign structure redefinition: incorrect initializers : INPUT_RECORD
1>c:\irvine\SmallWin.inc(290): error A2163: non-benign structure redefinition: incorrect initializers : INPUT_RECORD
1>c:\irvine\SmallWin.inc(290): error A2164: non-benign structure redefinition: too few initializers : INPUT_RECORD
1>c:\irvine\SmallWin.inc(291): error A2164: non-benign structure redefinition: too few initializers : INPUT_RECORD
1>c:\irvine\SmallWin.inc(293): error A2161: non-benign structure redefinition: too few labels : INPUT_RECORD
1>c:\irvine\SmallWin.inc(293): error A2164: non-benign structure redefinition: too few initializers : INPUT_RECORD
1>main.asm(7): warning A4011: multiple .MODEL directives found : .MODEL ignored
1>MASM : fatal error A1016: Internal error
1>C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\masm.targets(70,5): error MSB3721: The command "ml.exe /c /nologo /Zi /Fo"Debug\main.obj" /I "c:\irvine" /W3 /errorReport:prompt  /Tamain.asm" exited with code -1073741819.
1>Done building project "ModSum.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

非常感谢任何能抽出时间或提供帮助的人,我已经自己研究了一段时间,但无法弄清楚。

我认为 external.inc 不应包含 Irvine32.inc,因为 .asm 文件包含两者。最终结果是 Irvine32.inc 被每个 .asm 文件包含两次。