flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > .for macro

Author
Thread Post new topic Reply to topic
KeSqueer



Joined: 07 Mar 2008
Posts: 2
KeSqueer 07 Mar 2008, 20:51
I'm beginning with fasm macro syntax. So, I tried to write my own macro .for (c 'for' simple implementation) and got this:
Code:
macro JCOND label,v1,c,v2
{
 match any,c
 \{
   cmp v1,v2
   j\#c label
 \}
 match ,c
 \{
   PARSECOND parsed@cond,v1
   match cond,parsed@cond \\{ JCONDEXPR label,cond \\}
 \}
}

macro   .for [args]
{
 common
  local ..for
  local ..endfor
  local ..continue
  __FOR equ ..for
  __ENDFOR equ ..endfor
  __CONTINUE equ ..continue

  macro for_expr [argsa]
  \{
   cont = 1
   if cont
    match arg=+==val, argsa \\{
    add arg, val
    cont = 0\\}
   end if
   if cont
    match arg=-==val, argsa \\{
    sub arg, val
    cont = 0\\}
   end if
   if cont
    match arg=--, argsa \\{
    dec arg
    cont = 0\\}
   end if
   if cont
    match arg=++, argsa \\{
    inc arg
    cont = 0\\}
   end if
   if cont
    match arg=<<val, argsa \\{
    shl arg, val
    cont = 0\\}
   end if
   if cont
    match arg=>>val, argsa \\{
    shr arg, val
    cont = 0\\}
   end if
   if cont
    match arg=|==val, argsa \\{
    or arg, val
    cont = 0\\}
   end if
   if cont
    match arg=&==val, argsa \\{
    and arg, val
    cont = 0\\}
   end if
   if cont
    match arg=^==val, argsa \\{
    xor arg, val
    cont = 0\\}
   end if
   if cont
    match arg==val, argsa \\{
    mov arg, val
    cont = 0\\}
   end if
  \}

define current args

  match =(a=:b=:c=), current
  \{
    for_expr a
    JNCOND __ENDFOR, b
  \}
  __FOR:

  macro .break
  \{
    jmp __ENDFOR
  \}

  macro .continue
  \{
    jmp __CONTINUE
  \}

  macro .endf
  \{
    match =(a=:b=:c=), current
    \\{
      __CONTINUE:
      for_expr c
      JCOND __FOR, b
      __ENDFOR:
      restore __FOR
      restore __ENDFOR
      restore __CONTINUE
      restore current
    \\}
  \}
}    


It can be used in following format:
.for (statement1, statement2,... : conditions : statement3, statement4...)
; Some code may be placed here. Nested loops are allowed.
.endf

Example:
Code:
.for (eax=0, ebx=[base], edx=[ordword]: eax < 10: [ebx+eax*4] |= edx, eax++)
; some code may be placed here
.endf    


I've not included such operators as *,/ etc because it is not a trivial task, but operators --,++, <<,>> are included.

So, what do you think about this macro? I'll conside any comments.

---------
Excuse me for my English, I'm just learning it Wink
Post 07 Mar 2008, 20:51
View user's profile Send private message ICQ Number Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP 11 Mar 2008, 16:44
Very nice, I'm just trying to learn FASM macros too. After my current project I'll probably take some time off to learn the more high-level parts of FASM, hopefully the designer(s) will keep adding more features to it's syntax!

The only downside I see of having much more high-level things is it appears (as I saw in other threads) that people are becoming split in their programming styles. Some using almost all high-level features, and others being so low-level purists that it takes me several minutes to figure out what they're trying to do!

PS: I tried using this:

Code:
struct Food_
  start = $
  ... some other stuff
  end = $
ends

; declare the structure, and later:
mov eax,Food.end-Food.start
; which returned 0!
    

Do u know what happened? Just a small thing I remembered, thought you would be the person to inquire.
Post 11 Mar 2008, 16:44
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 19 Sep 2008, 23:08
AlexP,

"This is by design". Wink

struct macro redefines db, dw, etc. (even itself - for nested substructs) so they don't define/reserve bytes. Instead they collect information about struct's structure Wink in symbolic constant fields@struct. After that – only virtual at 0.

It's the ends macro that actually defines/reserves bytes. No wonder that end == start (after preprocessing ... some other stuff is empty).

By the way, isn't sizeof.Food working?
Post 19 Sep 2008, 23:08
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.