The MASM Forum

General => The Campus => Topic started by: MASM on August 21, 2012, 06:02:17 AM

Title: Macro-less problems
Post by: MASM on August 21, 2012, 06:02:17 AM
I'm new to this forum, and MASM32, so don't be too harsh...

I have been writing MASM (6.11) programs for DOS to learn the language, and am now trying to move into Win32 MASM programming.

I'm having issues making a simple code segment with a main program! I want to use the macros that come with MASM32, but I'd rather know how to do everything without them, and use them simply to save time, not as a crutch.

With the .code directive I can at least make a program that will assemble, but if I try to make my own segment and main procedure, I get link error:A1010 unmatched block nesting.

Here is my attempt after looking at some of the examples:

.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

SSEG SEGMENT STACK
DB 32 DUP("STACK---")
SSEG ENDS

CSEG SEGMENT
ASSUME CS:CSEG, SS:SSEG

MAIN PROC
    invoke StdOut,expand_prefix(reparg("Test"))
RET
MAIN ENDP
CSEG ENDS
END MAIN
Title: Re: Macro-less problems
Post by: jj2007 on August 21, 2012, 06:23:32 AM
First things first: Welcome to the Forum :icon14:

Here is a template - have a look at \masm32\include\masm32rt.inc to see what it does:
include \masm32\include\masm32rt.inc

.data?
dummy dd ?

.data

dummy2 dd 123

.code
start: invoke StdOut,expand_prefix(reparg("Test"))
inkey chr$(13, 10, 10, "That was cute, now hit a key")
invoke ExitProcess, 0

end start

Title: Re: Macro-less problems
Post by: MASM on August 21, 2012, 07:12:45 AM
Thank you, but I can already make functional code using the .data .stack and .code directives, but I would prefer to be able to not depend on them.

Could I get an elaborated equivalent of the macros, or a reason for the link error?

EDIT: thanks for the RT include, that makes includes very convenient!
Title: Re: Macro-less problems
Post by: qWord on August 21, 2012, 07:31:10 AM
hi,
the problem on using the macros is that some of them switch the segment using the .code/.date/.data? directive instead of SEGEMNT/ENDS (some versions of MASM have problems(=bug) with nested segments).

include \masm32\include\masm32rt.inc
XYZ SEGMENT PAGE

sz db "Hello World",13,10,0

XYZ ENDS
_TEXT SEGMENT ; this is MASM's .code-Segment

main proc

print OFFSET sz
inkey
exit

main endp

_TEXT ENDS
end main
Title: Re: Macro-less problems
Post by: hutch-- on August 21, 2012, 09:20:57 AM
MASM is very well behaved when you use the .CODE, .DATA, .DATA? and .CONST and as long as you are tidy and end each section you can routinely mix them if it suits your programming style.


.code
  ; assembler statements
.data
  ; initialised data statements
.data?
  ; uninitialised data statements
.code
  ; back to code section assembler statements.


You have to have some gain in using the older segment notation and about the only place I can see this is for folks who need to shift from real mode to protected mode where using a blanket .MODEL statement will not work.
Title: Re: Macro-less problems
Post by: japheth on August 21, 2012, 11:20:36 AM

Well, using the full segment directives is not just some weird Luddite attitude - they are more powerful and in fact the only way to have full control of COFF section attributes ( since Masm version 8 ).

But, as you have noticed, the macros supplied with Masm32 may come into the way if you're using them. My simple advice is: avoid the macros, don't get used to them, the costs are higher than the benefits.
Title: Re: Macro-less problems
Post by: MASM on August 21, 2012, 02:52:19 PM
japheth - I realize this, and am asking how to not use the macros and use SEGMENT ... ENDS, but it isn't working...

Can anyone show source that doesn't use .code, but instead properly using the SEGMENT directives? All I'm asking for is proper syntax and use :'(
Title: Re: Macro-less problems
Post by: hutch-- on August 21, 2012, 03:48:43 PM
If you have the MASM manual from the version you use in your avatar, then you have the correct notation if you wish to use that form. Just remember that 32 bit Windows code uses FLAT memory model. If you bother to obtain PECOFF.DOCX from Microsoft you will see that a 32 bit PE executable uses SECTIONS, not SEGMENTS and those sections are defined as _TEXT for .CODE, then both initialised data and UNinitialised data.

Where you can gain in terms of notation is if you are writing boot code that starts in real mode then shifts to protected mode. Apart from such uses you do not use SEGMENTS at all in FLAT memory model 32 bit portable executable files.
Title: Re: Macro-less problems
Post by: jj2007 on August 21, 2012, 03:56:14 PM
Quote from: japheth on August 21, 2012, 11:20:36 AMMy simple advice is: avoid the macros

Or, even better: use C. As we all know, JWasm is written in C, not in Assembler ;)
Title: Re: Macro-less problems
Post by: japheth on August 21, 2012, 04:39:54 PM
Quote from: MASM on August 21, 2012, 02:52:19 PM
Can anyone show source that doesn't use .code, but instead properly using the SEGMENT directives? All I'm asking for is proper syntax and use :'(

The syntax is ok - more or less. As already mentioned, the problem are the macros: reparg() and similar. Remove them and the code assembles fine:


.386

ExitProcess proto stdcall :dword

CSEG SEGMENT FLAT 'CODE'
invoke ExitProcess, 0
CSEG ENDS

end



Title: Re: Macro-less problems
Post by: hutch-- on August 21, 2012, 04:53:33 PM
The problem is mixing technologies that are not understood, if you want to use fully specified segments, you write your own code as many of the macros available for 32 bit MASM use the highlevel .CODE, .DATA, .DATA? notation.
Title: Re: Macro-less problems
Post by: Manos on August 21, 2012, 05:02:33 PM
Quote
Or, even better: use C. As we all know, JWasm is written in C, not in Assembler ;)

I think, MASM is also written in C.

Manos.
Title: Re: Macro-less problems
Post by: hutch-- on August 21, 2012, 06:53:50 PM
I don't know about the later versions but 6.14/5 had parts written in MASM itself. How do you know ???? MASM has a signature stack handling that is different from a C compiler and hand written code has a different logic to compiler generated code, it can usually be read by a human being where compiled C code looks like machine generated code, it is often fast and often uses statistically determined logical structures but you don't have to be clever to read the difference.

I personally don't care if an assembler is written in FORTRAN (NOTE THE UPPER CASE) as long as it works correctly.
Title: Re: Macro-less problems
Post by: MASM on August 22, 2012, 11:11:31 AM
Hutch -
Okay, I do. I haven't made it all the way through the technical manuals yet though... (it is MASM 6.11, is it still exactly the same?)

Thanks all, I guess I'll have to get used to using the segment macros :/
Title: Re: Macro-less problems
Post by: hutch-- on August 22, 2012, 05:30:28 PM
There is something else I should have mentioned, as MASM creates object modules (COFF in Win32) you can write your own object modules and assemble them using the older form of segment definitions if you like and then link the object module into an exe using other object modules (library functions etc ....).

About the only place where you can get into trouble is with macros if their technical assumptions use higher level section definitions while you are using the older segment definitions.
Title: Re: Macro-less problems
Post by: sinsi on August 22, 2012, 06:36:17 PM
From what I remember, you can use segments as well as simplified .code/.data as well.
.code expands to "_TEXT segment public"
.data expands to "_DATA segment public"

The only problem I remember was that using a dotname (.code, .data) would close the preceding segment (an invisible "_TEXT ends" for example).
Mixing .code and "_TEXT segment public" will line the procs up one after another, same as multiple .code lines.