III.10.2 IFxx and ELSEIFxx Instructions

The particular IFxx instructions are working as follows:

IF <expr> The IF condition is TRUE, if the expression <expr> is not equal to 0.
The value of <expr> must be known on pass 1!
IFN <expr> The IFN condition is TRUE, if the expression <expr> is equal to 0.
The value of <expr> must be known on pass 1!
IFDEF <symbol> The IFDEF condition is TRUE, if the <symbol> is defined in the program.
Forward references to <symbol> are not allowed!
IFNDEF <symbol> The IFNDEF condition is TRUE, if the <symbol> is not defined in the program.
Forward references to <symbol> are not allowed!
IFB <literal> The IFB (if blank) condition is TRUE, if the <literal> is empty.
<literal> is a string, enclosed in angle brackets.
IFNB <literal> The IFNB (if not blank) condition is TRUE, if the <literal> is not empty.
<literal> is a string, enclosed in angle brackets.
  Although the IFB and IFNB statements are valid also outside of macros, they can be applied sensefully in macro bodies only. Usually they are used to decide, whether macro arguments have been left blank, or not.

The corresponding ELSEIFxx instructions are working respectively.

Example 1:

IF .. ELSE .. ENDIF construction
TARGET	EQU 0	;configuration:  1 for application board
		;--------------  0 for evaluation board
IF TARGET
	ORG 0	;program start address of application board
ELSE
	ORG 8000H	;program start address of evaluation board
ENDIF
Currently the program is configured for the evaluation board version.


Example 2:

IFNDEF .. ELSE .. ENDIF construction
;EVA_537 EQU 0		;symbol undefined: 80C537 application board
			;symbol defined:   80C537 evaluation board
IFNDEF EVA_537
 CLOCK	EQU 16		;clock frequency of application board
 CSEG AT 0		;program start address of application board
ELSE
 CLOCK	EQU 12		;clock frequency of evaluation board
 CSEG AT 8000H		;program start address of evaluation board
ENDIF
Currently the program is configured for the application board version.


Example 3:

IFB .. ELSE .. ENDIF construction
DECIDE MACRO X, Y
 IFB <X&Y>
	NOP
	NOP
 ELSE
	DB	'&X,&Y'
 ENDIF
ENDM
If the above macro is invoked as follows,
	DECIDE Nonsense
the parameter X will be replaced by “Nonsense” and the parameter Y by a zero length string. Thus the IFB literal becomes <Nonsense>, and the macro will be expanded to:
	DB	'Nonsense,'
If the macro will be invoked without arguments,
	DECIDE
the parameters X and Y will be replaced by zero length strings both, and the IFB literal becomes <>. Thus the macro will be expanded to:
	NOP
	NOP
Macros are explained in detail in chapter “III.11 Macro Processing”.


Example 4:

IFNDEF .. ELSEIF .. ELSEIF .. ELSE .. ENDIF construction

The symbol BAUDRATE serves to define the UART baudrate:
IFNDEF BAUDRATE
	LJMP	AUTOBAUD	;automatic baudrate detection
ELSEIF BAUDRATE EQ 9600
	MOV	TH1, #0FDH	;9600 baud
ELSEIF BAUDRATE EQ 1200
	MOV	TH1, #0E8H	;1200 baud
ELSE
 $ERROR(baudrate not implemented)
ENDIF
If the symbol BAUDRATE is not defined at all, a jump to the label AUTOBAUD is performed.
If the symbol BAUDRATE is defined with one of the legal values 9600 or 1200, timer 1 is initialized accordingly.
If the symbol BAUDRATE is defined with another value, a corresponding user-defined error message is generated.