There is an assembly code written for Windows API, how to compile it on Linux and run with Wine

坚强是说给别人听的谎言 提交于 2019-12-02 05:47:59

You should have been able to find a nasm syntax hello world. Anyway, here is a quick transcription:

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 using nasm -f win64 hello.asm. You will also need a linker, I used the mingw port as ld hello.obj -lkernel32 -luser32 (let me emphasize this is not the native ld)

Although package names vary from Linux distro to distro, you can do what you are suggesting by installing (or building from source) a mingw-w64 tool chain and the program JWASM. JWASM is a an assembler that is mostly compatible with MASM.

On Debian based distros (including Ubuntu) you should be able to install the prerequisites with:

apt-get install mingw-w64-x86-64-dev binutils-mingw-w64-x86-64 jwasm

With Ubuntu based systems you'll need to prepend the command above with sudo.

You should then be able to assemble and link using something like:

jwasm -win64 hello.asm
x86_64-w64-mingw32-ld hello.o -lkernel32 -luser32 -o hello.exe

The executable should be runnable using wine64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!