title: How to print to console in 64-Bit Windows NASM in without stdio or Visual Studio?
body: <p>I am trying to write a 64-bit NASM assembly program in Windows to simply print some output to the console.  I would like to do this with the most pure assembly possible, so that excludes any standard C libraries.  I'm also trying to automate the assembly and linking process, so opening Visual Studio and doing it manually is not an option.</p>
  
  <p>I know that I have to use the Windows APIs like _GetStdHandle and _WriteFile, but I can't figure out how to link those libraries, or how to use them in a 64-bit program.</p>
  
  <p>This is the "Hello World" assembly code I am trying to run:</p>
  
  <pre><code>    global _main
      extern  _GetStdHandle@4
      extern  _WriteFile@20
      extern  _ExitProcess@4
  
      section .text
  _main:
      ; DWORD  bytes;    
      mov     ebp, esp
      sub     esp, 4
  
      ; hStdOut = GetstdHandle( STD_OUTPUT_HANDLE)
      push    -11
      call    _GetStdHandle@4
      mov     ebx, eax    
  
      ; WriteFile( hstdOut, message, length(message), &amp;bytes, 0);
      push    0
      lea     eax, [ebp-4]
      push    eax
      push    (message_end - message)
      push    message
      push    ebx
      call    _WriteFile@20
  
      ; ExitProcess(0)
      push    0
      call    _ExitProcess@4
  
      ; never here
      hlt
  message:
      db      'Hello, World', 10
  message_end:
  </code></pre>
  
  <p>But I'm not sure how to link the Windows 64-bit APIs to the program.  I have also been getting this error: "instruction not supported in 64-bit mode" for the instruction "push eax".  I'm just not sure how to re-write this code for 64-bit.</p>
  
  <p>This is the batch program I am using to assemble and link the program:</p>
  
  <pre><code>nasm -f win64 HelloWorld.asm -o HelloWorld.obj &amp;&amp;^
   "C:\Program Files\GoLink\GoLink.exe" /console /entry _main HelloWorld.obj /fo HelloWorld.exe &amp;&amp;^
   HelloWorld.exe
  </code></pre>
  
  <p>Can anyone walk me through the steps required to get this program to run with the given constraints?</p>