有一个为 Windows API 编写的汇编代码,如何用 Wine 在 Linux 和 运行 上编译它
There is an assembly code written for Windows API, how to compile it on Linux and run with Wine
this introduction中有示例代码,如下所示:
; Sample x64 Assembly Program
; Chris Lomont 2009 www.lomont.org
extrn ExitProcess: PROC ; external functions in system libraries
extrn MessageBoxA: PROC
.data
caption db '64-bit hello!', 0
message db 'Hello World!', 0
.code
Start PROC
sub rsp,28h ; shadow space, aligns stack
mov rcx, 0 ; hWnd = HWND_DESKTOP
lea rdx, message ; LPCSTR lpText
lea r8, caption ; LPCSTR lpCaption
mov r9d, 0 ; uType = MB_OK
call MessageBoxA ; call MessageBox API function
mov ecx, eax ; uExitCode = MessageBox(...)
call ExitProcess
Start ENDP
End
以上代码在hello.asm
和Windows上,可以编译为:
ml64 hello.asm /link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user32.lib /entry:Start
我无法访问 Windows 和 MASM,因为我在 Linux 并使用 NASM。我认为如果我在 Linux 上编译代码,我将能够 运行 它与 Wine
。但是,我无法弄清楚如何在 Linux 上使用 NASM 编译它,而且我也无法弄清楚与 MASM 等价的 NASM 选项是什么。有人可以帮助我吗?
你应该能够找到一个 nasm 语法的 hello world。无论如何,这是一个快速转录:
extern ExitProcess
extern MessageBoxA
section .data
caption db '64-bit hello!', 0
message db 'Hello World!', 0
section .text
sub rsp,28h ; shadow space, aligns stack
mov rcx, 0 ; hWnd = HWND_DESKTOP
lea rdx, [message] ; LPCSTR lpText
lea r8, [caption] ; LPCSTR lpCaption
mov r9d, 0 ; uType = MB_OK
call MessageBoxA ; call MessageBox API function
mov ecx, eax ; uExitCode = MessageBox(...)
call ExitProcess
Assemble 使用 nasm -f win64 hello.asm
。您还需要一个链接器,我将 mingw 端口用作 ld hello.obj -lkernel32 -luser32
(让我强调一下,这不是本机 ld
)
虽然软件包名称因 Linux 发行版而异,但您可以通过安装(或从源代码构建)mingw-w64 工具链和程序 JWASM 来执行您的建议。 JWASM 是一个 assembler,主要与 MASM 兼容。
在基于 Debian 的发行版(包括 Ubuntu)上,您应该能够安装先决条件:
apt-get install mingw-w64-x86-64-dev binutils-mingw-w64-x86-64 jwasm
对于基于 Ubuntu 的系统,您需要在上面的命令前加上 sudo
。
然后您应该能够 assemble 和 link 使用类似的东西:
jwasm -win64 hello.asm
x86_64-w64-mingw32-ld hello.o -lkernel32 -luser32 -o hello.exe
可执行文件应该可以使用 wine64
运行
this introduction中有示例代码,如下所示:
; Sample x64 Assembly Program
; Chris Lomont 2009 www.lomont.org
extrn ExitProcess: PROC ; external functions in system libraries
extrn MessageBoxA: PROC
.data
caption db '64-bit hello!', 0
message db 'Hello World!', 0
.code
Start PROC
sub rsp,28h ; shadow space, aligns stack
mov rcx, 0 ; hWnd = HWND_DESKTOP
lea rdx, message ; LPCSTR lpText
lea r8, caption ; LPCSTR lpCaption
mov r9d, 0 ; uType = MB_OK
call MessageBoxA ; call MessageBox API function
mov ecx, eax ; uExitCode = MessageBox(...)
call ExitProcess
Start ENDP
End
以上代码在hello.asm
和Windows上,可以编译为:
ml64 hello.asm /link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user32.lib /entry:Start
我无法访问 Windows 和 MASM,因为我在 Linux 并使用 NASM。我认为如果我在 Linux 上编译代码,我将能够 运行 它与 Wine
。但是,我无法弄清楚如何在 Linux 上使用 NASM 编译它,而且我也无法弄清楚与 MASM 等价的 NASM 选项是什么。有人可以帮助我吗?
你应该能够找到一个 nasm 语法的 hello world。无论如何,这是一个快速转录:
extern ExitProcess
extern MessageBoxA
section .data
caption db '64-bit hello!', 0
message db 'Hello World!', 0
section .text
sub rsp,28h ; shadow space, aligns stack
mov rcx, 0 ; hWnd = HWND_DESKTOP
lea rdx, [message] ; LPCSTR lpText
lea r8, [caption] ; LPCSTR lpCaption
mov r9d, 0 ; uType = MB_OK
call MessageBoxA ; call MessageBox API function
mov ecx, eax ; uExitCode = MessageBox(...)
call ExitProcess
Assemble 使用 nasm -f win64 hello.asm
。您还需要一个链接器,我将 mingw 端口用作 ld hello.obj -lkernel32 -luser32
(让我强调一下,这不是本机 ld
)
虽然软件包名称因 Linux 发行版而异,但您可以通过安装(或从源代码构建)mingw-w64 工具链和程序 JWASM 来执行您的建议。 JWASM 是一个 assembler,主要与 MASM 兼容。
在基于 Debian 的发行版(包括 Ubuntu)上,您应该能够安装先决条件:
apt-get install mingw-w64-x86-64-dev binutils-mingw-w64-x86-64 jwasm
对于基于 Ubuntu 的系统,您需要在上面的命令前加上 sudo
。
然后您应该能够 assemble 和 link 使用类似的东西:
jwasm -win64 hello.asm
x86_64-w64-mingw32-ld hello.o -lkernel32 -luser32 -o hello.exe
可执行文件应该可以使用 wine64