首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用“`nasm`”和“`alink`”的组合链接kernel32.lib和user32.lib?

如何使用“`nasm`”和“`alink`”的组合链接kernel32.lib和user32.lib?
EN

Stack Overflow用户
提问于 2016-07-06 10:46:52
回答 1查看 1.7K关注 0票数 1

如何使用nasmalink组合链接kernel32.lib和user32.lib

我正在学习一些关于程序集编程的教程,指南希望我执行以下命令:

代码语言:javascript
复制
nasm  -fobj hello.asm                                    
alink -oPE hello \lib\kernel32.lib \lib\user32.lib

第一个命令按预期执行,但是第二个命令失败。

为了链接.lib文件,我从

代码语言:javascript
复制
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib

进入我现在的文件夹。

执行第二个命令时收到的错误消息是:

代码语言:javascript
复制
Loading file hello.obj
Loading file Kernel32.lib
2327 symbols
Loaded first linker member
Loading file User32.lib
1385 symbols
Loaded first linker member
matched Externs
matched ComDefs
Unresolved external MessageBoxA
Unresolved external ExitProcess

现在,我有两个问题:

1)内核32.lib和user32.lib位于哪里?

2)如何正确链接这些库文件?

操作系统是Windows 10 (64位).

更新:

代码语言:javascript
复制
; Coded for NASM                                           ;
; nasm  -fobj hello.asm                                    ;
; alink -oPE hello \lib\kernel32.lib \lib\user32.lib       ;
                                                           ;
extern MessageBoxA              ; APIs used                ;
extern ExitProcess              ; in this file             ;
                                                           ;
[SECTION CODE USE32 CLASS=CODE] ; code section             ;
..start:                        ; for the linker           ;
                                                           ;
    push byte 0                 ; only the buttons 'OK'    ; 
    push dword caption          ; caption of the BOX       ;
    push dword text             ; text in the BOX          ;
    push byte 0                 ; handle of the Box        ;
      call MessageBoxA          ; print BOX on screen      ;
                                                           ;
    push byte 0                 ;                          ;
      call ExitProcess          ; EXIT                     ;
                                                           ;
    caption db "Your first WIN32 programm",0               ;
    text db "HELLO",0                                      ;
                                                           ;
end                             ; for the linker
EN

回答 1

Stack Overflow用户

发布于 2017-04-22 05:44:34

我找到了一个kernel.libuser.lib,可以供ALINK使用。这可能是由于所需的.obj文件的格式,而大多数Windows.obj的格式都是在COFF ALINK中格式化的。

合适的WIN32.LIB这里。它包括MessageBoxA,但不包括ExitProcess。不建议使用简单的RET来终止纯Windows程序。

然而,NASM如果不是更好的话,也可以做得更好:

代码语言:javascript
复制
; Import the needed Win32 API functions.- http://www.nasm.us/doc/nasmdoc7.html#section-7.4.4
IMPORT ExitProcess kernel32.dll
IMPORT MessageBoxA user32.dll

; Still needed to be declared as external
EXTERN ExitProcess, MessageBoxA

[SECTION CODE USE32 CLASS=CODE] ; code section
..start:
    push 0                  ; only the buttons 'OK'
    push dword caption      ; caption of the BOX
    push dword text         ; text in the BOX 
    push 0                  ; handle of the Box
    call [MessageBoxA]      ; print BOX on screen

    push 0
    call [ExitProcess]

    caption db "Your first WIN32 programm",0
    text db "HELLO",0

请注意,函数在调用时使用括号进行修饰。此外,最好将变量放在单独的数据部分中。

如果您计划使用一堆从..DLL的tahe导入的大型项目,请看一下NASMX项目

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38222139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档