flat assembler
Message board for the users of flat assembler.

Index > Main > 'match' vs 'if defined', different behavior

Author
Thread Post new topic Reply to topic
ejamesr



Joined: 04 Feb 2011
Posts: 52
Location: Provo, Utah, USA
ejamesr 13 Jan 2015, 18:51
When a symbol is defined as nothing (which is entirely valid and useful in certain cases), this can be detected by the 'match' statement, but not by 'if defined'.

When defining symbolic constants, 'if defined' works only if a value is given to the constant; if it is defined as nothing, an error is reported (error: invalid expression). This is because, after the preprocessor does its job by replacing the symbolic constant with nothing, the instruction 'if defined MYLABEL' becomes 'if defined', which is an error.

In the sample below, 'define MYLABEL' and 'MYLABEL equ' both cause errors for 'if defined'. The problem is solved by assigning some value to the label, such as 'define MYLABEL 1' or 'MYLABEL equ 1':
Code:
; Compile the code below as is, and then with the 'define' line commented out
define MYLABEL  ; Causes an error for 'if defined' test below
                ; When changed to 'define MYLABEL 1', it works with all cases below
                ; Similar results with 'MYLABEL equ' vs 'MYLABEL equ 1'
                                
; Test if defined with 'match' statement...
; One of these always works, whether 'define' line is commented out or not
; But when MYLABEL is given a value, it never thinks it is defined!
match ,MYLABEL {
  display 'match indicates label is defined',13,10
}
match x,MYLABEL {
  display 'match indicates label is NOT defined',13,10
}

; Test if defined with 'if defined' statement...
; The test for 'defined' does not work when defined as nothing
if defined MYLABEL
  display 'label is defined',13,10
else
  display 'label is NOT defined',13,10
end if    

I hope this info helps others avoid unnecessary time figuring out how to eliminate 'if defined' errors.

EDIT:
Use the following match statement to detect when label is defined as 1:
Code:
match =1,MYLABEL {
  display 'match indicates label is defined',13,10
}    
Post 13 Jan 2015, 18:51
View user's profile Send private message Send e-mail Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 13 Jan 2015, 19:01
Code:
if MYLABEL eq ;fixes the problem in the current case, when you define as nothing
    
Post 13 Jan 2015, 19:01
View user's profile Send private message Visit poster's website Reply with quote
ejamesr



Joined: 04 Feb 2011
Posts: 52
Location: Provo, Utah, USA
ejamesr 13 Jan 2015, 19:12
Good point, I think I'll use that in some cases, as it's simpler.

Thanks!
Post 13 Jan 2015, 19:12
View user's profile Send private message Send e-mail Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 14 Jan 2015, 20:20
ejamesr,

Don't mix preprocessor and assembler features blindly.
Post 14 Jan 2015, 20:20
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.