Author Topic: Examples,Hello worlds and Windows application  (Read 24746 times)

Offline Borneq

  • Jr. Member
  • *
  • Posts: 26
Examples,Hello worlds and Windows application
« on: August 11, 2010, 12:39:13 PM »
Where I can find Nasm examples. How to write simple program to printf to console? I must create console WinApi function and then call function to printf? How to write simple Windows application which create window and serve Windows Paint message?

Offline Borneq

  • Jr. Member
  • *
  • Posts: 26
Re: Examples,Hello worlds and Windows application
« Reply #1 on: August 11, 2010, 01:46:17 PM »
I found http://www.csee.umbc.edu/portal/help/nasm/sample.shtml
but there is sample under Linux
Alink samples works with Tasm not Nasm, and included includelib "win32.lib",
I prefer do not use external library but only WinAPI function from DLL, Loaded with LoadLibrary or statically linked with Library.

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Examples,Hello worlds and Windows application
« Reply #2 on: August 11, 2010, 02:51:50 PM »
Take a look at the latest stable version of NASMX. You'll find such an example in Win32 DEMO11 or DEMO12.

You can put NASM in preprocess-only (-e) mode, preprocess one of the NASMX demos, and see what kind of "raw" assembly is needed to interface with Win32 DLL's.

Offline Borneq

  • Jr. Member
  • *
  • Posts: 26
Re: Examples,Hello worlds and Windows application
« Reply #3 on: August 11, 2010, 03:34:22 PM »
I do not know why -e produces only line %line 1+1 ..\..\..\inc\nasmx.inc and for demo1 is error.
If is -l %file%.lst then is listing and I see "IMPORT MessageBoxA, 16".
It creates exe - ok, but I want debug with Gdb and Gdb not recognizes DEMO1.exe as executable format. Nasmx example uses linker GoLink, I prefer Alink (are yet another free linkers?) but Alink say that unresolved external _MessageBoxA@16 and _ExitProcess@4.
Why changed name adding "@16", because is "IMPORT MessageBoxA, 16"? How link with Alink or another linker. How add debug information, how add information only to selected lines?
Thanks

Offline Borneq

  • Jr. Member
  • *
  • Posts: 26
Re: Examples,Hello worlds and Windows application
« Reply #4 on: August 11, 2010, 03:42:47 PM »
"IMPORT MessageBoxA, 16" speedup exe running compare to "IMPORT MessageBoxA" ? How find number to name Api function? This number is immutable in various Windows versions?
I change "IMPORT MessageBoxA" but Alink still can't find this function
« Last Edit: August 11, 2010, 03:46:42 PM by Borneq »

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Examples,Hello worlds and Windows application
« Reply #5 on: August 11, 2010, 03:59:03 PM »
IIRC, ALINK expects .lib files for resolving symbols during linking, where-as GoLink can resolve symbols from DLL files, so there's one good reason to use GoLink.

Unless you have a more important reason than personal preference, then I advise you to move away from ALINK, as it hasn't been updated in nearly 10 years... open-source or not.

As for the @16 in _MessageBoxA@16, etc... that is the fully qualified symbol as per the STDCALL convention, anything different is just an error or abstraction.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Examples,Hello worlds and Windows application
« Reply #6 on: August 12, 2010, 07:22:21 AM »
There are a couple of things that should be cleared up, lest Borneq become very confused!

James Harris has posted (on c.l.a.x.) a link to his example:

http://codewiki.wikispaces.com/winpopup.nasm

(there are some other examples there, too)

This uses Nasm's "-f obj" output format (and Alink - "personal preference" is as good a reason as any to use it). In Nasm's "- obj" output format (only), "import" is a Nasm directive - in NASMX, "IMPORT" is a complicated macro - they are not the same thing! Also, "..start" is specific to "-f obj". Also, in "-f obj" output format, using "import", you want "call [ExitProcess]", etc. I think - but I don't remember for sure - that if you link (Alink) against the "win32.lib" that comes with Alink, you use "call ExitProcess", etc. - without the square brackets (republican parentheses), which is more usual. I'm not sure why this is, but it's a big difference, and it won't work "the other way"!

Nasm's "-f obj" is, by default, a 16-bit output format. There are 32-bit extensions - you'll have to tell Nasm "use32" in your segment/section declarations. If you use "-f win32", it's 32-bit by default, and you won't have to bother with that. I don't know if Alink will work with "-f win32" or not (not well, as I recall). You probably want to learn/use the more "modern" methods, as Keith suggests. If you "wanna do it the way you wanna do it" (asmfolk are like that, often), the "old" stuff still works (mostly... I guess Windows has gotten fussier since win98 - the last time I used it).

There are a flock of "old" examples in the "files" section of the !YAHOO! group, "win32-nasm-users". I packed 'em all up into:

http://sourceforge.net/projects/nasm/files/example%20code/yahoowin/yahoowin.zip/download

It is a bit of a "code bomb" - 16.7MB of unsorted example files, etc. Includes, I think, two versions of Nasm "translations" of the code from Iczelion's tutorials (this ain't the tutorials:

http://win32assembly.online.fr/tutorials.html

this is just the example code.) Iczelion's tutorials are highly recommended, and you should probably read them... even if he expects you to be using Masm. :)

Most of the examples use NaGoA - an IDE... unfortunately no longer supported, AFAIK. There may be a fairly recent executable in the "code bomb". You can use the examples without the IDE, in any case. "NaGoA" stands for "Nasm", "Gorc", "Alink"... so it's a huge archive of examples using Nasm and Alink, if you want to slog through it looking for the "gems".

Perhaps some of these examples should be "modernized" to be compatible with NASMX, or so...

None of this is intended to teach you assembly language! If you're new to assembly language, as well as to Windows programming, you may want to look at something completely different first...

http://www.drpaulcarter.com/pcasm

This tutorial "hides" the interface to the OS so it can be used with Windows, Linux, BSD, (MACOS?)... It won't teach you "Windows programming", which seems to be your goal, but if you're confused by the "assembly language" parts of the Windows examples, see if it helps.

I don't know why gdb doesn't believe your executables are executables. (I avoid gdb when I can, even in Linux) I don't think there's a way to add debug information in "-f win32". The "-g" switch will enable Borland's TD-compatible debug info in "-f obj" mode, but it doesn't really work right at its best (dos), and I doubt it'll do you any good at all in Windows! Ollydbg seems to do okay without it.

If you're asking "how do I know it should be 16 for MessageBox", consult Microsoft. Type some API name into Google and follow the link to Microsoft. Learn to navigate that help system. You'll need it! :)

Best,
Frank