Author Topic: New wit NASM, mi simple test.exe program is not working  (Read 27326 times)

Offline wyvern666

  • Jr. Member
  • *
  • Posts: 32
New wit NASM, mi simple test.exe program is not working
« on: June 18, 2011, 05:14:20 PM »
Hi all the forum!

im new using NASM (i know masm better), i spend hours to make a simple HelloWorld calling MessageBox from the win32api. Im using radasm ide, by default the ide is configured to use NASMW.exe (now nasm.exe) with -fobj, and the ALINK linker. With that format, using "extern" & "import" and such things, and alink linker i couldnt make the program work. So i give up with that config and tools.

Reading so much info in the net (i think the vast mayority seems to be "old") i concluded to use the -fwin32 format, but i am having problems with the linker, i am using now the microsodt LINK.exe. The program links without errors, but i dont see nothing in screen.

Mi code is:

         
Code: [Select]
bits 32

extern _MessageBoxA@16
;extern _ExitProcess@4

    section .data

title db 'Somme message',0
message db 'Hello World!',0

section .code
global _main         ;i really need this for the LINK.EXE ?

_main:
push 0Eh
push title
push message
push 0
call _MessageBoxA@16            ;dont know if really need the "dword" here
           ;call dword _MessageBox@16    ;

;push 0
;call dword _ExitProcess@4

The commands the RADASM ide is using are:

-> NASM -fwin32 -i C:\NASM\nasm-2.09.08\Inc\ "Test.asm"

-> LINK.EXE /ENTRY:main /SUBSYSTEM:windows /MACHINE:X86 C:\NASM\nasm-2.09.08\Lib\user32.lib "Test.obj"

With that i have no errors, and when the Text.exe is executed sometimes it keeps loaded in memory, sometimes not (i know i am not using ExitProcess), the thing is that the message box never appears. Can you help me please?

I have another questions about NASM:
%include is like the include in masm, i am correct?
and what is the similar to includelib from masm?


Sorry bad english.

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: New wit NASM, mi simple test.exe program is not working
« Reply #1 on: June 18, 2011, 08:16:44 PM »
I can't find a uType of 0x0E within the MSDN documentation for MessageBox().

Start with MB_OK (0x00) or some other documented uType, to be "vanilla" as possible when testing, and work your way out from there.

Your use of ExitProcess() is correct, and I would recommend uncommenting (re-implementing) it to ensure crash-less testing.

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: New wit NASM, mi simple test.exe program is not working
« Reply #2 on: June 18, 2011, 08:18:29 PM »
Hi all the forum!

im new using NASM (i know masm better), i spend hours to make a simple HelloWorld calling MessageBox from the win32api.

Well, that's just uncalled for in regards to coding a hello world app. Maybe we can help! :)

Im using radasm ide, by default the ide is configured to use NASMW.exe (now nasm.exe) with -fobj, and the ALINK linker. With that format, using "extern" & "import" and such things, and alink linker i couldnt make the program work. So i give up with that config and tools.

Yeah, in the early days of NASMX (at that time NASM32) all the code used the OBJ format with ALINK, I believe KetilO followed the same concept when a few of us at ASMCommunity started asking for NASM support. Later, NASM32 dropped the OBJ format cause we wanted to use golink which removed the need for a large number of library files.

Reading so much info in the net (i think the vast mayority seems to be "old") i concluded to use the -fwin32 format, but i am having problems with the linker, i am using now the microsodt LINK.exe. The program links without errors, but i dont see nothing in screen.

I don't use LINK.EXE under windows so I'm not sure how much help I'll be. Have you thought about using GoLINK from the GoASM Website?

Mi code is:

          
Code: [Select]
bits 32

extern _MessageBoxA@16
;extern _ExitProcess@4

    section .data

title db 'Somme message',0
message db 'Hello World!',0

section .code
global _main         ;i really need this for the LINK.EXE ?

_main:
push 0Eh
push title
push message
push 0
call _MessageBoxA@16            ;dont know if really need the "dword" here
           ;call dword _MessageBox@16    ;

;push 0
;call dword _ExitProcess@4

