在的HelloWorld Win32汇编 [英] HelloWorld in Win32 Assembly

查看:307
本文介绍了在的HelloWorld Win32汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建只使用MASM而不是MASM32库一个HelloWorld程序。这里是code片断:

  .386
.MODEL平,STDCALL
选项​​casemap:无EXTRN的MessageBox:PROC
EXTRN了ExitProcess:PROC。数据
        的HelloWorld DB你好!,0。code
开始:        LEA EAX,的HelloWorld
        MOV EBX,0
        推EBX
        推EAX
        推EAX
        推EBX
        调用的MessageBox
        推EBX
        调用了ExitProcess年底启动

我能够组装这一点使用MASM:

  C:\\ MASM32 \\ code>毫升/ C / COFF demo.asm
微软(R)宏汇编版本9.00.21022.08
版权所有(C)微软公司。版权所有。 组装:demo.asm

不过,我无法将其链接:

  C:\\ MASM32 \\ code>链接/子系统:窗户/defaultlib:kernel32.lib / defaultlib:用户
32.lib demo.obj
微软(R)增量链接器版本9.00.21022.08
版权所有(C)微软公司。版权所有。demo.obj:错误LNK2001:无法解析的外部符号_MessageBox
demo.obj:错误LNK2001:无法解析的外部符号_ExitProcess
DEMO.EXE:致命错误LNK1120:2无法解析的外部

我包括库链接过程中,所以不知道为什么它仍然悬而未决说的符号?

更新:

  C:\\ MASM32 \\ code>链接/子系统:窗户/defaultlib:kernel32.lib / defaultlib:用户
32.lib demo.obj
微软(R)增量链接器版本9.00.21022.08
版权所有(C)微软公司。版权所有。demo.obj:错误LNK2001:解析外部符号_MessageBox @ 16
DEMO.EXE:致命错误LNK1120:1无法解析的外部

更新2:最后的工作code

  .386
.MODEL平,STDCALL
选项​​casemap:无EXTRN MessageBoxA @ 16:PROC
EXTRN ExitProcess的@ 4:PROC。数据
        的HelloWorld DB你好!,0。code
开始:        LEA EAX,的HelloWorld
        MOV EBX,0
        推EBX
        推EAX
        推EAX
        推EBX
        调用MessageBoxA @ 16
        推EBX
        调用了ExitProcess @ 4年底启动


解决方案

正确的函数名是 MessageBoxA @ 16 ExitProcess的@ 4

几乎所有的Win32 API函数是STDCALL,所以他们的名字与装饰 @ 标志中,然后用它们的参数占用的字节数。

此外,当一个Win32函数采用字符串,有两个变种:一个是需要一个ANSI字符串(名称 A 结尾),一个采用一个统一code字符串(名称是W 结尾)。你提供一个ANSI字符串,所以你要在 A 版本。

当你没有在汇编语言编程,编译器会处理这些点的给你。

I am trying to create a helloworld program using only masm and not masm32 libs. Here is the code snippet:

.386
.model flat, stdcall
option casemap :none

extrn MessageBox : PROC
extrn ExitProcess : PROC

.data
        HelloWorld db "Hello There!", 0

.code
start:

        lea eax, HelloWorld
        mov ebx, 0
        push ebx
        push eax
        push eax
        push ebx
        call MessageBox
        push ebx
        call ExitProcess

end start

I am able to assemble this using masm:

c:\masm32\code>ml /c /coff demo.asm
Microsoft (R) Macro Assembler Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: demo.asm

However, I am unable to link it:

c:\masm32\code>link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user
32.lib demo.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

demo.obj : error LNK2001: unresolved external symbol _MessageBox
demo.obj : error LNK2001: unresolved external symbol _ExitProcess
demo.exe : fatal error LNK1120: 2 unresolved externals

I am including the libs during linking, so not sure why it still says unresolved symbols?

UPDATE:

c:\masm32\code>link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user
32.lib demo.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

demo.obj : error LNK2001: unresolved external symbol _MessageBox@16
demo.exe : fatal error LNK1120: 1 unresolved externals

UPDATE 2: Final working code!

.386
.model flat, stdcall
option casemap :none

extrn MessageBoxA@16 : PROC
extrn ExitProcess@4 : PROC

.data
        HelloWorld db "Hello There!", 0

.code
start:

        lea eax, HelloWorld
        mov ebx, 0
        push ebx
        push eax
        push eax
        push ebx
        call MessageBoxA@16
        push ebx
        call ExitProcess@4

end start

解决方案

The correct function names are MessageBoxA@16 and ExitProcess@4.

Almost all Win32 API functions are stdcall, so their names are decorated with an @ sign, followed by the number of bytes taken up by their parameters.

Additionally, when a Win32 function takes a string, there are two variants: one that takes an ANSI string (name ends in A) and one that takes a Unicode string (name ends in W). You're supplying an ANSI string, so you want the A version.

When you're not programming in assembly the compiler takes care of these points for you.

这篇关于在的HelloWorld Win32汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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