News:

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

Main Menu

How does one use @CurSeg to restore segment?

Started by jimg, March 27, 2020, 08:18:22 AM

Previous topic - Next topic

jimg

I have a macro that can be used in either the .data or .code segments.
I want to restore the correct segment after the macro has finished.
It looks like @CurSeg would be the answer, but I can't make it work.

If I run the following code, then way at the end of the program I get the error "Error A2080: Block nesting error: _DATA"

thisseg textequ @CurSeg  ; save current segment

%echo  @CurSeg
%echo thisseg
.data
%echo  @CurSeg
.code
  ; do something here
.data
  ; do something here

thissseg segment  ; restore segment

hutch--

In win32, data and code live in the only segment with FLAT memory model. They are leftovers from the MS-DOS era.

jj2007

One of my macros uses @CurSeg:

ClearLocals MACRO args ; use below last LOCAL
Local is, ct, tmp$
  is INSTR @CurSeg, <TEXT>
  ife is
tmp$ CATSTR <## line >, %@Line, <: put .code before the proc ##>
% echo tmp$
  endif
... other stuff ...
ENDM


For your purpose, it should be, at the end of the macro
if IsCode
  .code
else
  .data
endif

jimg

Ah, the brute force technique.  Thanks.  I thought I was just doing something wrong.

Hutch.   Is not the .data segment different from the .code segment, flat model or no?

hutch--


_japheth

Quote from: jimg on March 27, 2020, 01:25:54 PM
Is not the .data segment different from the .code segment, flat model or no?

Of course. In the flat model, the "only" segment is the FLAT "pseudo" segment, but the SEGMENT directive has got a somewhat different meaning: it defines sections, not segments.

And since FS and GS have survived even in 64-bit mode, it's still to some degree a "segmented" model.
Dummheit, gepaart mit Dreistigkeit - eine furchtbare Macht.

mabdelouahab


MY$   MACRO
      cs_Seg catstr @CurSeg
      
      ...
      
       ifidn cs_Seg, <CONST>
          .const              
       elseifidn cs_Seg, <_BSS>
          .data?
       elseifidn cs_Seg, <_TEXT>
          .code   
       endif
ENDM

daydreamer

Quote from: _japheth on March 27, 2020, 05:48:42 PM
Quote from: jimg on March 27, 2020, 01:25:54 PM
Is not the .data segment different from the .code segment, flat model or no?

Of course. In the flat model, the "only" segment is the FLAT "pseudo" segment, but the SEGMENT directive has got a somewhat different meaning: it defines sections, not segments.

And since FS and GS have survived even in 64-bit mode, it's still to some degree a "segmented" model.
I think FS is used for exception handler in Windows
read Randy Hyde's "write great code",there is a interesting chapter on those different sections ends up in executable,independent of computer language


my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

jimg

This works-

%xnaz textequ <@SubStr(<.data>,1,5*@InStr(,%@CurSeg,<_DATA>))>,<@SubStr(<.code>,1,5*@InStr(,%@CurSeg,<_TEXT>))>,<@SubStr(<.data?>,1,6*@InStr(,%@CurSeg,<_BSS>))>
; do some stuff
xnaz

but it only works once.

If I try to do two, e.g.

%xnaz textequ <@SubStr(<.data>,1,5*@InStr(,%@CurSeg,<_DATA>))>,<@SubStr(<.code>,1,5*@InStr(,%@CurSeg,<_TEXT>))>,<@SubStr(<.data?>,1,6*@InStr(,%@CurSeg,<_BSS>))>
xnaz

%xnaz textequ <@SubStr(<.data>,1,5*@InStr(,%@CurSeg,<_DATA>))>,<@SubStr(<.code>,1,5*@InStr(,%@CurSeg,<_TEXT>))>,<@SubStr(<.data?>,1,6*@InStr(,%@CurSeg,<_BSS>))>
xnaz


I get a syntax error : textequ on the second one, no idea why.  Happens in both masm and uasm, so ???

jj2007

Try this:
xnaz
% echo xnaz

You should see the echo in your output window, it's a five character string, like e.g. #s??t

In the first line, you are writing xnax textequ ...
In the second line, you are writing #s??t textequ ...

Now look at the echoed string and decide whether the second line should work or not  :smiley:

jimg

Ah, I see what you are saying.  It makes perfect sense to me except that wasn't what I was seeing.  I originally debugged this using echo.
But I get it.  What's not occurring to me immediately is how you set something already set with textequ to something else without the name being changed before the second text equate.  I remember reading that the manual said it could be done (in reference to EQU which can't be changed).  I vaguely remember some escape character, I'll have to look it up.  Thanks.

jimg

Duh,  okay, got it-

xnaz textequ <@SubStr(<.data>,1,5*@InStr(,%@CurSeg,<_DATA>))>,<@SubStr(<.code>,1,5*@InStr(,%@CurSeg,<_TEXT>))>,<@SubStr(<.data?>,1,6*@InStr(,%@CurSeg,<_BSS>))>
%xnaz
.data
xnaz textequ <@SubStr(<.data>,1,5*@InStr(,%@CurSeg,<_DATA>))>,<@SubStr(<.code>,1,5*@InStr(,%@CurSeg,<_TEXT>))>,<@SubStr(<.data?>,1,6*@InStr(,%@CurSeg,<_BSS>))>
%xnaz
.data?
xnaz textequ <@SubStr(<.data>,1,5*@InStr(,%@CurSeg,<_DATA>))>,<@SubStr(<.code>,1,5*@InStr(,%@CurSeg,<_TEXT>))>,<@SubStr(<.data?>,1,6*@InStr(,%@CurSeg,<_BSS>))>
%xnaz

that works.


Edit:

Nope, still not working.  But I'll get it eventually.

jimg

Okay, finally.  Had to make it a local so it didn't carry down to definition in next macro.

Here's a simple example of usage-

;macros to echo size of code and data between two points to output
startsize macro
local xnaz
%xnaz textequ <@SubStr(<.data>,1,5*@InStr(,%@CurSeg,<_DATA>))>,<@SubStr(<.code>,1,5*@InStr(,%@CurSeg,<_TEXT>))>,<@SubStr(<.data?>,1,6*@InStr(,%@CurSeg,<_BSS>))>
.code
plxcode=$
.data
plxdata=$
%xnaz
endm
showsize macro info:vararg
local xnaz
%xnaz textequ <@SubStr(<.data>,1,5*@InStr(,%@CurSeg,<_DATA>))>,<@SubStr(<.code>,1,5*@InStr(,%@CurSeg,<_TEXT>))>,<@SubStr(<.data?>,1,6*@InStr(,%@CurSeg,<_BSS>))>
.code
aax=$-plxcode
.data
aay=$-plxdata
aaz=aax+aay
%echo info @CatStr(<size of code=>,%aax,<  .data used=>,%aay,<  total=>,%aaz)
%xnaz
endm