1

There is an example code in this introduction, like below:

; 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

The above code is inside hello.asm and on Windows, it can be compiled with:

ml64 hello.asm /link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user32.lib /entry:Start

I don't have access to Windows and MASM, since I'm on Linux and work with NASM. I think if I compile the code on Linux, I would be able to run it with Wine. But yet, I couldn't figure out how to compile it with NASM on Linux and also I couln't figure out what are the NASM options which are equivalent to the MASM ones. Can anybody help me?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Megidd
  • 7,089
  • 6
  • 65
  • 142
  • 2
    You should be able to find a nasm syntax hello world. – Jester Mar 13 '18 at 14:52
  • I recommend reading some other thing as assembler tutorial. BTW, see also [Assembler HowTo](http://www.tldp.org/HOWTO/Assembly-HOWTO/) and read [syscalls(2)](http://man7.org/linux/man-pages/man2/syscalls.2.html) – Basile Starynkevitch Mar 13 '18 at 14:52
  • @BasileStarynkevitch I bookmarked those links. I'm going to study them. Thanks. – Megidd Mar 13 '18 at 15:08

2 Answers2

7

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)

Jester
  • 56,577
  • 4
  • 81
  • 125
  • Related: [a 32-bit NASM hello world with a Linux `sys_write` system call](https://stackoverflow.com/questions/39550402/what-parts-of-this-helloworld-assembly-code-are-essential-if-i-were-to-write-the/39551489#39551489). It prints to its `stdout` (i.e. on a terminal) instead of calling a GUI message box function, using native Linux system calls, no WINE needed. It's heavily commented and with separate paragraphs describing how it works. I wrote it for SO docs, then ported it to an answer where it fits when SO docs shut down. – Peter Cordes Mar 14 '18 at 03:54
5

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

Michael Petch
  • 46,082
  • 8
  • 107
  • 198