I only really see two things (immediately) wrong with your code. I would personally use 'section .text' instead of 'section .code' as you'll find that's much more common (does section .code even work?). I'm going to show you some code, I want you to wait until you finish reading this post though before you go using it, there are some things I want to point out ;)

Code: [Select]
bits 32
extern _MessageBoxA@16
extern _ExitProcesses@4

section .data
title db 'Somme message',0
message db 'Hello World!',0

section .text
global main
main:
push dword 0 ; Type = MB_OK
push dword title ; Caption
push dword message ; Message
push dword 0 ; Handle = NULL
call _MessageBoxA@16

push dword 0 ; Exit Code = EXIT_SUCCESS
call _ExitProcess@4

The commands the RADASM ide is using are:

-> NASM -fwin32 -i C:\NASM\nasm-2.09.08\Inc\ "Test.asm"

-> LINK.EXE /ENTRY:main /SUBSYSTEM:windows /MACHINE:X86 C:\NASM\nasm-2.09.08\Lib\user32.lib "Test.obj"

With that i have no errors, and when the Text.exe is executed sometimes it keeps loaded in memory, sometimes not (i know i am not using ExitProcess), the thing is that the message box never appears. Can you help me please?

Okay, build and link my version of Test.asm in place of yours. You'll notice I made a few changes, first I made use of section .text which (like I said earlier I'm not up on windows systems but) I know it works so lets just go that way. Second change I made was to your exported "main' routine. If you check out your link command, you are specifying 'main' as your entrypoint but exporting it as '_main'.

I have another questions about NASM:
%include is like the include in masm, i am correct?
and what is the similar to includelib from masm?


Sorry bad english.

Yes, %include does essentially the same thing as MASM. it just "includes" a copy of the source file which is specified as an argument directly into your code listing. As for includelib, that's more or less MASM specific, but this is also why I suggested using GoLINK! If you pass GoLink the system DLL's which contain the library code you want, GoLINK will automagically fix up your imports against those DLL's, so there is no need for LIB files!

I hope I was able to help, I hope even more my code works as I'm not on a win-box atm. :))

Best Regards,
Bryant Keller
« Last Edit: June 18, 2011, 08:21:02 PM by Bryant Keller »

About Bryant Keller
bkeller@about.me

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: New wit NASM, mi simple test.exe program is not working
« Reply #3 on: June 18, 2011, 08:42:10 PM »
Easy question first: Nasm has no equivalent to Masm's "includelib". I think maybe we could "fake" one, but that's another subject. Nasm's "%include" is essentially a "cut and paste" of the included file, which I think is the same as what Masm does(?).

Now... there are subtle differences between "-f obj" and "-f win32", one of which is that "-f win32" knows certain names for your sections/segments. I would suggest changing "section .code" to "section .text" (a "known" name).

The other thing I see that looks "funny" is the "push 0Eh". This is the "messagebox style". The default messagebox is zero. I was messing with this (long ago - win98), and found a messagebox that produced two "help" buttons. I think it was 0Eh that did that, and I think it doesn't work in more modern Windows versions(?). Try changing that to "push 0". I found the "two help buttons" amusing, but it probably isn't what you want. :)

You will need "ExitProcess" (I think it's in "kernel32.dll", so you'll probably need "kernel32.lib" in your "link" options). Lacking this, your program will probably crash, but you would probably see the messagebox first, so try it without if you want. Ending with "ret" might also work, but I don't think it's "right"...

See if "section .text" and "push 0" help any, to start with...

Best,
Frank


Offline wyvern666

  • Jr. Member
  • *
  • Posts: 32
Re: New wit NASM, mi simple test.exe program is not working
« Reply #4 on: June 18, 2011, 09:03:19 PM »
First, thank you for your comments.

With your fixed code i get:

"LINK : error LNK2001: unresolved external symbol _main
Test.exe : fatal error LNK1120: 1 unresolved externals"

Thats why i was using "_main", is like LINK.EXE need some "_", xD or i dont know. I am going to use golink.exe, hehe.

I am frustrated, i cant do nothing to work, for example in: http://stackoverflow.com/questions/1023593/how-to-write-hello-world-in-assembler-under-windows

