flat assembler
Message board for the users of flat assembler.

Index > Main > Bug in fasm 1.50?

Author
Thread Post new topic Reply to topic
Joshua



Joined: 12 Jul 2003
Posts: 56
Location: Belgium
Joshua 10 Dec 2003, 21:07
Privalov,

Code:
temp1 = 1
if used temp1 ;false
    if ~defined VAR | defined temp2
        VAR = 1
        temp2 = 1
    end if
end if
if ~defined VAR | defined temp3
    VAR = 2
    temp3 = 1
end if
display VAR
    

here VAR is correctly 2
Code:
temp1 = 1
if used temp1 ;true
    if ~defined VAR | defined temp2
        VAR = 1
        temp2 = 1
    end if
end if
mov eax,temp1 ;just to make used return true
display VAR
    

here VAR is correctly 1
Code:
temp1 = 1
if used temp1 ;true
    if ~defined VAR | defined temp2
        VAR = 1
        temp2 = 1
    end if
end if
if ~defined VAR | defined temp3
    VAR = 2
    temp3 = 1
end if
mov eax,temp1 ;just to make used return true
display VAR
    

but here VAR is 2 (and should be 1)
The second part seems to be processed first, and thus the first never gets executed
Post 10 Dec 2003, 21:07
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 10 Dec 2003, 21:17
During the first pass fasm doesn't know yet that you are going to use the "temp1" label, so that "if" block is skipped and the second one is processed, as the "VAR" is still not defined. After that the second block is always processed, even when first block is processed in the later passes, when fasm correctly predicts that "temp1" label will be used (the prediction mechanism is based on the history of the usage in the previous pass, if at the end of pass assembler detects that there was some misprediction, it does that pass again).
Post 10 Dec 2003, 21:17
View user's profile Send private message Visit poster's website Reply with quote
Joshua



Joined: 12 Jul 2003
Posts: 56
Location: Belgium
Joshua 10 Dec 2003, 21:25
I think i understand this, but it doesn't feel logical, and i feel i shouldn't need to know the inner workings of the assembler to know what code will be generated...

Perhaps you have a solution for this problem?
Post 10 Dec 2003, 21:25
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 10 Dec 2003, 21:43
How would you interprete the expected result of such code:
Code:
if alpha*alpha=alpha
  if ~ alpha
   alpha = 1
  else
   alpha = beta+1
  end if
else
  alpha = alpha+1
end if
if alpha
  beta=alpha-1
end if    

without knowing the inner workings of assembler? I had to keep all such things in mind while designing fasm's multi-pass algorithms.

There is no any universal logic that would be applied to sources when the "if"-blocks can affect themselves; this is the similar problem to the classic antinomies of logic, which occur when you use some formulation talking about itself, like "the clause you are reading is false", etc.
Post 10 Dec 2003, 21:43
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 10 Dec 2003, 22:10
BTW, here is a small example how you can utilize fasm's multi-pass features even to solve equations Wink
Code:
macro solve equation_in_x,result
 {
  R equ result
  local t,X
  x equ X
  if ~ defined t
    t = 0
  else
    x = t
    if ~ equation_in_x
      x = x+1
    end if
    t = x
  end if
  R = t
  restore x,R
 }

solve x*x*x-31*x*x+311*x-1001=0 ,t

display '0'+t    

This example finds the smallest positive integer solution of the equation x^3-31x^2+311x-1001=0 and displays the result (one digit number). That is not an example of serious programming, it's just a little bit of fun with playing with fasm's abilities.
Post 10 Dec 2003, 22:10
View user's profile Send private message Visit poster's website Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1150
Location: Russian Federation
comrade 11 Dec 2003, 03:35
Nice!

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 11 Dec 2003, 03:35
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
scientica
Retired moderator


Joined: 16 Jun 2003
Posts: 689
Location: Linköping, Sweden
scientica 11 Dec 2003, 05:43
Shocked -w00t! (/jaw drops and hit the floor)
That is cool, I could't even imagine that fasm is this powerfull! Very Happy

_________________
... a professor saying: "use this proprietary software to learn computer science" is the same as English professor handing you a copy of Shakespeare and saying: "use this book to learn Shakespeare without opening the book itself.
- Bradley Kuhn
Post 11 Dec 2003, 05:43
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 11 Dec 2003, 09:03
This is not the best way to do it - I have written it as a demonstration how multi-pass resolving works. Similarly fasm can resolve such code:
Code:
times x/2+4 nop
x = $    

If after some iterations it actually finds the value which is equal to its truncated half with four added (it's 7 in this case), it generates the code; otherwise it will stop at the limit of passes and say "code cannot be generated". In the previous solving example it would also search only up to the limit of passes.
Much better would be single-pass method with manually set range, also the code is much cleaner and easier to understand in this case:
Code:
macro solve equation_in_x,result
{
  x = RANGE_MIN
  repeat RANGE_MAX-RANGE_MIN
    if ~ equation_in_x
      x = x + 1
    end if
  end repeat
  if equation_in_x
    result = x
  end if
}

RANGE_MIN = 0
RANGE_MAX = 1000

solve x*x*x-31*x*x+311*x-1001=0 ,t

display '0'+t
    
Post 11 Dec 2003, 09:03
View user's profile Send private message Visit poster's website Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo 11 Dec 2003, 11:12
Amazing demonstration Exclamation
You have shown a face of fasm that could open new scenarios. Amusing math contests. Sorry for Fermat, Gauss or Fourier. They haven't it
Twisted Evil
(however, imagine what they could have done with it Sad )
Post 11 Dec 2003, 11:12
View user's profile Send private message Yahoo Messenger Reply with quote
Tommy



Joined: 17 Jun 2003
Posts: 489
Location: Norway
Tommy 11 Dec 2003, 11:19
Impressive Privalov! Cool
Post 11 Dec 2003, 11:19
View user's profile Send private message Visit poster's website 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.