I'm trying to compile an example nasm program I downloaded for windows. But I can't get it to compile.

C:\nasm-2.06rc1\nasmw -f win32 example.asm
C:\lcc\bin\lcclnk -s -subsystem windows example.obj

When I compile it with that, the first part of it goes fine, it's when I use lcclnk to link it I get these error messages:

C:\Documents and Settings\Administrator\Desktop\NASM\DEMO>C:\lcc\bin\lcclnk -s -
subsystem windows example.obj
example.obj .text: undefined reference to 'LoadIconA'
example.obj .text: undefined reference to 'LoadCursorA'
example.obj .text: undefined reference to 'RegisterClassA'
example.obj .text: undefined reference to 'GetLastError'
example.obj .text: undefined reference to 'CreateWindowExA'
example.obj .text: undefined reference to 'ShowWindow'
example.obj .text: undefined reference to 'UpdateWindow'
example.obj .text: undefined reference to 'GetMessageA'
example.obj .text: undefined reference to 'TranslateMessage'
example.obj .text: undefined reference to 'DispatchMessageA'
example.obj .text: undefined reference to 'ExitProcess'
example.obj .text: undefined reference to 'DefWindowProcA'
example.obj .text: undefined reference to 'PostQuitMessage'
example.obj .text: undefined reference to 'BeginPaint'
example.obj .text: undefined reference to 'DrawTextA'
example.obj .text: undefined reference to 'EndPaint'

Here's the first part of my code:

%include "C:\Documents and Settings\Administrator\Desktop\NASM\DEMO\win32n.inc"

extern RegisterClassA
extern CreateWindowExA
extern CreateWindow
extern ShowWindow
extern UpdateWindow
extern GetMessageA
extern TranslateMessage
extern DispatchMessageA
extern DefWindowProcA
extern ExitProcess
extern MessageBeep
extern GetLastError
extern LoadIconA
extern LoadCursorA
extern PostQuitMessage

extern BeginPaint
extern EndPaint
extern DrawTextA

global _WinMain@16

SEGMENT .text USE32 class=code
_WinMain@16:

%define ebp_hInstance       ebp+8	; handle of current instance
%define ebp_hPrevInstance   ebp+0ch	; handle of previous instance
%define ebp_lpszCmdLine     ebp+10h	; pointer to command line
%define ebp_nCmdShow        ebp+14h	; show state of window

I'm determined to get this working, as I really want to start coding in NASM for windows.

Thanks in advance.

Does the link know where and to include C headers etc....

Just link it with gcc using gcc example.obj -o example.exe

Does the link know where and to include C headers etc....

Just link it with gcc using gcc example.obj -o example.exe

"Does the link know where and to include C headers etc...." Good point, how does one go about doing that? I tried linking it with gcc, but I still get the same error message.

I'd recommend a read of Narue's tutorial, its a few posts down and is a brillaint intro to NASM on windows :)

I get it now. It doesn't know where to get the winAPI functions from.

I added this:

import MessageBoxA user32.dll

Still though, it's still pissing me about. There's so many annoying little things that don't work, yet should.

[extern MessageBoxA]
import MessageBoxA user32.dll

[global _main]
[segment .text]


_main:
push dword szText 	; Push param onto stack
call box 			; Call our function
add esp, 4 			; un-bork the stack?
ret

box:
enter 0,0
mov eax, [ebp+8] 	; Pull first param off stack
push dword 0 		; MB_OK style
push dword eax 		; First Param
push dword eax 		; First Param
push dword 0 		; HWND parent (NULL)
call MessageBoxA

xor eax, eax
leave
ret

[segment .data]
szText: db 'Hello',0

if I link it with alink, I get this (which doesn't work):

C:\nasm-2.06rc1>nasm -o messagebox.obj -f obj messagebox.asm

C:\nasm-2.06rc1>c:\alink\alink.exe -oPE -subsys con messagebox.obj
ALINK v1.6 (C) Copyright 1998-9 Anthony A.J. Williams.
All Rights Reserved

Loading file messagebox.obj
matched Externs
matched ComDefs
Generating PE file messagebox.exe
Warning, no entry point specified
Error writing to file messagebox.exe

If I link it with gcc I get this:

C:\nasm-2.06rc1>nasm -o messagebox.obj -f obj messagebox.asm

C:\nasm-2.06rc1>c:\MinGW\bin\gcc.exe "C:\nasm-2.06rc1\messagebox.obj" -o size2.e
xe
C:\nasm-2.06rc1\messagebox.obj: file not recognized: File format not recognized
collect2: ld returned 1 exit status

What the hell's going? Is there any documentation online about compiling things in nasm?

[segment .text]
  global _main

_main:

Should work with GCC, as for ALINK I think that require .start or ..start as it's entry point.

Chris

This article has been dead for over six months. Start a new discussion instead.