flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Macro get from label name number.

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1314
Roman 02 Dec 2022, 09:58
Fasmw 1.73
Code:
lLoop_8:
MacroNum lLoop_ ;get me mov ecx,8

DLoop_5:
MacroNum DLoop_ ;get me mov ecx,5
    
Post 02 Dec 2022, 09:58
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8103
Location: Kraków, Poland
Tomasz Grysztar 02 Dec 2022, 10:36
You could do something like:
Code:
macro MacroNum prefix
{
  local i,c,d
  virtual at 0
    db `prefix
    i = $
    match label, $label \{ db \`label \}
    repeat i
      load c byte from %-1
      load d byte from i+%-1
      if c <> d
        err 'the last label does not fit the current prefix'
      end if
    end repeat
    c = 0
    repeat $-2*i
      load d byte from 2*i+%-1
      if d < '0' | d > '9'
        err 'the label number contains incorrect digits'
      end if
      c = c * 10 + d - '0'
    end repeat
  end virtual
  mov ecx,c
}    
But this requires defining labels with a special macro, to be able to catch their names into "$label" symbol:
Code:
macro label name
{
  label name
  $label equ name
}

label lLoop_8
MacroNum lLoop_ ; mov ecx,8

label DLoop_5
MacroNum DLoop_ ; mov ecx,5    
A straightforward conversion to fasmg allows to catch even the regular labels:
Code:
macro MacroNum prefix
  local i,c,d
  virtual at 0
    db `prefix
    i = $
    match label, $label
      db `label
    end match
    repeat i
      load c byte from %-1
      load d byte from i+%-1
      if c <> d
        err 'the last label does not fit the current prefix'
      end if
    end repeat
    c = 0
    repeat $-2*i
      load d byte from 2*i+%-1
      if d < '0' | d > '9'
        err 'the label number contains incorrect digits'
      end if
      c = c * 10 + d - '0'
    end repeat
  end virtual
  mov ecx,c
end macro

struc (name) ? def&
  name def
  match :, def
    $label equ name
  end match
end struc

lLoop_8:
MacroNum lLoop_ ; mov ecx,8

DLoop_5:
MacroNum DLoop_ ; mov ecx,5    


Last edited by Tomasz Grysztar on 02 Dec 2022, 13:25; edited 1 time in total
Post 02 Dec 2022, 10:36
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1314
Roman 02 Dec 2022, 12:29
Thanks Tomasz !
Its amazing and awesome !
Post 02 Dec 2022, 12:29
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1314
Roman 02 Dec 2022, 12:36
I write:
label lloop_160
MacroNum lloop_ and get ecx =3Dh not A0h
Post 02 Dec 2022, 12:36
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8103
Location: Kraków, Poland
Tomasz Grysztar 02 Dec 2022, 13:24
Oh, I messed up the direction, it was parsing 061 instead of 160. It should be:
Code:
load d byte from 2*i+%-1    
instead of:
Code:
load d byte from $-%    
Post 02 Dec 2022, 13:24
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1314
Roman 04 Dec 2022, 08:47
Code:
macro MacroNum prefix
{
  local i,c,d
  virtual at 0
    db `prefix ; i not understood why this needed ?
    i = $
    match label, $label \{ db \`label \} ; i not understood why this needed ?
    repeat i
      load c byte from %-1
      load d byte from i+%-1
      if c <> d
        err 'the last label does not fit the current prefix'
      end if
    end repeat
    c = 0
    repeat $-2*i
      load d byte from 2*i+%-1
      if d < '0' | d > '9'
        err 'the label number contains incorrect digits'
      end if
      c = c * 10 + d - '0'
    end repeat
  end virtual
  mov ecx,c
}    

And not understood why needed prefix ? If $label almost the same.
Post 04 Dec 2022, 08:47
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.