title: FASM write Hello World to console with NO includes or dependencies at all
body: <p>I've seen</p>
  <p><a href="https://stackoverflow.com/questions/1023593/how-to-write-hello-world-in-assembler-under-windows">How to write hello world in assembler under Windows?</a></p>
  <p>and</p>
  <p><a href="https://stackoverflow.com/questions/55431211/writing-hello-world-to-console-in-fasm-with-dos">Writing hello,world to console in Fasm with DOS</a></p>
  <p><a href="https://stackoverflow.com/questions/2540478/how-to-write-to-the-console-in-fasm/2568311#2568311">How to write to the console in fasm?</a></p>
  <p>I've tried / seen code like this MASM example from <a href="https://stackoverflow.com/questions/1023593/how-to-write-hello-world-in-assembler-under-windows/1032422#1032422">this answer</a></p>
  <pre><code>;---ASM Hello World Win64 MessageBox
  
  extrn MessageBoxA: PROC
  extrn ExitProcess: PROC
  
  .data
  title db 'Win64', 0
  msg db 'Hello World!', 0
  
  .code
  main proc
    sub rsp, 28h  
    mov rcx, 0       ; hWnd = HWND_DESKTOP
    lea rdx, msg     ; LPCSTR lpText
    lea r8,  title   ; LPCSTR lpCaption
    mov r9d, 0       ; uType = MB_OK
    call MessageBoxA
    add rsp, 28h  
    mov ecx, eax     ; uExitCode = MessageBox(...)
    call ExitProcess
  main endp
  
  End
  </code></pre>
  <p>(to which I get an error &quot;Illegal instruction&quot; on windows 64 bit <code>extrn MessageBoxA:PROC</code> because FASM doesn't understand that MASM directive.)</p>
  <p>also this FASM example <a href="https://stackoverflow.com/questions/37671413/push-pop-eax-gives-me-illegal-instruction-at-compile-time">from this question</a></p>
  <pre><code> ; Example of 64-bit PE program
  
  
  format PE64 GUI 
  entry start 
  
  section '.text' code readable executable 
  
    start: 
        sub     rsp,8*5         ; reserve stack for API use and make stack dqword aligned 
  
      mov     r9d,0 
      lea     r8,[_caption] 
      lea     rdx,[_message] 
      mov    rcx,0 
      call    [MessageBoxA] 
  
      mov     ecx,eax 
      call    [ExitProcess] 
  
  section '.data' data readable writeable 
  
    _caption db 'Win64 assembly program',0 
    _message db 'Hello World!',0 
  
  section '.idata' import data readable writeable 
  
    dd 0,0,0,RVA kernel_name,RVA kernel_table 
    dd 0,0,0,RVA user_name,RVA user_table 
    dd 0,0,0,0,0 
  
    kernel_table: 
      ExitProcess dq RVA _ExitProcess 
      dq 0 
    user_table: 
      MessageBoxA dq RVA _MessageBoxA 
      dq 0 
  
    kernel_name db 'KERNEL32.DLL',0 
    user_name db 'USER32.DLL',0 
  
    _ExitProcess dw 0 
      db 'ExitProcess',0 
    _MessageBoxA dw 0 
      db 'MessageBoxA',0
  </code></pre>
  <p>but it displays a message box and also has external dependencies &quot;kernel32.dll&quot; and &quot;user32.dll&quot;</p>
  <p>also tried this example <a href="https://board.flatassembler.net/topic.php?p=150882" rel="nofollow noreferrer">from the FASM forum</a></p>
  <pre><code>format pe console
  
  
  include 'win32ax.inc'
  
  entry main
  
  section '.data!!!' data readable writeable
  
  strHello db 'Hello World !',13,10,0
  strPause db 'pause',0
  
  section '.txt' code executable readable
  
  main:
         ; you can use crt functions or windows API.
         cinvoke printf,strHello
         cinvoke system,strPause; or import getc()
         ; or
         ; invoke printf,srtHello
         ; add esp, 4
  
         ; or use WriteFile and GetStdHandle APIs
         push 0
         call [ExitProcess]
        
  section '.blah' import data readable
  
  library kernel32,'kernel32.dll',\
      msvcrt,'msvcrt.dll'    ;; C-Run time from MS. This is always on every windows machine
  
  import kernel32,\
            ExitProcess,'ExitProcess'
  import msvcrt,\
            printf,'printf',\
            system,'system'
  </code></pre>
  <p>but it depends on <code>win32ax.inc</code> and other imports</p>
  <p>also</p>
  <pre><code>format PE console
  include 'win32ax.inc'
  .code
  start:
          invoke  WriteConsole,&lt;invoke GetStdHandle,STD_OUTPUT_HANDLE&gt;,&quot;Hello World !&quot;,13,0
          invoke  Sleep,-1
  .end start
  </code></pre>
  <p>but requires &quot;win32ax.inc&quot; import</p>
  <p>closest I could find without the win32ax <a href="https://board.flatassembler.net/topic.php?t=21489" rel="nofollow noreferrer">from the FASM forum</a>:</p>
  <pre><code>format pe64 console
  entry start
  
  STD_OUTPUT_HANDLE       = -11
  
  section '.text' code readable executable
  
  start:
          sub     rsp,8*7         ; reserve stack for API use and make stack dqword aligned
          mov     rcx,STD_OUTPUT_HANDLE
          call    [GetStdHandle]
          mov     rcx,rax
          lea     rdx,[message]
          mov     r8d,message_length
          lea     r9,[rsp+4*8]
          mov     qword[rsp+4*8],0
          call    [WriteFile]
          mov     ecx,eax
          call    [ExitProcess]
  
  section '.data' data readable writeable
  
  message         db 'Hello World!',0
  message_length  = $ - message
  
  section '.idata' import data readable writeable
  
          dd      0,0,0,RVA kernel_name,RVA kernel_table
          dd      0,0,0,0,0
  
  kernel_table:
          ExitProcess     dq RVA _ExitProcess
          GetStdHandle    dq RVA _GetStdHandle
          WriteFile       dq RVA _WriteFile
                          dq 0
  
  kernel_name     db 'KERNEL32.DLL',0
  user_name       db 'USER32.DLL',0
  
  _ExitProcess    db 0,0,'ExitProcess',0
  _GetStdHandle   db 0,0,'GetStdHandle',0
  _WriteFile      db 0,0,'WriteFile',0    
  </code></pre>
  <p>but still requires the kernel32.dll and user32.dll</p>
  <p>Any way to do this without any external DLLs at all? I know just the program fasm itself does it, and prints to the console, doesn't it?</p>