Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
74
Добавлен:
14.04.2015
Размер:
2.03 Mб
Скачать

Keil Software — A51/AX51/A251 Macro Assembler and Utilities

93

 

 

Expressions

An expression is a combination of operands and operators that must be calculated by the assembler. An operand with no operators is the simplest form of an expression. An expression can be used in most places where an operand is required.

Expressions have a number of attributes that are described in the following sections.

Expression Classes

Expressions are assigned classes based on the operands that are used. The

3

following classes apply to expressions:

Expression Class

Description

 

 

N NUMB

A classless number.

 

 

C ADDR

A CODE address symbol.

 

 

D ADDR

A DATA address symbol.

 

 

I ADDR

An IDATA address symbol.

 

 

X ADDR

An XDATA address symbol.

 

 

B ADDR

A BIT address symbol.

 

 

 

 

 

 

CO ADDR

A CONST address symbol.

 

 

EC ADDR

An ECONST address symbol.

 

 

CE ADDR

An ECODE address symbol.

 

 

ED ADDR

An EDATA address symbol.

 

 

EB ADDR

An EBIT address symbol.

 

 

HD ADDR

An HDATA address symbol.

 

 

HC ADDR

An HCONST address symbol.

 

 

Shaded directives and options are available only in AX51 and A251.

94

Chapter 3. Writing Assembly Programs

 

 

Typically, expressions are assigned the class NUMBER because they are composed only of numeric operands. You may assign a class to an expression using a class operand. An address symbol value is automatically assigned the class of the segment where it is defined. When a value has a class, a few rules apply to how expressions are formed:

1.

The result of a unary operation has the same class as its operand.

2.

The result of all binary operations except + and – will be a NUMBER type.

3.

If only one of the operands of an addition or subtraction operation has a class,

3

the result will have that class. If both operands have a class, the result will be

a NUMBER.

 

This means that a class value (i.e. an addresses symbol) plus or minus a number (or a number plus a class value) give a value with class.

Examples

data_address - 10

gives a data_address value

10 + edata_address

gives an edata_address value

(data_address - data_address)

gives a classless number

code_address + (data_address - data_address)

gives a code_address value

Expressions that have a type of NUMBER can be used virtually anywhere. Expressions that have a class can only be used where a class of that type is valid.

Shaded directives and options are available only in AX51 and A251.

Keil Software — A51/AX51/A251 Macro Assembler and Utilities

95

 

 

Relocatable Expressions

 

Relocatable expressions are so named because they contain a reference to a

 

relocatable or external symbol. These types of expressions can only be partially

 

calculated by the assembler since the assembler does not know the final location

 

of relocatable segments. The final calculations are performed by the linker.

 

A relocatable expression normally contains only a relocatable symbol, however,

 

it may contain other operands and operators as well. A relocatable symbol can

 

be modified by adding or subtracting a constant value.

 

Examples for valid relocatable expression

 

3

relocatable_symbol + absolute_expression

 

relocatable_symbol - absolute_expression

 

absolute_expression + relocatable_symbol

 

There are two basic types of relocatable expressions: simple relocatable

 

expressions and extended relocatable expressions.

 

Shaded directives and options are available only in AX51 and A251.

96

Chapter 3. Writing Assembly Programs

 

 

Simple Relocatable Expressions

Simple relocatable expressions contain symbols that are defined in a relocatable segment. Segment and external symbols are not allowed in simple relocatable expressions.

Simple relocatable expression can be used in four contexts:

 

1.

As an operand to the ORG directive.

 

2.

As an operand to a symbol definition directive (i.e. EQU, SET)

3

3.

As an operand to a data initialization directive (DB, DW or

 

 

DD)

 

 

4.

As an operand to a machine instruction

 

 

 

Examples for simple relocatable expressions

 

 

 

 

 

 

 

 

REL1 + ABS1 * 10

 

 

 

 

 

REL2 - ABS1

 

 

 

 

 

REL1 + (REL2 - REL3)

assuming REL2 and REL3 refer to the same segment.

 

 

Invalid form of simple relocatable expressions

 

 

 

 

 

 

(REL1 + ABS1) * 10

relocatable value may not be multiplied.

 

 

(EXT1 - ABS1)

this is a general relocatable expression

 

 

REL1 + REL2

you cannot add relocatable symbols.

Shaded directives and options are available only in AX51 and A251.

Keil Software — A51/AX51/A251 Macro Assembler and Utilities

97

 

 

Extended Relocatable Expressions

The extended relocatable expressions have generally the same rules that apply to simple relocatable expressions. Segment and external symbols are allowed in extended relocatable expressions. Extended relocatable expressions may be used only in statements that generate code as operands; these are:

As an operand to a data initialization directive (DB, DW or DD)

As an operand to a machine instruction

Examples for extended relocatable expressions

REL1 + ABS1 * 10

 

3

EXT1 - ABS1

 

LOW (REL1 + ABS1)

 

WORD2 (SEG1)

 

 

Invalid form of simple relocatable expressions

 

 

(SEG1 + ABS1) * 10

relocatable value may not be multiplied.

 

(EXT1 - REL1)

you can add/subtract only absolute quantities

 

LOW (REL1) + ABS1

LOW may be applied only to the

 

 

final relocatable expression

 

Shaded directives and options are available only in AX51 and A251.

98 Chapter 3. Writing Assembly Programs

Examples with Expressions

 

EXTRN CODE (CLAB)

; entry in CODE space

 

EXTRN DATA (DVAR)

; variable in DATA space

 

MSK

EQU

0F0H

; define a symbol to replace 0xF0

 

VALUE

EQU

MSK - 1

; another constant symbolic value

 

 

LVAL

EQU

12345678H

; LVAL get the value 12345678H

 

 

 

 

?PR?FOO SEGMENT CODE

 

 

 

 

 

 

 

 

 

RSEG

?PR?FOO

 

 

 

 

 

 

 

ENTRY:

MOV

A,#40H

; load register with constant

 

 

 

MOV

R5,#VALUE

; load constant symbolic value

 

 

 

3

 

 

MOV

R3,#(0x20 AND MASK)

; examples for calculations

 

 

MOV

R7,#LOW (VALUE + 20H)

 

 

 

 

 

 

 

 

MOV

R6,#1 OR (MSK SHL 4)

 

 

 

 

 

 

 

 

MOV

R0,DVAR+20

; load content from address DVAR+20

 

 

 

MOV

R1,#LOW (CLAB+10)

; load low byte of address CLAB+10

 

 

 

 

 

 

MOV

WR4,#WORD2 (LVAL)

; load high word of LVAL

 

 

 

 

 

 

 

MOV

DR0,#ENTRY

; load low word of addr. ENTRY to DR0

 

 

 

 

MOVH

DR0,#WORD2 (ENTRY)

; load high word of addr. ENTRY to DR0

 

 

 

MOV

R4,@DR0

; load content of ENTRY to R4

 

 

;

 

 

 

 

 

 

 

 

 

 

 

MOV

R5,80H

; load DATA addr. 80H (= SFR P0) to R5

 

 

 

MOV

R5,EDATA 80H

; load EDATA address 80H to R5

 

 

 

 

 

SETB

30H.2

; set bit at 30H.2 (long address)

 

 

 

 

SETB

20H.2

; set bit at 20H.2 (short address)

END

Shaded directives and options are available only in AX51 and A251.

Соседние файлы в папке HLP