Author Topic: NASM externs and Microsoft LINK?  (Read 13498 times)

Offline vLK

  • Jr. Member
  • *
  • Posts: 2
NASM externs and Microsoft LINK?
« on: March 19, 2010, 07:35:35 AM »
Hello!
I am quite new to NASM, for some time I've used FASM (NASM syntax is a bit scary for beginners), but in general, I'm also quite new to ASM.

At the moment, I am mostly playing around with this assembler to learn its syntax fully. Much of it I like, and it's been pretty straight forward so far, although this morning I encountered a problem with WinAPI imports and I hope that you can help me..

So.. I'm declaring an extern using the obvious 'extern' directive:
Code: [Select]
extern SleepIn my code I then call this a couple of times, this is not very important, it assembles nicely with:
Code: [Select]
nasm -f win32 -Ox -o code.obj code.s
However when I try to link with this line, using Microsoft LINK, all hell is let loose:
Code: [Select]
link /ENTRY:main /SUBSYSTEM:console /MACHINE:X86 kernel32.lib code.obj
code.obj : error LNK2001: unresolved external symbol Sleep
code.exe : fatal error LNK1120: 1 unresolved externals
I have tried declaring the symbol _Sleep, __imp__Sleep and __imp__Sleep@4 but it generates the same error.

What am I doing wrong here? Should I maybe just use another linker? Anyone else had these problems?

And another question: Whats the essential difference between a %define and an equ? Is there any differences but the one that equ supports various expressions? Hope I don't appear too nooby :P Thanks!

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: NASM externs and Microsoft LINK?
« Reply #1 on: March 19, 2010, 08:21:58 AM »
I have tried declaring the symbol _Sleep, __imp__Sleep and __imp__Sleep@4 but it generates the same error.

You pretty much danced around it. IIRC, the STDCALL decoration for that function should be: _Sleep@4

And another question: Whats the essential difference between a %define and an equ? Is there any differences but the one that equ supports various expressions? Hope I don't appear too nooby :P Thanks!

As per NASM Doc Section 3.2.4, an equ is for assigning constants, thus it is only evaluated once and cannot be changed.

As per NASM Doc Section 4.1.1, a define is a single-line macro, thus it is apart of the preprocessor, can be changed and may be evaluated multiple times.

Offline vLK

  • Jr. Member
  • *
  • Posts: 2
Re: NASM externs and Microsoft LINK?
« Reply #2 on: March 19, 2010, 09:16:59 AM »
That worked! Thank you for your answers :)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: NASM externs and Microsoft LINK?
« Reply #3 on: March 19, 2010, 09:30:21 AM »
A subtle difference between "%define" and "equ" is that a symbol assigned with "equ" goes into the symbol table, and makes it into the executable (at least in Linux). This may have advantages. A symbol defined with "%define" is an "assemble-time only" symbol - no trace of it in the executable (AFAIK).

You could think of "equ" as a "numeric cut-and-paste" and "%define" as a "text cut-and-paste"... (I think...).

Out of curiousity, vLK, what parts of Nasm syntax do you find scary, compared to Fasm?

Best,
Frank


Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: NASM externs and Microsoft LINK?
« Reply #4 on: March 19, 2010, 05:21:31 PM »
Out of curiousity, vLK, what parts of Nasm syntax do you find scary, compared to Fasm?

I'm curious too, as FASM is basically a fusion of TASM and NASM concepts that were favored by the author.