title: Trouble linking assembly program: File format not recognized
body: <p>I have the following simple assembly program (downloaded from <a href="https://www.csee.umbc.edu/portal/help/nasm/sample_64.shtml" rel="nofollow noreferrer">this website</a>).:</p>
  
  <pre><code>        extern  printf          ; the C function, to be called
  
          section .data           ; Data section, initialized variables
  msg:    db "Hello world", 0     ; C string needs 0
  fmt:    db "%s", 10, 0          ; The printf format, "\n",'0'
  
          section .text           ; Code section.
  
          global main             ; the standard gcc entry point
  main:                           ; the program label for the entry point
          push    rbp             ; set up stack frame, must be alligned
  
          mov rdi,fmt
          mov rsi,msg
          mov rax,0               ; or can be  xor  rax,rax
          call    printf          ; Call C function
  
          pop rbp                 ; restore stack
  
          mov rax,0               ; normal, no error, return value
          ret                     ; return
  </code></pre>
  
  <p>When I try to build from the command line using NASM I get no errors, and it creates the object file correctly. Then I link it using Mingw-w64 (ld) (because I am on a x64 machine) with command line option <code>-m i368pe</code>, but this fails with an error message: File format not recognized. After searching a bit, I found that this has to do with the fact that there I am trying to link a 32 bit object file with a 64 bit linker or the other way around. However, this does not make sense to me as I built the object file with the <code>-f win64</code> flag. Also, I believe <code>-m i386pe</code> is a 64 bit flag for linking, so what am I doing wrong here?</p>
  
  <p>My commands for building/linking are:</p>
  
  <ul>
  <li><p>Build: <code>nasm -f win64 -o test.obj test.asm</code></p></li>
  <li><p>Link: <code>ld test.obj -m i386pe -o test.exe</code></p></li>
  </ul>