Author Topic: Trouble with import  (Read 11716 times)

Offline Gerhard

  • Jr. Member
  • *
  • Posts: 51
Trouble with import
« on: February 09, 2013, 07:50:34 PM »
Hi,

I'm a human and not a Spam Bot.

I'm trying to assemble the following small file with: nasm -f win32 mesbox.asm.

Code: [Select]
        [BITS 32]                        ; 32 bit segment

        %include "win32n.inc"

        extern ExitProcess
        IMPORT ExitProcess kernel32.dll
        extern MessageBoxA
        IMPORT MessageBoxA user32.dll

        section .data                    ; data

title   db '32 bit Windows and assembly language', 0
msg     db 'Hello World!', 0
Error   dd 0

        section .text                    ; code
       
..start

        push dword MB_ICONHAND + MB_OKCANCEL
        push dword title
        push dword msg
        push dword 0
        call MessageBoxA
        push dword Error
        call ExitProcess

That generates the following error messages:

Code: [Select]
mesbox.asm:6: error: parser: instruction expected
mesbox.asm:8: error: symbol `IMPORT' redefined
mesbox.asm:8: error: parser: instruction expected

I've used the following nasm version:

Code: [Select]
NASM version 2.10.07 compiled on Jan  2 2013

under Windows XP, SP3 inside VirtualPC. What's wrong? Any help is appreciated. Thank you.

Gerhard

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Trouble with import
« Reply #1 on: February 09, 2013, 08:38:48 PM »
IMPORT (and for that matter "..start") is a feature of the OMF object format ("-f obj") and does not work with the MS-COFF object format ("-f win32"). You should assemble it with: nasm -f obj mesbox.asm

About Bryant Keller
bkeller@about.me

Offline Gerhard

  • Jr. Member
  • *
  • Posts: 51
Re: Trouble with import
« Reply #2 on: February 09, 2013, 09:01:43 PM »
Synfire,

thank you for the fast answer. It works. Is there a better way to assemble that kind of Win32 files?

Gerhard
 

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Trouble with import
« Reply #3 on: February 09, 2013, 09:21:26 PM »
Quote
I'm a human and not a Spam Bot.

Hi Gerhard - I never would have guessed that from the question! :)

Well... "import" is known only to "-f obj" (it is provided as a macro by the NASMX package). The special "..start" label is also known only to "-f obj". IF you're going to use "-f obj", you'll also need to specify "use32" on both (all) of the section declarations ("-f obj" defaults to 16-bit code). You will also need to use "call [MessageBox]" and "call [ExitProcess]". I don't know why this is, but it goes along with "import". Alink should link this - I'm not sure about other linkers.

If you're going to stick with "-f win32" (which I think I would recommend), comment out (or delete) the "import" lines. Change "..start" to "_start" and add "global _start". If you're using GoLink, you can add "kernel32.dll" and "user32.dll" to the command line. Other linkers may require a "library"(?)...

I haven't run Windows for a long time (win98), so this is all "from memory" or "so I've been told". Poke around and you should find examples - and example linker command lines...

Best,
Frank

Edit: I see Bryant has already told you that. Well... I'll post anyway...


Offline Gerhard

  • Jr. Member
  • *
  • Posts: 51
Re: Trouble with import
« Reply #4 on: February 09, 2013, 11:39:56 PM »
Frank,

thank you for the answer. I'll check the nasmx package for examples.

Hi Gerhard - I never would have guessed that from the question! :)

I wrote this for the moderators, because it's a bit strange always put together the image puzzle before one can answer or open a new topic.

Gerhard
« Last Edit: February 10, 2013, 12:29:27 AM by Gerhard »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Trouble with import
« Reply #5 on: February 10, 2013, 12:43:40 AM »
Fortunately, I got signed up before I had to "guess the puzzle". I hate those things - often give up in frustration! I'm better at "guess why my code doesn't work". :)

Hope you won't have to do that every time!

Best,
Frank


Offline Gerhard

  • Jr. Member
  • *
  • Posts: 51
Re: Trouble with import
« Reply #6 on: February 10, 2013, 12:49:40 AM »
Hi Frank,

Hope you won't have to do that every time!

Best,
Frank

I hope so, too.

Gerhard

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Trouble with import
« Reply #7 on: February 10, 2013, 02:45:28 AM »
If you are programming on Windows, I definitely suggest using GoLink. It's a really good linker!

If you're using GoLink, you can add "kernel32.dll" and "user32.dll" to the command line. Other linkers may require a "library"(?)...

In regards to what Frank said here, you might also look the following post:

Re: Using "#DYNAMICLINKFILE" in golink

The method described there will allow you to tell GoLink what DLL's you are linking against without having to pass them on the command line.

About Bryant Keller
bkeller@about.me

Offline Gerhard

  • Jr. Member
  • *
  • Posts: 51
Re: Trouble with import
« Reply #8 on: February 10, 2013, 12:00:57 PM »
Hi Bryant,

thank you for the informative link; I'll check it out.

If you are programming on Windows, I definitely suggest using GoLink. It's a really good linker!

I'm writing code for Windows and Linux. You're right: GoLink is a good linker and it comes together with the latest nasmx package. Another alternative is PoLink from Pelle's C; that's a good linker, too.

Gerhard