flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Detect last macro invocation

Author
Thread Post new topic Reply to topic
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 13 Jun 2010, 22:46
I'm fiddling with data import / library / import and contrived one trick to detect the last macro invocation (not exactly macro: preprocessor doesn't support forward references, thus assembler was used too):
Code:
MORE equ more
macro detect_last {
  MORE = 1              ; tell previous macro invocation that there's more
  local more            ; create new name for marker
  restore MORE          ; cleanup, not required
  MORE equ more         ; remember marker's name
  if ~defined more      ; check that marker is defined later
    display "last "     ; not defined - this is the last one
  end if
  display "invocation", 13, 10
}
; Test
detect_last
detect_last
detect_last
detect_last    
This is not something completely new: structuring macros in MACRO\IF.INC use similar technique (for different purpose though).

Probably this can be used to merge .code- and .data-defined sections too when using extended headers (and place string literals from invoke into .rdata at last Wink).
Post 13 Jun 2010, 22:46
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20453
Location: In your JS exploiting you and your system
revolution 14 Jun 2010, 04:21
I nice trick. Can you show some example code using this?
Post 14 Jun 2010, 04:21
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 14 Jun 2010, 10:06
revolution,

I'm going to use this in data import wrapper macros that will allow to have several import definition blocks throughout source and will actually declare them as a whole at the end of last data import / end data. They're still WIP (imatch, I miss you so much… Wink).

Another possible application for this is auto-inlined function if I'll manage to convince FASM that the following code can be resolved without scope error:
Code:
display "Pass: ", '0'+%p, 13, 10
macro display_cond cond {
  irps symbol, cond \{ display \`symbol, ' ' \}
  match line, __line__ \{ display "@", \`line, 13, 10 \}
}

usage_counter = 0

; the following is expanded «inline» macro
more = 1
usage_counter = usage_counter+1
if ~defined more?0              ; false
  display_cond ~defined more?0
  usage_count = usage_counter   ; this shouldn't take effect; if I comment this line, everything is OK
end if
if usage_count>1                ; this forward reference should be valid
  display_cond usage_count>1
  if ~defined more?0
    display_cond ~defined more?0
    push ..over?1
    ..func:
    mov eax, 'Hi!'; func body
    ret
    ..over?1:
  else
    display_cond defined more?0
    call ..func
  end if
else
  mov eax, 'Hi!'; func body
end if

; the following is expanded «inline» macro again
more?0 = 1
usage_counter = usage_counter+1
if ~defined more?2              ; true
  display_cond ~defined more?2
  usage_count = usage_counter   ; this one should be used
end if
if usage_count>1
  display_cond usage_count>1
  if ~defined more?2
    display_cond ~defined more?2
    push ..over?3
    ..func:
    mov eax, 'Hi!'; func body
    ret
    ..over?3:
  else
    display_cond defined more?2
    call ..func
  end if
else
  mov eax, 'Hi!'; func body
end if    
Post 14 Jun 2010, 10:06
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.