MASM32 help - ASM x86 ASM 370

This is a discussion on MASM32 help - ASM x86 ASM 370 ; I'm trying to assemble this little program: ..model flat ..data msg db 'Hello, world!',0xa ;our dear string len equ $-msg ..code ..386 _start: mov edx,len ;message length mov ecx,msg ;message to write mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system ...

+ Reply to Thread
Results 1 to 7 of 7

MASM32 help

  1. Default MASM32 help

    I'm trying to assemble this little program:

    ..model flat
    ..data
    msg db 'Hello, world!',0xa ;our dear string
    len equ $-msg
    ..code
    ..386
    _start:

    mov edx,len ;message length
    mov ecx,msg ;message to write
    mov ebx,1 ;file descriptor (stdout)
    mov eax,4 ;system call number (sys_write)
    int 0x80 ;call kernel

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

    end _start

    It doesn't assemble. Here's the errors:

    Assembling: C:\hello.asm
    C:\hello.asm(1) : error A2085: instruction or register not accepted in
    current CPU mode
    C:\hello.asm(2) : error A2013: .MODEL must precede this directive
    C:\hello.asm(3) : error A2034: must be in segment block
    C:\hello.asm(4) : error A2034: must be in segment block
    C:\hello.asm(5) : error A2013: .MODEL must precede this directive
    C:\hello.asm(7) : error A2034: must be in segment block
    C:\hello.asm(9) : error A2034: must be in segment block
    C:\hello.asm(10) : error A2034: must be in segment block
    C:\hello.asm(11) : error A2034: must be in segment block
    C:\hello.asm(12) : error A2034: must be in segment block
    C:\hello.asm(13) : error A2034: must be in segment block
    C:\hello.asm(15) : error A2034: must be in segment block
    C:\hello.asm(16) : error A2034: must be in segment block
    C:\hello.asm(18) : error A2006: undefined symbol : _start
    C:\hello.asm(18) : error A2148: invalid symbol type in expression :
    _start

    I'm using MASM32 on WinXP. If you could help me, that'd be great.
    Thanks!!!!


  2. Default Re: MASM32 help

    Protoman wrote:
    > I'm trying to assemble this little program:
    >
    > .model flat
    > .data
    > msg db 'Hello, world!',0xa ;our dear string
    > len equ $-msg
    > .code
    > .386
    > _start:
    >
    > mov edx,len ;message length
    > mov ecx,msg ;message to write
    > mov ebx,1 ;file descriptor (stdout)
    > mov eax,4 ;system call number (sys_write)
    > int 0x80 ;call kernel
    >
    > mov eax,1 ;system call number (sys_exit)
    > int 0x80 ;call kernel
    >
    > end _start
    >
    > It doesn't assemble. Here's the errors:
    >
    > Assembling: C:\hello.asm
    > C:\hello.asm(1) : error A2085: instruction or register not accepted in
    > current CPU mode
    > C:\hello.asm(2) : error A2013: .MODEL must precede this directive
    > C:\hello.asm(3) : error A2034: must be in segment block
    > C:\hello.asm(4) : error A2034: must be in segment block
    > C:\hello.asm(5) : error A2013: .MODEL must precede this directive
    > C:\hello.asm(7) : error A2034: must be in segment block
    > C:\hello.asm(9) : error A2034: must be in segment block
    > C:\hello.asm(10) : error A2034: must be in segment block
    > C:\hello.asm(11) : error A2034: must be in segment block
    > C:\hello.asm(12) : error A2034: must be in segment block
    > C:\hello.asm(13) : error A2034: must be in segment block
    > C:\hello.asm(15) : error A2034: must be in segment block
    > C:\hello.asm(16) : error A2034: must be in segment block
    > C:\hello.asm(18) : error A2006: undefined symbol : _start
    > C:\hello.asm(18) : error A2148: invalid symbol type in expression :
    > _start
    >
    > I'm using MASM32 on WinXP. If you could help me, that'd be great.
    > Thanks!!!!


    (wiping the coffee spray off my monitor)

    Try one of the examples with the masm32 package. What you've got there
    is code for people who'd rather "rule" their OS than be ruled by it.

    Best,
    Frank


  3. Default Re: MASM32 help


    Frank Kotler wrote:
    > Protoman wrote:
    > > I'm trying to assemble this little program:
    > >
    > > .model flat
    > > .data
    > > msg db 'Hello, world!',0xa ;our dear string
    > > len equ $-msg
    > > .code
    > > .386
    > > _start:
    > >
    > > mov edx,len ;message length
    > > mov ecx,msg ;message to write
    > > mov ebx,1 ;file descriptor (stdout)
    > > mov eax,4 ;system call number (sys_write)
    > > int 0x80 ;call kernel
    > >
    > > mov eax,1 ;system call number (sys_exit)
    > > int 0x80 ;call kernel
    > >
    > > end _start
    > >
    > > It doesn't assemble. Here's the errors:
    > >
    > > Assembling: C:\hello.asm
    > > C:\hello.asm(1) : error A2085: instruction or register not accepted in
    > > current CPU mode
    > > C:\hello.asm(2) : error A2013: .MODEL must precede this directive
    > > C:\hello.asm(3) : error A2034: must be in segment block
    > > C:\hello.asm(4) : error A2034: must be in segment block
    > > C:\hello.asm(5) : error A2013: .MODEL must precede this directive
    > > C:\hello.asm(7) : error A2034: must be in segment block
    > > C:\hello.asm(9) : error A2034: must be in segment block
    > > C:\hello.asm(10) : error A2034: must be in segment block
    > > C:\hello.asm(11) : error A2034: must be in segment block
    > > C:\hello.asm(12) : error A2034: must be in segment block
    > > C:\hello.asm(13) : error A2034: must be in segment block
    > > C:\hello.asm(15) : error A2034: must be in segment block
    > > C:\hello.asm(16) : error A2034: must be in segment block
    > > C:\hello.asm(18) : error A2006: undefined symbol : _start
    > > C:\hello.asm(18) : error A2148: invalid symbol type in expression :
    > > _start
    > >
    > > I'm using MASM32 on WinXP. If you could help me, that'd be great.
    > > Thanks!!!!

    >
    > (wiping the coffee spray off my monitor)
    >
    > Try one of the examples with the masm32 package. What you've got there
    > is code for people who'd rather "rule" their OS than be ruled by it.
    >
    > Best,
    > Frank


    OK, the example minimum.asm assembles and links...why won't mine?


  4. Default Re: [Clax86list] MASM32 help

    On 7 Aug 2006 14:04:06 -0700
    "Protoman" <spamtrap@crayne.org> wrote:

    :OK, the example minimum.asm assembles and links...why won't mine?

    "C:\hello.asm(1) : error A2085: instruction or register not accepted in
    current CPU mode"

    Move the .386 directive to line 1, and try again.

    -- Chuck


  5. Default Re: MASM32 help


    Frank Kotler wrote:
    >
    > (wiping the coffee spray off my monitor)
    >
    > Try one of the examples with the masm32 package. What you've got there
    > is code for people who'd rather "rule" their OS than be ruled by it.
    >
    > Best,
    > Frank


    Just in case you didn't get the joke from Frank, that's a Linux
    assembly program, not a WinXP program. Even if you get it to compile
    under Win32, it's not going to run.
    Cheers,
    Randy Hyde


  6. Default 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


  7. Default Re: MASM32 help

    Hello.asm
    If you are working with MASM on a Windows32 platform you may
    consider installing the Hutch's comprehensive MASM package. The
    easiest way to get the MASM32 Package is to stop by at the Webster
    site: (http://webster.cs.ucr.edu/AsmTools/MASM/index.html) and download
    MASM along with other assembly language literature, in particular the
    Art of Assembly language programming book.

    Once you have installed MASM you will find the 'Hello.asm',
    which I am posting below for you to see, in the
    "\masm32\tutorial\console\demo1" directory.

    ;
    ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

    ; Build this with the "Project" menu using
    ; "Console Assemble and Link"

    ;
    ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

    .486 ; create 32 bit code
    .model flat, stdcall ; 32 bit memory model
    option casemap :none ; case sensitive

    include \masm32\include\windows.inc ; always first
    include \masm32\macros\macros.asm ; MASM support macros

    ; -----------------------------------------------------------------
    ; include files that have MASM format prototypes for function calls
    ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

    ; ------------------------------------------------
    ; Library files that have definitions for function
    ; exports and tested reliable prebuilt code.
    ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

    .code ; Tell MASM where the code starts

    ;
    ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

    start: ; The CODE entry point to the program

    print chr$("Hey, this actually works.",13,10)
    exit

    ;
    ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

    end start ; Tell MASM where the program ends



    Protoman wrote:
    > I'm trying to assemble this little program:
    >
    > .model flat
    > .data
    > msg db 'Hello, world!',0xa ;our dear string
    > len equ $-msg
    > .code
    > .386
    > _start:
    >
    > mov edx,len ;message length
    > mov ecx,msg ;message to write
    > mov ebx,1 ;file descriptor (stdout)
    > mov eax,4 ;system call number (sys_write)
    > int 0x80 ;call kernel
    >
    > mov eax,1 ;system call number (sys_exit)
    > int 0x80 ;call kernel
    >
    > end _start
    >
    > It doesn't assemble. Here's the errors:
    >
    > Assembling: C:\hello.asm
    > C:\hello.asm(1) : error A2085: instruction or register not accepted in
    > current CPU mode
    > C:\hello.asm(2) : error A2013: .MODEL must precede this directive
    > C:\hello.asm(3) : error A2034: must be in segment block
    > C:\hello.asm(4) : error A2034: must be in segment block
    > C:\hello.asm(5) : error A2013: .MODEL must precede this directive
    > C:\hello.asm(7) : error A2034: must be in segment block
    > C:\hello.asm(9) : error A2034: must be in segment block
    > C:\hello.asm(10) : error A2034: must be in segment block
    > C:\hello.asm(11) : error A2034: must be in segment block
    > C:\hello.asm(12) : error A2034: must be in segment block
    > C:\hello.asm(13) : error A2034: must be in segment block
    > C:\hello.asm(15) : error A2034: must be in segment block
    > C:\hello.asm(16) : error A2034: must be in segment block
    > C:\hello.asm(18) : error A2006: undefined symbol : _start
    > C:\hello.asm(18) : error A2148: invalid symbol type in expression :
    > _start
    >
    > I'm using MASM32 on WinXP. If you could help me, that'd be great.
    > Thanks!!!!



+ Reply to Thread

Similar Threads

  1. MASM32 command-line parameters
    By Application Development in forum ASM x86 ASM 370
    Replies: 3
    Last Post: 09-23-2007, 10:14 PM
  2. rename file? masm32? i pay for solution
    By Application Development in forum ASM x86 ASM 370
    Replies: 19
    Last Post: 06-20-2007, 07:41 AM
  3. Masm32 error line number limit
    By Application Development in forum ASM x86 ASM 370
    Replies: 5
    Last Post: 04-26-2007, 11:33 AM
  4. "TYPEDEF PTR with STRUCT" masm32
    By Application Development in forum ASM x86 ASM 370
    Replies: 3
    Last Post: 10-14-2006, 01:42 PM
  5. How to use CLFLUSH with masm32?
    By Application Development in forum ASM x86 ASM 370
    Replies: 2
    Last Post: 05-30-2006, 08:14 PM