There is a console helloworld example in NASM, proposing to compile with:
---
nasm -fwin32 hello.asm
link /subsystem:console /nodefaultlib /entry:main hello.obj
---
That example in stackoverflow is very good rated, but in mi case, LINK.exe fails and CRASH .... i mean WTF is wrong with me, or mi pc, or radasm, or nasm, or the linker?, i tryed with more than 15 examples and in may ways.

Note: mi pc is win 7, 64bit, but i think that is not the problem.

Mi last try in this day will be with the GoLink as you suggested.

Another thing, i am interested in learning how to write an .exe from scratch without the linker (usin nasm -f bin ...). The are good examples about that?, and yes i have documentation about the PE format.

Offline wyvern666

  • Jr. Member
  • *
  • Posts: 32
Re: New wit NASM, mi simple test.exe program is not working
« Reply #5 on: June 18, 2011, 09:06:10 PM »
yeah frank, i tried with "text" and the "0" in the push.  :(

Offline wyvern666

  • Jr. Member
  • *
  • Posts: 32
Re: New wit NASM, mi simple test.exe program is not working
« Reply #6 on: June 18, 2011, 09:43:46 PM »
Ok!, now i am going to somewhere, after figthing with some other f**** linkers, GOLINK is working hehe, and is really cool that it doesnt need the .lib files xD.

If this can help some n00b like me: this is HELLO WORLD in a MessageBox with NASM and GOLINK:

Code: [Select]
bits 32
extern _MessageBoxA@16
extern _ExitProcess@4

section .data
title db 'Somme message',0
message db 'Hello World!',0

section .text
global _start
_start:
push dword 0 ; Type = MB_OK
push dword title ; Caption
push dword message            ; Message
push dword 0 ; Handle = NULL
call _MessageBoxA@16

push dword 0 ; Exit Code = EXIT_SUCCESS
call _ExitProcess@4
---
nasm -fwin32 test.asm
golink /entry _start /mix test.obj user32.dll, kernel32.dll
---

Now after all this headache, what is exactly the job of the linker?, can i write mi executable program witouth using linker?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: New wit NASM, mi simple test.exe program is not working
« Reply #7 on: June 18, 2011, 11:26:32 PM »
Okay! Glad you got it working. I think the problem(s) you (and a lot of other people) are having is because of different linkers. I'll attach an example of a program that assembles direct to an executable using "-f bin". There are macros available that will simplify this somewhat - I'll see if I can find 'em. I really think that "standardizing" on some linker (probably GoLink) - or perhaps different examples for different linkers - is a better bet than doing it this way, but it's an option...

Best,
Frank


Offline wyvern666

  • Jr. Member
  • *
  • Posts: 32
Re: New wit NASM, mi simple test.exe program is not working
« Reply #8 on: June 18, 2011, 11:45:41 PM »
Thanks frank!, thats a good example, i know there is no much out there like that, but writing that way is good to learn more about the PE. But i compiled it, and get: "not a win32 valid application". ???

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: New wit NASM, mi simple test.exe program is not working
« Reply #9 on: June 19, 2011, 05:50:23 AM »
Thanks frank!, thats a good example, i know there is no much out there like that, but writing that way is good to learn more about the PE. But i compiled it, and get: "not a win32 valid application". ???

OPTIONAL_HEADER.SizeOfImage is being miscalculated as per the PE/COFF specification, causing Windows to read the generated EXE as bad/corrupt/invalid.

There are some cleaner and seemingly more correct PE generation examples HERE.
« Last Edit: June 19, 2011, 05:52:00 AM by Keith Kanios »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: New wit NASM, mi simple test.exe program is not working
« Reply #10 on: June 19, 2011, 04:45:37 PM »
The failure of the example I posted is probably a good reason to use a linker! Presumably it worked on some version(s) of Windows, but later versions have tightened up the rquirements - probably in the interest of greater security(?). The same fate could befall the code Keith linked to, at some future date (pretty clever, though!).

I'll attach a zip file with Numit_or's version of "pemacs.inc" (and an altered version with a special "shutdown stub"). It used to work at some point - no idea whether it still does or not.

Best,
Frank