flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > fasmx - .while with .break .and .continue

Author
Thread Post new topic Reply to topic
Mike Gonta



Joined: 26 Dec 2010
Posts: 238
Location: the-ideom
Mike Gonta
Here is a modified .while macro with .break, .break .if, .continue and .continue .if.
Code:
use32
  .while 1
    nop
    .break
    nop
    .while 1
       nop
       .break .if eax=0
     .endw
     nop
     .continue .if signed eax<ebx
     nop
     .continue
     nop
   .endw    
Here is the macro
Code:
include 'macro/if.inc'

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 .while arg {
local while, endw
  while:
  JNCOND endw, arg
  macro .break arg1 \{
    match =.if x, arg1 \\{
      JCOND endw, x
      stat equ
    \\}
    match =stat, stat \\{
      jmp endw
    \\}
    restore stat
  \}
  macro .continue arg1 \{
    match =.if x, arg1 \\{
      JCOND while, x
      stat equ
    \\}
    match =stat, stat \\{
      jmp while
    \\}
    restore stat
  \}
  macro finish@while \{
    jmp while
    endw:
  \}
}

macro .endw {
  finish@while
  purge .break, .continue, finish@while
}

.endwhile FIX .endw    

_________________
Mike Gonta
the-ideom - now you know how to compile

https://mikegonta.com


Last edited by Mike Gonta on 10 Aug 2013, 20:53; edited 13 times in total
Post 24 Dec 2011, 19:33
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2914
Location: 0x77760000
typedef
But why ? Feeling smart ?
Post 24 Dec 2011, 23:41
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1399
Location: Toronto, Canada
AsmGuru62
Cool! But I never use these things.
Here is a reason: in a lot of cases I want to see if code falls down or branches to a label. It is good to know for optimizing reasons, because when code falls down - CPU does it faster than a branch, because fall down is predicted by CPU.

Here is a code example(s):
Code:
proc1:
   cmp     ecx, [glb_VectorCount]
      jae     .ret_null
   ;
   ; Get vector value and return
       ;
   mov     ebx, [glb_VectorBuf]
        mov     eax, [ebx + ecx*4]
  ret

.ret_null
        xor     eax, eax
    ret
;
; -----------------------------------------------
;
proc2:
    cmp     ecx, [glb_VectorCount]
      jb      .idx_good

       xor     eax, eax
    ret

.idx_good:
   mov     ebx, [glb_VectorBuf]
        mov     eax, [ebx + ecx*4]
  ret
    

Now, if you look closely at these two functions - they both do same thing: they VALIDATE the index into a vector of 32-bit values and if index is in range of [0 .. glb_VectorCount-1] - the value at that index is returned in EAX.

However, notice how the code flow changes in proc2 vs. proc1. I always look at my code flow in a statistical point of view. How many times the index will be CORRECT vs. INCORRECT? I assume that it will be CORRECT most of the time, so code flow in proc1 is much better than flow in proc2, because in proc2 code in MOST CASES will jump forward and the CPU can't predict these jumps.

So, back to the topic - using these macros hides the real code flow - it show the logic very nicely, however, but I value the performance -- we're talking about Assembler, not C.
Post 25 Dec 2011, 13:48
View user's profile Send private message Send e-mail 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-2020, Tomasz Grysztar.

Powered by rwasa.