Chapter 3 Introduction to Operation

3.5Conditional Assembly and Linking

The Cross-Assembler provides many assembler directives. Assembler directives are not converted directly to machine language, but are used to control how the assembler processes.

For example, during the development stage a programmer may want to include a special program only for debugging. This program must be deleted when the product is complete. That can be accomplished by editing the source file, but if that editing is spread throughout the program, mistakes will be easy to make.

It is convenient to use assembler directives in such cases. The conditions for assembly are defined at the start of the program, and programs to be assembled when the conditions are satisfied or not satisfied are written in the source file.

Create source file

Using an editor, create the program program5.asm shown below.

The contents of program5.asm are as follows.

#define

DEBUG

 

*

macro

adr, dat

dat_set

 

mov

adr, A0

 

mov

dat, D0

 

mov

D0, (A0)

*

endm

 

section CODE, PUBLIC,2

 

_CODE

 

main

DEBUG

 

#ifdef

dat_set

data1, 0x11

#else

dat_set

data1, 0x22

#endif

_DATA

section DATA, PUBLIC, 2

0

data1

dw

data2

dw

0

 

end

 

The operation of this program is meaningless. The program will be used instead to explain program structure as it pertains to conditional assembly.The define DEBUG on the first line selects DEBUG as a condition by defining the identifier DEBUG. In the assembly control block starting with #ifdef DEBUG on line 13, the instructions between #ifdef to #else will be assembled if DEBUG has been defined, and the instructions between #else to #endif will be assembled if DEBUG is undefined. In this example DEBUG was defined on line 1, so the instructions in the defined block will be assembled.

This program also uses a macro control directive. Lines 4 to 8 are the macro definition. The macro's name is dat_set, and it has two parameters (adr, dat).

38 Conditional Assembly and Linking

Page 50
Image 50
Panasonic MN1030 user manual Conditional Assembly and Linking, Create source file, #ifdef