News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Parameters of SEGMENT directives

Started by Quan, June 09, 2025, 08:29:02 PM

Previous topic - Next topic

Quan

Hi, I'm again trying to understand some MASM syntax. I found the SEGMENT directive on Microsoft's documentation, so I wrote a test programme to understand all the parameters of this directive.

include \Masm32\include\masm32rt.inc

forTest SEGMENT read write execute
    strHW db 'Hello World!', 0
    strQuery db 'test.exe', 0
    VA EQU $ - offset strCFA
    strTest db 260 dup(0)
   
start:
    mov strCFA, 'J'
    invoke MessageBox, 0, offset strQuery, offset strF1A, 0

    push 0
    call ExitProcess

forTest ENDS

end start

This programme returns these errors:
Z:\3\test.asm(3) : error A2008: syntax error : read
Z:\3\test.asm(4) : error A2034: must be in segment block
Z:\3\test.asm(5) : error A2034: must be in segment block
Z:\3\test.asm(6) : error A2034: must be in segment block
Z:\3\test.asm(7) : error A2034: must be in segment block
Z:\3\test.asm(9) : error A2034: must be in segment block
Z:\3\test.asm(10) : error A2034: must be in segment block
Z:\3\test.asm(11) : error A2034: must be in segment block
Z:\3\test.asm(11) : error A2006: undefined symbol : strF1A
Z:\3\test.asm(11) : error A2114: INVOKE argument type mismatch : argument : 3
Z:\3\test.asm(11) : error A2006: undefined symbol : strQuery
Z:\3\test.asm(11) : error A2114: INVOKE argument type mismatch : argument : 2
Z:\3\test.asm(13) : error A2034: must be in segment block
Z:\3\test.asm(14) : error A2034: must be in segment block
Z:\3\test.asm(16) : fatal error A1010: unmatched block nesting : forTest

I suppose the error is that it doesn't recognise READ, WRITE and EXECUTE as valid parameters for the SEGMENT directive. I don't understand how this is happening since it seems to be the correct syntax according to the documentation. Even using the code from this post by jj2007, which has a similar syntax, yields a similar error.

Can anyone help me with this? Thanks in advance!

Edit: execute, not executable

TimoVJL

with poasm:.686
includelib user32.lib

MessageBoxA proto stdcall :ptr, :ptr, :ptr, :dword
ExitProcess proto stdcall :dword

forTest SEGMENT read write execute
    strF1A db 'Hello World!', 0
    strQuery db 'test.exe', 0
;    VA EQU $ - offset strCFA
    strTest db 260 dup(0)
   
start:
    mov strTest, 'J'
    invoke MessageBoxA, 0, offset strQuery, offset strF1A, 0

    push 0
    call ExitProcess

forTest ENDS

end start
May the source be with you

Vortex

Hello Quan,

Adapting Timo's Poasm example to Masm :

.686
.model flat,stdcall
option casemap:none

MessageBoxA proto stdcall :ptr, :ptr, :ptr, :dword
ExitProcess proto stdcall :dword

_data SEGMENT PARA PUBLIC 'DATA'
    strF1A db 'Hello World!', 0
    strQuery db 'test.exe', 0
;    VA EQU $ - offset strCFA
    strTest db 260 dup(0)
_data ENDS

_text SEGMENT PARA PUBLIC 'CODE'

start:

    mov strTest, 'J'
    invoke MessageBoxA, 0, offset strQuery, offset strF1A, 0

    push 0
    call ExitProcess

_text ENDS

end start

NoCforMe

... but why in the world would you want to use those old-school SEGMENT directives when we have the new simplified ones?
.data
[put initialized data here]

.data?
[uninitialized data]

.code

I just don't see the point. You ain't writing DOS programs here ...
32-bit code and Windows 7 foreva!

sinsi


Quan

Quote from: sinsi on June 10, 2025, 08:09:25 AMexecutable is wrong, try execute.
Ah oops, that was an oversight on my part. I changed it to executable in a vain attempt to fix the issue (suggested by GPT :eusa_boohoo:). In the beginning, and just now, I changed it back to execute and that didn't fix the problem. The assembler doesn't seem to recognise neither READ, WRITE or EXECUTE.

Quote from: NoCforMe on June 10, 2025, 05:42:07 AM... but why in the world would you want to use those old-school SEGMENT directives when we have the new simplified ones?
Mainly curiosity, because the documentation mentions nothing about the directive being deprecated so it should still work. In my example, I want to make a segment that is readable, writable and executable; and the new, simplified ones don't do that. A few other parameters don't work either, e.g. ALIGN(n) and the rest of the parameters for Characteristics; and I want to make use of them.

