0

I am trying to compile this program under NASM (x86-64, Windows).

; prime.asm
global _main
extern _scanf
extern _printf

section .data:
    n: dd 0
    i: dd 2
    format_string_scanf: db '%d', 0x0A
    format_string_no_printf: db 'The number %d is not prime', 0x0A
    format_string_yes_printf: db 'The number %d is prime', 0x0A

section .text
_main:
    push n
    push format_string_scanf
    call _scanf
    cmp dword [n], 1
    je NOT_PRIME
    cmp dword [n], 0
    je NOT_PRIME
FOR_LOOP:
    mov eax, [i]
    mul dword [i]
    cmp eax, [n]
    jg IS_PRIME
    mov edx, 0
    mov eax, [n]
    div dword [i]
    cmp edx, 0
    je NOT_PRIME
    jmp FOR_LOOP
IS_PRIME:
    push qword [n]
    push format_string_yes_printf
    call _printf
    jmp PROGRAM_END
NOT_PRIME:
    push qword [n]
    push format_string_no_printf
    call _printf
PROGRAM_END:
    ret

These are the options I use:

nasm.exe -f win64 -o prime.obj prime.asm
link.exe prime.obj /subsystem:console /entry:_main

NASM does fine, however link.exe throws 2 errors:

prime.obj : error LNK2001: unresolved external symbol _scanf
prime.obj : error LNK2001: unresolved external symbol _printf
prime.obj : fatal error LNK1120: 2 unresolved externals

I guess it is failing to link to the C Runtime one way or another. Can somebody help me out to solve these 2 errors?

DarkAtom
  • 2,589
  • 1
  • 11
  • 27
  • Apparently microsoft moved those into `legacy_stdio_definitions.lib` so you need to add that to your linker command. – Jester Oct 18 '19 at 19:16
  • Tried like this: `link.exe prime.obj /subsystem:console /entry:_main legacy_stdio_definitions.lib`. Tells me it cannot find the library. – DarkAtom Oct 18 '19 at 19:19
  • You will just have to search your file system for it. Quoting [this answer](https://stackoverflow.com/a/43239812/547981): _"found deep in a sub-dir of my VS2017 install directory"_ – Jester Oct 18 '19 at 19:26
  • Ok found it, but...yet another error. LNK1112: Module machine type 'x86' conflicts with target machine type 'x64'. Is the library 32-bit only? – DarkAtom Oct 18 '19 at 19:31
  • Your code is 32 bit anyway. So assemble as such. – Jester Oct 18 '19 at 19:44
  • Did it. I am getting about 20 of these errors: `legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol ___acrt_iob_func referenced in function __vwprintf_l` – DarkAtom Oct 18 '19 at 19:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201120/discussion-between-jester-and-darkatom). – Jester Oct 18 '19 at 19:53

0 Answers0