Visual Studio 2017的简单组装程序 [英] Simple Assembly program on visual studio 2017

查看:137
本文介绍了Visual Studio 2017的简单组装程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        .386
        .model flat, c
        .stack 100h
printf  PROTO arg1:Ptr Byte

        .data
msg1    byte "Hello World!", 0Ah, 0

        .code
main    proc
        INVOKE printf, ADDR msg1
        ret

main    endp
        end main

我收到以下错误消息:

我四处搜索,发现有人说可以通过链接Microsoft运行时库来解决此问题

I searched around and found someone said that it can be fixed by linking the microsoft runtime library

任何人都可以教我如何完全解决问题吗?

Can anyone teach me how can I exactly fix it?

谢谢

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol _printf referenced in function _main testing C:\Users\Kin\Desktop\assembly\testing\testing\Source.obj    1   
Error   LNK1120 1 unresolved externals  testing C:\Users\Kin\Desktop\assembly\testing\Debug\testing.exe 1   

推荐答案

我没有安装VS 2017来尝试此操作. 重要:请确保您创建的是Console Application而不是Windows Application.创建该项目后,请确保将 MASM 添加到构建自定义项中.将.ASM文件添加到您的项目中.

I don't have VS 2017 installed to try this. Important: Make sure you create a Console Application and not a Windows Application. Once you create this project make sure MASM is added to the build customizations. Add an .ASM file to your project.

获取您的代码,并在顶部插入以下行:

Take your code and insert the following lines at the top:

includelib libcmt.lib
includelib libvcruntime.lib
includelib libucrt.lib
includelib legacy_stdio_definitions.lib

有关此原因的说明,请参见 Stackoverflow答案.

An explanation as to why these lines are needed in Visual Studio later than 2013 can be found in this Stackoverflow Answer.

您希望 C 运行时成为控制台应用程序的入口点(依次调用main).因此,您必须从最后一行end main删除main.当您执行end main时,它将绕过 C 运行时启动启动.未能正确初始化 C 运行时,可能会导致在您调用printf之类的程序时崩溃.它应该只是end而不是end main.

You want the C runtime to be the entry point to your console application (which in turn will call your main). Because of this you MUST remove main from the last line that says end main. When you do end main it bypasses the C runtime startup startup. Failure to properly initialize the C runtime will likely lead to the program crashing when you make calls like printf. It should simply be end and not end main.

您应该测试的最终代码是:

The final code you should test is:

includelib libcmt.lib
includelib libvcruntime.lib
includelib libucrt.lib
includelib legacy_stdio_definitions.lib

        .386
        .model flat, c
        .stack 100h
printf  PROTO arg1:Ptr Byte

        .data
msg1    byte "Hello World!", 0Ah, 0

        .code
main    proc
        INVOKE printf, ADDR msg1
        ret

main    endp
        end

这篇关于Visual Studio 2017的简单组装程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