And thanks for the code samples, @TimoVJL and @Vortex. The first one is in PoASM, which I'm not yet familiar with. The second one doesn't seem to address some parameters that is causing the error (READ, WRITE, EXECUTE, SHARED, etc.) But I appreciate them regardless, and will study them a bit more.

One more thing, these parameters don't work for everyone else right? Just to make sure it's not a problem with my machine.

sinsi

Quote from: Quan on June 10, 2025, 12:43:37 PMOne more thing, these parameters don't work for everyone else right? Just to make sure it's not a problem with my machine.
If I fix the missing label then it assembles fine with ML 14.44.35208.0

You cannot view this attachment.

NoCforMe

Quote from: Quan on June 10, 2025, 12:43:37 PMA few other parameters don't work either, e.g. ALIGN(n) and the rest of the parameters for Characteristics; and I want to make use of them.

Well, so far as that one goes, a single ALIGN directive at the top of your .code section will accoplish exactly the same thing.

Just wondering why you wanted a segment that had read, write and execute attributes: just curiousity?
32-bit code and Windows 7 foreva!

Quan

Quote from: NoCforMe on June 10, 2025, 01:37:15 PMJust wondering why you wanted a segment that had read, write and execute attributes: just curiousity?
Essentially yes. I'm doing an exercise that's using MASM. And with my style of studying, I like to dig around and explore all the possible options that may be beneficial to my exercise, no matter how useless it might be, because it helps me understand the syntax/language more thoroughly. Here I'm trying to make a section that has all of those attributes, because it is possible when I edit the file manually with, say, CFF Explorer.

Quote from: sinsi on June 10, 2025, 01:35:00 PMIf I fix the missing label then it assembles fine with ML 14.44.35208.0
You cannot view this attachment.
Oh, what "missing label" are you talking about?

Also my ML.exe seems to be super outdated compared to yours. And I wasn't aware of a way to update ML.exe, where can I find that?
Microsoft (R) Macro Assembler Version 6.14.8444

sinsi

Quote from: Quan on June 10, 2025, 02:07:24 PMOh, what "missing label" are you talking about?
Your original code didn't define strF1A

Quote from: Quan on June 10, 2025, 02:07:24 PMAlso my ML.exe seems to be super outdated compared to yours. And I wasn't aware of a way to update ML.exe, where can I find that?
Microsoft (R) Macro Assembler Version 6.14.8444
I use ML from Visual Studio, the free community version.
There is a link to download the tools somewhere on the forum.


Quan

Ah right lol, that was supposed to be a test programme for me to understand how this works, so forgive me for all the mixed up labels and hiccups in my code.  :mrgreen:

And thanks, I really thought using ML.exe that comes packaged with the MASM32 installer should be good enough, didn't think it would be so out of date. I'll try to get that file and test this again, hopefully it works.

NoCforMe

Well now that you've satisfied your curiosity I'd really, really recommend just sticking with the regular simplified segment directives.

In my own programming I've never run into a situation where they didn't work for me, and that includes writing DLLs. Maybe if you were writing Windows device drivers you'd need them, or if you're messing around with old 16-bit DOS code.

Otherwise just use them; it'll simplify your assembly programming.
32-bit code and Windows 7 foreva!

jj2007

Quote from: NoCforMe on June 10, 2025, 07:00:05 PMIn my own programming I've never run into a situation where they didn't work for me

Self-modifying code is one example.

NoCforMe

Quote from: jj2007 on June 10, 2025, 08:20:05 PM
Quote from: NoCforMe on June 10, 2025, 07:00:05 PMIn my own programming I've never run into a situation where they didn't work for me

Self-modifying code is one example.

True, but how often is that actually used or even needed?
Very rarely is my Guess®™.
32-bit code and Windows 7 foreva!

Vortex

#14
Poasm can assemble the code below :

.686
.model flat,stdcall
option casemap:none

MessageBoxA PROTO :DWORD,:DWORD,:DWORD,:DWORD
MessageBox TEXTEQU <MessageBoxA>

ExitProcess PROTO :DWORD

PUBLIC start

_data SEGMENT 'DATA'

message1  db 'This is a test.',0
caption1  db 'Hello',0

_data ENDS

_text SEGMENT 'CODE'

start:

    xor     eax,eax
    invoke  MessageBox,eax,ADDR message1,ADDR caption1,eax

    invoke ExitProcess,0

_text ENDS

.drectve SEGMENT BYTE 'CONST'

db '-entry:_start'

.drectve ENDS

END

It's preferable to use the simplified directives like .data and .code