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编译它,而且我也无法弄清楚哪些NASM选项等同于MASM选项。有人能帮助我吗?
答案 0 :(得分:5)
您应该能够找到一个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
使用nasm -f win64 hello.asm
进行汇总。您还需要一个链接器,我将mingw端口用作ld hello.obj -lkernel32 -luser32
(让我强调这不是原生的ld
)
答案 1 :(得分:4)
虽然软件包名称从Linux发行版到发行版不等,但您可以通过安装(或构建源代码)mingw-w64工具链和程序JWASM来执行您的建议。 JWASM是一个主要与MASM兼容的汇编程序。
在基于Debian的发行版(包括Ubuntu)上,您应该能够安装先决条件:
apt-get install mingw-w64-x86-64-dev binutils-mingw-w64-x86-64 jwasm
使用基于Ubuntu的系统,您需要在sudo
之前添加上述命令。
然后,您应该能够使用以下内容进行汇编和链接:
jwasm -win64 hello.asm
x86_64-w64-mingw32-ld hello.o -lkernel32 -luser32 -o hello.exe
可执行文件应该可以使用wine64