The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Duequalf on April 18, 2007, 11:47:24 PM

Title: MASM Basics
Post by: Duequalf on April 18, 2007, 11:47:24 PM
Hi,

This is an extremely basic question.

I'm new to assemby programming, and I am attempting to create an executable from Assembly code. I have experience programming in C, C++, and Visual Basic.

I would like some information on the process of compiling an assembly program using MASM. I'm unsure exactly how to do this.
Title: Re: MASM Basics
Post by: Duequalf on April 19, 2007, 12:11:18 AM
In particular, it would be nice if there were step by step instructions on writing and compiling a hello world program.-Thanks
Title: Re: MASM Basics
Post by: dsouza123 on April 19, 2007, 01:17:55 AM
MASM32 has many example programs, including a console version of hello world.

Using the QEditor that comes with MASM32, it already has menu entries to assemble and link
then run the programs.

A GUI version of hello world.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
;                 Build this with the "Project" menu using
;                       "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

    .data                       ; Tell MASM where initialized data (variables) start
       szMessage  db "Hello World",0
       szCaption  db "The titlebar caption",0

    .data?                      ; Tell MASM where uninitialized data (variables) start
       somevar    dd ?          ; not used by this example


    .code                       ; Tell MASM where the code starts

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; The CODE entry point to the program

    invoke MessageBox, 0, addr szMessage, addr szCaption, MB_OK
    invoke ExitProcess, 0

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start                       ; Tell MASM where the program ends


The console version of hello world, it is the file C:\MASM32\TUTORIAL\CONSOLE\DEMO1\HELLO.ASM

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

;                 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
Title: Re: MASM Basics
Post by: Duequalf on April 19, 2007, 03:08:13 AM
Quote from: dsouza123 on April 19, 2007, 01:17:55 AM
MASM32 has many example programs, including a console version of hello world.

Using the QEditor that comes with MASM32, it already has menu entries to assemble and link
then run the programs.

A GUI version of hello world.

Thanks Dsouza.

That was extremely useful. I got it to work.  :U

My problem before was that I was getting the error message:

Assembling: C:\Documents.asm
MASM : fatal error A1000: cannot open file : C:\Documents.asm
...

I realized that MASM32 doesn't like spaces in file names, but I had difficulty figuring this out before since I didn't know the exact sequence of the compilation process. It was useful to know the exact order is assemble, link, and run. It was also useful to have a functional program to compile.

Thanks again.- Duequa
Title: Re: MASM Basics
Post by: Vortex on April 19, 2007, 04:54:41 PM
Did you try to enclose the file name between double quotes?
Title: Re: MASM Basics
Post by: dsouza123 on April 19, 2007, 06:05:09 PM
The problem is not just a space in the filename but a space anywhere in the path+filename.

When a path+filename is passed to the build.bat batch file called by qeditor
ml stops parsing at the first space
and attempts to use the resulting string for the source.

Example demonstrating the issue at the commd prompt

ml /c /coff C:\Documents and Settings\test.asm

results in

ml /c /coff C:\Documents and Settings\test.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: C:\Documents
MASM : fatal error A1000: cannot open file : C:\Documents



If the path+filename is either in double quotes

"C:\Documents and Settings\test.asm"

or expressed with the short name for the directory in cases where it results in the space being removed

C:\Docume~1\test.asm

then it will work.

How to get the batch file to enclose the path+filename is still unresolved.
Title: Re: MASM Basics
Post by: hutch-- on April 19, 2007, 10:27:49 PM
I posted a batch file in the MASM32 sub forum that solved the problem some time ago.
Title: Re: MASM Basics
Post by: dsouza123 on April 20, 2007, 03:12:15 AM
Thanks Hutch
That fixes it.

The link is
http://www.masm32.com/board/index.php?topic=6238.msg46450#msg46450