News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Question about organizing segments ?

Started by EmuRatti39, April 21, 2019, 07:39:51 AM

Previous topic - Next topic

EmuRatti39

How many types of segments are there ?






.MODEL small, c ; This statement is required before you
                         ; can use other simplified segment directives

.STACK              ; Use default 1-kilobyte stack

.DATA
    <Your initialized data>
    ......
.DATA?

   <Your uninitialized data>
   ......
.CONST
   <Your constants>
   ......
.CODE
   <label>
    <Your code>
   .....

.STARTUP               ; Generate start-up code
                              ; Place instructions here

.EXIT                      ; Generate exit code


END  <label>






FORTRANS

Hi,

Quote from: EmuRatti39 on April 21, 2019, 07:39:51 AM
How many types of segments are there ?

   Well, for the 8086/8088 there are four segment registers, so
four segments associated with them.  CS is the code segment,
DS for the DATA segment, ES the extra segment, and SS is the
stack segment.

   Using the SEGMENT directive you can define as many different
segments as you want.  But there are only a few "types".  From
MASM 6.0, and later, added some modifiers that were not available
in earlier versions.  For the earlier versions:

name    SEGMENT align combine 'class'

Combine = PUBLIC, STACK, COMMON, MEMORY, and AT.

'Class' = segments to place in contiguous memory.


   Using the simplified segment directives, from MASM 5.0 onward,
there are seven directives.


.CODE           ; Code Segment
.STACK          ; Stack Segment
.DATA           ; Data Segment
.DATA?          ; Data segment for uninitialized data.
.FARDATA        ; Data Segment for "far" data.
.FARDATA?       ; DATA Segment for uninitialized "far"data.
.CONST          ; DATA Segment for constant data.


HTH,

Steve N.

EmuRatti39

@FORTRAN

Thanks for the detailed reply .

I have few more questions

I see a few more segment directives  like

.BSS
.TEXT

What are these for ?


jj2007

From the Masm programming guide:
The simplified segment directives are .MODEL, .CODE, .CONST, .DATA, .DATA?, .FARDATA, .FARDATA?, .STACK, .STARTUP, and .EXIT

For playing:
include \masm32\include\masm32rt.inc

_TEXT SEGMENT PUBLIC 'CODE'
mov eax, eax
_TEXT ENDS
_BSS SEGMENT PUBLIC 'CODE'
mov ebx, ebx
_BSS ENDS

.code
start:
int 3
  inkey "ok?"
  exit

end start

EmuRatti39

@jj2007

Yes , i have this book called ,
Programmers guide : Microsoft® MASM Assembly-Language Development System Version 6.1 For MS-DOS ® and Windows ™ Operating Systems

The simplified segment directives are .MODEL, .CODE, .CONST, .DATA, .DATA?, .FARDATA, .FARDATA?, .STACK, .STARTUP, and .EXIT.

According to this Website ,
http://downloads.ti.com/docs/esd/SPNU118O/Content/SPNU118O_HTML/introduction_to_object_modules.html

There is few more mentioned ,

    .text -- Used for program code.
    .bss -- Used for uninitialized objects (global variables).
    .data -- Used for initialized non-const objects (global variables).
    .const -- Used for initialized const objects (string constants, variables declared const).
    .cinit -- Used to initialize C global variables at startup.
    .stack -- Used for the function call stack.
    .sysmem - Used for the dynamic memory allocation pool.




 


EmuRatti39

OK ,Is this correct ?



.MODEL                          ;This statement is required before you can use other simplified segment directives

.STACK                           ;Use default 1-kilobyte stack

.DATA                             ;This contains initialized variables

.DATA?                           ;This contains uninitialized variables

.BSS                               ;This contains uninitialized variables

.CONST                          ;This contains declaration of constants

.CODE                            ;This is where your code resides

.TEXT                             ;This is where your code resides

.STARTUP
.EXIT
end

FORTRANS

Hi,

Quote from: EmuRatti39 on April 21, 2019, 10:58:48 PM
@FORTRAN

Thanks for the detailed reply .

   You're welcome.

Quote
I have few more questions

I see a few more segment directives  like

.BSS
.TEXT

What are these for ?

   As later versions of MASM were created additional directives
and modifiers were added.  .BSS is a synonym for .DATA?.  .TEXT
is the same as .CODE.  MASM was modified to have directives
that "look" more like high level language constructs.  Big changes
were made in both versions 5.0 and 6.0.

   However my hard copy references, which are easy to find, are
way out of date.  My PDF copies of newer references tend to
remain in obscure places on a mostly unused drive.  And newer
versions tend to be backwards compatible.  So some of my replies
are a bit simplified when referring to newer MASM features.  Thus
I mentioned "earlier versions".  That way I can give a reply in a
reasonable amount of time, accuracy, and effort.

Regards,

Steve N.

P.S.  While typing this in, you made a new posting.  Your comments
look correct.

SRN

EmuRatti39

@FORTRANS ,

Thanks a lot for all the replies .

This thread cleared a lot of my doubts

:biggrin:

jj2007

Note that in practice you need only three of them for 32-bit code:
include \masm32\include\masm32rt.inc

.data?
somevar dd ?

.data
txHello db "Hello World", 0

.code
start:
  inkey offset txHello
  exit
end start

EmuRatti39

@jj2007

Thanks a lot for the reply .

Quote from: jj2007 on April 22, 2019, 01:29:59 AM
Note that in practice you need only three of them for 32-bit code:
include \masm32\include\masm32rt.inc

.data?                  ;This contains uninitialized variables
somevar dd ?

.data                    ;This contains initialized variables
txHello db "Hello World", 0

.code                    ;This is where your code resides
start:
  inkey offset txHello
  exit
end start


That means if you plan to code an assembly language based Win32 console application or a Win32 window application in MASM32 , One only makes use of these 3 segment directives .

That is great :)

jj2007

Quote from: EmuRatti39 on April 22, 2019, 02:40:43 AMThat means if you plan to code an assembly language based Win32 console application or a Win32 window application in MASM32 , One only makes use of these 3 segment directives.

YES :P

It may look different if you code in 16- or 64-bit land, and one may find exotic reasons for using other directives even with 32-bit code, but 99.9% of all 32-bit coding can be done with these three directives. Check the \Masm32\Examples\... folders.

EmuRatti39

@jj2007

Thanks a lot for the reply .


I think my next stop should be this book .

I have a lot more basic things to learn before i should try to learn the more advanced examples in those folders .

So i have decided to take a print out of this PDF book i have .


Programmers guide : Microsoft® MASM Assembly-Language Development System Version 6.1 For MS-DOS ® and Windows ™ Operating Systems

:)

jj2007

Quote from: EmuRatti39 on April 22, 2019, 04:53:10 AMProgrammers guide : Microsoft® MASM Assembly-Language Development System Version 6.1 For MS-DOS ® and Windows ™ Operating Systems

Very old stuff, stay away from that. Check the Help & Tutorials section. The Kusswurm books are also good, I've heard, but they cost 20 bucks. For free, you can get Paul Carter's excellent book - don't miss it.

The fun starts with Windows programming, and here the free Iczelion tutorials are your friend. Don't miss \Masm32\Examples, though, because learning by doing is at least as important as the theory. What is your background, if I may ask? Did you learn other programming languages before?

EmuRatti39

@jj2007

I only know some C language .

Thanks for the book suggestions

:)