Image Map Image Map
Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: MASM question--determining segment of a variable for conditional assembly

  1. #1
    Join Date
    Jan 2007
    Location
    Pacific Northwest, USA
    Posts
    36,113
    Blog Entries
    18

    Default MASM question--determining segment of a variable for conditional assembly

    I'm a little fuzzy-minded today, but I can't for the life of me, figure out the answer to this one.

    Here's a code snippet (assume MASM 6.13):

    Code:
            .model  small,c
            .data
    one     dw      100 dup 100
            .code
    two     dw      100 dup 100
    ...
    clearit  macro   what,howmany
            mov     di,offset what
            if      seg what EQ @code
            push    cs
            pop     es
            endif
            mov     cx,howmany
            cld
            rep stosw
            endm
    
            clearit one
            clearit two
            end
    Now, clearly, this won't work, as the SEG operator defers resolution until link time. A possible solution to this might be to compare the name of the segment that the variable resides in, but I don't see a MASM pseudo-op that will allow that. You can see the utility of such a facility, as one can move variables between segments and have the correct code generated regardless.

    So how does one solve this?

  2. #2
    Join Date
    Jun 2012
    Location
    UK - Worcester
    Posts
    4,461

    Default

    Can’t you just load SEG(what) into ES and OFFSET(what) into DI regardless of what segment ‘what’ lives in? You might have to craft the remaining code to suite.

    I also remember LES to load a 32-bit pointer into ES and another 16 bit offset register.

    Dave

  3. #3
    Join Date
    Jan 2007
    Location
    Pacific Northwest, USA
    Posts
    36,113
    Blog Entries
    18

    Default

    I'm not talking about link- or run- time, but assembly time.

    Clearly, if I know that ES=DS in this case and "one" resides in the .data segment, I don't need to generate any extra code.
    Similarly, if I want to address "two" through ES, the simplest way would be to generate code to set ES = CS. This can matter a lot when writing code that's moved at run-time.

    All I want to do is extract the segment name that a variable belongs in at assembly time. Since the assembly cross reference shows this, it's clear that it's known to MASM.

    I suppose I could define a "shadow" symbol indicating the segment association with each variable, but that seems like the long way around.

    A corollary to this is the determination of other variable attributes, such as alignment at assembly time. MASM appears to have a hole in the pseudo-op repertory.

  4. #4
    Join Date
    Jun 2012
    Location
    UK - Worcester
    Posts
    4,461

    Default

    >>> I'm not talking about link- or run- time, but assembly time.

    Understand.

    >>> A possible solution to this might be to compare the name of the segment that the variable resides in.

    I have never seen such a feature in MASM.

    Dave

  5. #5
    Join Date
    Jan 2007
    Location
    Pacific Northwest, USA
    Posts
    36,113
    Blog Entries
    18

    Default

    For that matter, I'm not even aware of a pseudo-op that will tell you the name of the segment that you're currently in.

    It seems that with MASM 6, Microsoft took the attitude of "who needs to program new 16 bit code?"

    EDIT: Belay that last comment. @Curseg returns the name of the current segment, but there's still no way to obtain the name of the segment that an item belongs to.
    Last edited by Chuck(G); December 6th, 2020 at 01:59 PM.

  6. #6

    Default

    I have been using an early version of MASM in my IBM5155. Managed to write a few simple assembly language programs that worked.

    But compared to say 8080 programming in my sol-20, the issues of segmented memory, as they apply to the MASM assembler itself, for the 8088, baffled me. So I just stuck with an "only segment" in my code which is plenty big enough for the short and somewhat amateurish programs I have been able to write.

    I wish I had enough knowledge to even adequately understand your question, because I'm sure that the answer would be very enlightening, if I could get to grips with the implications of it.

    In the books I have, it is the memory segmentation, with respect to how the assembler handles that, that is the least well covered. When that sort of thing happens, it is generally my experience that the author of the text was struggling with it too.

  7. #7
    Join Date
    Jan 2007
    Location
    Pacific Northwest, USA
    Posts
    36,113
    Blog Entries
    18

    Default

    The difference between MASM 5 and 6 and earlier versions is a matter of lightyears. The problem is that MS implemented new features in a sort of haphazard way. By the time the Windows standard became the 32-bit model, MS seemingly lost interest in filling out MASM capabilities, restricting themselves to adding new instruction support. That, and the migration toward C for most systems-level programming. I've used other strong-typed assembly languages where all relevant information about a data item was kept in an assembly time array, which was convenient.

    Still, in this respect, MASM is a bunch better than most microcontroller assemblers.

    At any rate, I think I may have a hack solution. I'll post it later after I check it out.

  8. #8
    Join Date
    Jan 2007
    Location
    Pacific Northwest, USA
    Posts
    36,113
    Blog Entries
    18

    Default

    Okay, here's my hack. One defines a macro that also defines a "shadow" textequ macro that contains the current segment.

    It's nasty, but it works. Consider the following snipped that loads es depending on the segment within which the "dws" macro occurs. Word "one" is in the _DATA segment and word "two" is in the code segment:

    Code:
    Microsoft (R) Macro Assembler Version 6.14.8444		    12/07/20 20:24:49
    x.asm							     Page 1 - 1
    
    
    					.MODEL small,c
    
    				dws	macro	what,val
    				what	dw	val
    				%?&what	textequ	<@CurSeg>
    					endm
    
    				;	load es with either DS or CS
    
    				ldeseg 	macro	what
    				%	ifdef	?&what
    				%	ifidn 	<?&what>,<_TEXT>
    					push	cs
    					pop	es
    				%	elseifidn  <?&what>,<_DATA>
    					push	ds
    					pop	es
    					endif
    					endif
    					endm
    
     0000					.data
    
    					dws	one,0
     0000 0000		     1	one	dw	0
    
     0000					.code
    
    					dws	two,0
     0000 0000		     1	two	dw	0
    
    					ldeseg	one
     0002  1E		     1		push	ds
     0003  07		     1		pop	es
    					ldeseg	two
     0004  0E		     1		push	cs
     0005  07		     1		pop	es
    
    					end
    				 
    Microsoft (R) Macro Assembler Version 6.14.8444		    12/07/20 20:24:49
    x.asm							     Symbols 2 - 1
    
    
    
    
    Macros:
    
                    N a m e                 Type
    
    dws  . . . . . . . . . . . . . .	Proc
    ldeseg . . . . . . . . . . . . .	Proc
    
    
    Segments and Groups:
    
                    N a m e                 Size     Length   Align   Combine Class
    
    DGROUP . . . . . . . . . . . . .	GROUP
    _DATA  . . . . . . . . . . . . .	16 Bit	 0002	  Word	  Public  'DATA'	
    _TEXT  . . . . . . . . . . . . .	16 Bit	 0006	  Word	  Public  'CODE'	
    
    
    Symbols:
    
                    N a m e                 Type     Value    Attr
    
    ?one . . . . . . . . . . . . . .	Text   	 _DATA
    ?two . . . . . . . . . . . . . .	Text   	 _TEXT
    @CodeSize  . . . . . . . . . . .	Number	 0000h	 
    @DataSize  . . . . . . . . . . .	Number	 0000h	 
    @Interface . . . . . . . . . . .	Number	 0001h	 
    @Model . . . . . . . . . . . . .	Number	 0002h	 
    @code  . . . . . . . . . . . . .	Text   	 _TEXT
    @data  . . . . . . . . . . . . .	Text   	 DGROUP
    @fardata?  . . . . . . . . . . .	Text   	 FAR_BSS
    @fardata . . . . . . . . . . . .	Text   	 FAR_DATA
    @stack . . . . . . . . . . . . .	Text   	 DGROUP
    one  . . . . . . . . . . . . . .	Word	 0000	  _DATA	
    two  . . . . . . . . . . . . . .	Word	 0000	  _TEXT	
    
    	   0 Warnings
    	   0 Errors
    Like I said, it's nasty and I don't plan on using it. Just to demonstrate that it's possible.

  9. #9
    Join Date
    Feb 2017
    Location
    Guildford, U.K.
    Posts
    475
    Blog Entries
    1

    Default

    Isn't that a little risky? As the Model (Tiny, Small, Medium, Huge) will cause the current contents of CS and DS to vary, depending upon *which* CODE and DATA segment is currently addressed, as well as the programmer being expected to know where the variable was defined?

  10. #10
    Join Date
    Jan 2007
    Location
    Pacific Northwest, USA
    Posts
    36,113
    Blog Entries
    18

    Default

    The code is based on some assumptions, namely that the same CS value is used throughout the assembly module and that DS will refer to the default data segment. My interest is mostly for small and tiny models, but perhaps also compact. The basic idea is to provide for movable code without bringing the linker into the picture.

    Color this exercise academic and something really asking the question why is ES wired to STOS (i.e. no segment override possible)?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •