Re: MASM32 help



Protoman wrote:

.....
Try one of the examples with the masm32 package...
.....
OK, the example minimum.asm assembles and links...why won't mine?

Minor syntax error "cascading" into many errors, would be my guess. Chuck says move the ".386" first. That makes sense. As I understand it (as is probably obvious, I am neither a Masm user nor a Windows programmer - and it makes a difference!), Masm uses ".386" and friends for two purposes - to enable "modern" instructions, and to ask for 32-bit code. You want both. Moving the ".386" may "enable" enough things to assemble without errors, with just that one change. (I'm not in a position to try it) I don't think the linker will have a problem with it - you might have to specify "/entry:_start" or something. But it won't run. What you've got there is Linux code, modified from Nasm syntax - not quite enough, apparently - to Masm syntax. I recognize the comment "our dear string"... from Konstantin Boldyshev, if I recall - and may not be original with him... :)

But lets look at it... Even though I don't know Masm or Windows, I can tell you *some* of what's wrong with it, and what you want to do instead...

Chuck says ".386 first".

...386

...model flat

As I recall hearing, ".model flat stdcall" is the "canonical" directive for masm32. Unless I'm mistaken, it affects the code produced by "proc" and "invoke" and friends. You're probably okay with just ".model flat"... until you try to use these.

...data

This is a "simplified segment directive", I think. Will probably work, once you've got Masm convinced you want 32-bit code.

msg db 'Hello, world!',0xa ;our dear string

Just 0xa is the line-ending convention for Linux. For Windows, you probably want "0xd, 0xa" (or "13, 10" or "CR, LF", with those constants defined somewhere). *Some* Windows APIs want a zero-terminated string. I think the closest equivalent to this code (WriteFile) wants the length...

len equ $-msg

It *may* be that you'll want to modify this to "len equ $ - offset msg". Nasm doesn't use "offset" - the unadorned name means "offset" - with Masm, the unadorned name means "contents of memory"... sometimes...

The "$" means "here" - current location pointer - to both Nasm and Masm (in this context). Gas uses "." for it...

...code
...386

I think this is where you want the ".386" if you want to enable 386 instructions and registers, but *don't* want 32-bit code. You'd do this to write dos code... if you wanted "modern" instructions in 16-bit code (which is perfectly legitimate)... then link with a 16-bit linker.

_start:

"_start" is the default entrypoint for Linux ld (the linker). MS link knows a default entrypoint name, too... but I've forgotten already... Link may tell you in it's error message what it can't find. (error messages can be useful - read 'em carefully to see how they're spelling things, etc.) Or "/entry:_start" may fix it.

mov edx,len ;message length

Note that since "len" is a defined constant, this syntax is okay for both Nasm and Masm.

mov ecx,msg ;message to write

Here, for sure, you want "offset msg" for Masm. This will load ecx with the first four characters of "Hell" :) Linux will/would try to use this as an address, and will most likely segfault. The Windows code you want pushes the parameters, instead of putting them in registers, but you still want the address - strictly speaking, the offset part of the address - there's a segment part too, but we can ignore it.

mov ebx,1 ;file descriptor (stdout)

In the Windows equivalent, you want ("push"ed) the file descriptor - or "handle", as Windows prefers - for "stdout" (the screen or "console", unless it's been redirected). But Windows uses -11, not 1 for "stdout", and does something weird with it... I've done "push -11" and had it work, but what I think you're supposed to do is "push -11", call GetStdHandle, and use what that returns (in eax) as your handle to "stdout". ("push eax", when the time comes)

mov eax,4 ;system call number (sys_write)

No real equivalent to this in the Windows code. Linux uses "int 80h" for everything, and puts the function number in eax (like dos, only moreso). In Windows, you'll call "WriteFile" by name.

int 0x80 ;call kernel

If I were writing Windows, doing "int 0x80" would pop up a messagebox saying "This is *Windows*, Fool!"... "Okay?" But I think it'll just crash...

mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel

You'll want to call ExitProcess here instead.

end _start

This little oxymoron tells Masm that the "_start:" label above is the entrypoint. It probably makes it "global"(?) so the linker can "see" it. In Nasm, we'd need to say "global _start"... I think "WinMain" is a mor common name for the entrypoint in Windows code - you might want to change it for "human clarity", but I don't think "WinMain" is known to link, either. You'll probably want to specify the entrypoint, and perhaps "/subsystem:console" on the command line to "link"... or prhaps Masm calls "link" for you(?)...

If you start out with "minimum.asm" as a "skeleton"... Lessee, WriteFile returns the number of characters actually written in a dword variable whose address (offset) we pass as a parameter, so you'll want "num_chars dd 0" in your ".data" section... then...

push 0 ; unicode flag???
push offset num_chars
push len
push offset msg
push -11
call Writefile

You might need "call [WriteFile]"... or even "_WriteFile@20"... but I think those "include" files in "minimum.asm" will take care of that(?). You may even be able to delete some of 'em. Should do pretty much what the Linux code would have done, it it had worked. :)

Best,
Frank

.



Relevant Pages

  • Re: beginner asm, where next?
    ... Kip's book uses concentrates on MASM, and since I'm on a Windows ... Desktop more than Linux, I figured MASM is the way to go ... I failed to mention that I'm learning VC++ (Vis Studio 2005) at the ...
    (alt.lang.asm)
  • Re: beginner asm, where next?
    ... Kip's book uses concentrates on MASM, and since I'm on a Windows ... Desktop more than Linux, I figured MASM is the way to go ... I failed to mention that I'm learning VC++ (Vis Studio 2005) at the ...
    (alt.lang.asm)
  • Re: HAY Herbert, wanna write a legal PE executable ?
    ... Windows 32 bit PE executable files are a specification from ... Who said it need the sheer brutal power of MASM to write a 1024 byte ... possible to improve it using a better assembler. ...
    (alt.lang.asm)
  • Re: Which x86 assembler for Windows/Linux/Solaris?
    ... > like to continue supporting our Windows program. ... "4.11 Using MASM under Linux ... The object files in ELF format can ...
    (comp.lang.asm.x86)
  • Re: ///HLA StdLib2 criticism
    ... this obsolete method would be adding black-box programming ... the truth is you use Windows, all your users use Windows, and ReactOS ... hit for NASM than for MASM. ... 800-pound gorilla amongst all the 8-pound assemblers out there. ...
    (alt.lang.asm)