4 OPERANDS AND EXPRESSIONS

This chapter contains the following sections:

Operands
Operands and Addressing Modes
Expressions
Number
Expression String
Symbol
Operators
Addition and Subtraction
Sign Operators
Multiplication and Division
Shift Operators
Relational Operators
Bitwise Operators
Logical Operators
Selection Operators
Section Type of Expressions
Functions
Mathematical Functions
String Functions
Macro Functions
Assembler Mode Functions
Detailed Description
Predefined Symbols

4.1 Operands

An operand is the part of the instruction that follows the instruction opcode. There can be one, two, three or even no operands in an instruction. The operands of the assembly instruction has one of the following types:

Operands Description

expr any valid expression as described in the section Expressions.

reg any valid register. See the section Registers for a complete description.

symbol a symbolic name as created by an equate. A symbol can be an expression or a register name.

address a combination of expr, reg and symbol.

An expression may have an associated 'section type', which is used by the assembler to perform type checking. The section types correspond with the available address spaces of the XA processor:

Type Description XA address range
BIT bit address space 20H ... 3FH in each 64K segment
CODE code address space 0 ... 64K-1
DATA direct addressable data 0 ... 3FFH in each 64K segment,
400H ... 7FFH (SFR)
IDATA indirect addressable space 0 ... 16M-1
XDATA external address space 200H ... 16M-1

Table 4-1: 8051 section memory types

Type Description XA address range
BITADDR BIT addressable data space 20H ... 3FH in each 64K segment
EDATA SmartXA EEPROM Data Memory for SmartXA only 0 ... 7FBFH
HCODE code address space 0 ... 16M-1
HDATA 24-bit indirect addr. data 0 ... 16M-1

Table 4-2: XA section memory types

Additionally, the section type NUMBER is used for expressions representing a typeless number. If the expression can be completely evaluated at assembly time, it is called an absolute expression; if it is not, it is called a relocatable expression. See the section Expressions for more details.

4.1.1 Operands and Addressing Modes

The XA assembly language has several addressing modes. These are listed below with a short description.

Register

The instruction specifies the register which contains the operand.

Syntax:

mnemonic register

Indirect

A complete 24-bit data memory address is formed by an 8-bit segment register concatenated with a 16-bit pointer register.

Syntax:

mnemonic [register]

Indirect offset

A singed 8-bit or 16-bit offset is added to the contents of a pointer register, and the result is concatenated with the 8-bit segment register DS to produce a complete 24-bit address.

Syntax:

mnemonic [register+offset]

Indirect autoincrement

A complete 24-bit data memory address is formed by an 8-bit segment register concatenated with a 16-bit pointer register. The pointer register is automatically incremented afterwards.

Syntax:

mnemonic [register+]

Direct

The instruction contains the operand address (10 bits). The first 1K bytes of data in each segment are direct accessable. The associated data segment is always identified by the 8-bit contents of DS.

Syntax:

mnemonic direct_address
mnemonic label

SFR

The instruction contains the SFR address (10 bits). The 1K SFR space is always directly addressed (400h-7FFh) and is mapped above the direct data space.

Syntax:

mnemonic direct_SFR_address

Immediate

An immediate operand is either an 4/5, 8 or 16-bit number, which is encoded as part of the instruction. Immediate operands are indicated by the # sign before the expression defining the value of the operand.

Syntax:

mnemonic #number

Relative

The instruction contains the relative 8-bit (-128 to 127) or 16-bit offset (-32768 to 32767) from the next instruction to the target address.

Syntax:

mnemonic label

Bit

The instruction contains the bit address (10 bits).

Syntax:

mnemonic bitaddress
mnemonic
bitbyte.bitpos
mnemonic
C

4.2 Expressions

An operand of an assembler instruction or directive is either an assembler symbol, a register name or an expression. An expression is a sequence of symbols that denotes an address in a particular memory space or a number.

Expressions that can be evaluated at assembly time are called absolute expressions. Expressions where the result is unknown until all sections have been combined and located are called relocatable expressions. When any operand of an expression is relocatable the entire expression is relocatable. Relocatable expressions are emitted in the object file and evaluated by the linker or the locator. Relocatable expressions may only contain integral functions. An error is emitted when during object creation non-IEEE relocatable expressions are found.

An expression has a type which depends on the type of the identifiers in the expression. See section 4.2.4, Expression Type, for details.

The assembler evaluates expressions with 64-bit precision in two's complement.

The syntax of an expression can be any of the following:

- number

- expression_string

- symbol

- $

- expression binary_operator expression

- unary_operator expression

- ( expression )

- function

All types of expressions are explained below and in the following sections.

$ represents the current location counter value in the section currently active.

( ) You can use parentheses to control the evaluation order of the operators. What is between parentheses is evaluated first.

Examples:

(3+4)*5     ; Result is 35.
            ; 3 + 4 is evaluated first.
3+(4*5)     ; Result is 23.
            ; 4 * 5 is evaluated first.
            ; parentheses are superfluous
            ; here

4.2.1 Number

Numeric constants can be used in expressions. If there is no postfix, the assembler assumes the number is decimal.

number can be one of the following:
- bin_numB
- dec_num (or dec_numD)
- oct_numO (or oct_numQ)
- hex_numH

Lowercase equivalences are allowed: b, d, o, q, h.

bin_num is a binary number formed of '0'-'1' ending with a 'B' or 'b'.

dec_num is a decimal number formed of '0'-'9', optionally followed by the letter 'D' or 'd'.

oct_num is an octal number formed of '0'-'7' ending with an 'O', 'o', 'Q' or 'q'.

hex_num is a hexadecimal number formed of the characters '0'-'9' and 'a'-'f' or 'A'-'F' ending with a 'H' or 'h'. The first character must be a decimal digit, so it may be necessary to prefix a hexadecimal number with the '0' character.

A number may be written without a following radix indicator if the input radix is changed using the RADIX directive. For example, a hexadecimal number may be written without the suffix H if the input radix is set to 16 (assuming an initial radix of 10). The default radix is 10.

4.2.2 Expression String

An expression_string is a string with an arbitrary length evaluating to a number. The value of the string is calculated by taking the first 4 characters padded with 0 to the left.

string is a string of ASCII characters, enclosed in single (') or double (") quotes. The starting and closing quote must be the same. To include the enclosing quote in the string, double it. E.g. the string containing both quotes can be denoted as: " ' ""  " or ' ' ' " '.
See the chapter Macro Operations for the differences between single and double quoted strings.

Examples:

'A'+1   ; a 1-character ASCII string,
        ; result 42H
,9C,+1  ; a 2-character ASCII string,
        ; result 3944H

4.2.3 Symbol

A symbol is an identifier. A symbol represents the value of an identifier which is already defined, or will be defined in the current source module by means of a label declaration, equate directive or the EXTRN directive.

Examples:

CON1 EQU 3H     ; The variable CON1 represents
                ; the value of 3

MOV R1, CON1 + 020H  ; Move contents of address
                     ; 023H to register R1

When you invoke the assembler, the following predefined symbols exist:

_ASXA contains a string with the name of the assembler ("asxa")

4.2.4 Expression Type

The type of an expression is either a number (integral) or an address. The result type of an expression depends on the operator and its operands. The tables below summarize all available operators.

Please note:

1. when an illegal combination (denoted as *) is found, a warning is emitted and the expression type will be undefined;

2. a label is of type 'address'; an equate symbol has the type of the equate expression;

3. the type of an untyped symbol can be an address or a number, depending on the context; the result of the operation can be determined using the tables;

4. the binary logical and relational operators (||, &&, ==, !=, <, <=, >, >=) accept any combination of operands, the result is always the integral number 0 or 1;

5. the binary shift and bitwise operators <<, >>, |, & and ^ only accept integral operands.

The following table shows the result type of expressions with unary operators.

Operator integer addr
~ integer *
! integer *
- integer *
+ integer integer

Table 4-3: Expression type, unary operators

The following table shows the result type of expressions with binary numerical operators.

Operator integer, integer addr, integer integer, addr addr,addr
- integer addr * integer
+ integer addr addr *
* integer * * *
/ integer * * *
% integer * * *

Table 4-4: Expression type, binary numerical operators

a string operand will be converted to an integral number

The following table shows the result type of functions. A '-' in the column Operands means that the function has no operands.

Function Operands Result
@ABS() integer integer
@ARG() symbol
integer
integer
integer
@ASXA() - string
@CAT() string,string string
@CNT() - integer
@DEF() symbol integer
@LEN() string integer
@LST() - integer
@MAC() symbol integer
@MAX() integer,integer,... integer
@MIN() integer,integer,... integer
@MXP() - integer
@POS() string,string
string,string,integer
integer
integer
@SCP() string,string integer
@SGN() integer integer
@SUB() string,integer,integer string

Table 4-5: Expression type, functions

4.3 Operators

There are two types of operators:

- unary operators

- binary operators

Operators can be arithmetic operators, shift operators, relational operators, bitwise operators, logical operators or special operators. All operators are described in the following sections.

If the grouping of the operators is not specified with parentheses, the operator precedence is used to determine evaluation order. Every operator has a precedence level associated with it. The following table lists the operators and their order of precedence (in descending order).

Operators Type
SEG, SOF unary
LOW, HIGH, LOWRD, HIWRD unary
+, -, ~, !, NOT unary
*, /, %, MOD binary
+, - binary
<<, >>, SHL, SHR binary
<, <=, >, >=, LT, LE, GT, GE unary
==, !=, =, EQ, <>, NE binary
&, AND binary
^, XOR binary
|, OR binary
&& binary
|| binary
. binary

Table 4-6: Operators Precedence List

Except for the unary operators, the assembler evaluates expressions with operators of the same precedence level left-to-right. The unary operators are evaluated right-to-left . So, -4 + 3 * 2 evaluates to (-4) + (3 * 2). Note that you can also use the '.' operator in expressions (for bit selection in a byte)!

4.3.1 Addition and Subtraction

Synopsis:

Addition: operand + operand

Subtraction: operand - operand

The + operator adds its two operands and the - operator subtracts them. The operands can be any expression evaluating to an absolute number or a relocatable operand, with the restrictions of Table 4-4 .

Examples:

0a342h  + 23h          ; addition of absolute numbers
0ff1ah  - AVAR         ; subtraction with the value of
                       ; symbol AVAR

4.3.2 Sign Operators

Synopsis:

Plus: +operand
Minus: -operand

The + operator does not modify its operand. The - operator subtracts its operand from zero.

Example:

5  +  -3  ; result is 2

4.3.3 Multiplication and Division

Synopsis:

Multiplication: operand * operand
Division: operand / operand
Modulo: operand % operand
operand
MOD operand

The * operator multiplies its two operands, the / operator performs an integer division, discarding any remainder. The % or MOD operator also performs an integer division, but discards the quotient and returns the remainder. The operands can be any expression evaluating to an absolute number or a relocatable operand, with the restrictions of Table 4-4. Note that the right operands of the / and % operator may not be zero.

Examples:

AVAR*2            ; multiplication
0FF3CH/COUNT      ; division
23%4              ; modulo, result is 3

4.3.4 Shift Operators

Synopsis:

Shift left: operand << count
operand SHL count
Shift right: operand >> count
operand SHR count

These operators shift their left operand (operand) either left (<< or SHL) or right (>> or SHR) by the number of bits (absolute number) specified with the right operand (count). The operands can be any expression evaluating to an absolute number or a relocatable operand.

Examples:

AVAR shr  COUNT  ; shift right variable AVAR,
                 ; COUNT times

4.3.5 Relational Operators

Synopsis:

Equal: operand EQ operand
operand
== operand
operand
= operand
Not equal: operand NE operand
operand
!= operand
operand
<> operand
Less than: operand LT operand
operand
< operand
Less than or equal: operand LE operand
operand
<= operand
Greater than: operand GT operand
operand
> operand
Greater than or equal: operand GE operand
operand
>= operand

These operators compare their operands and return an absolute number (a bit) of 0ffffH for 'true' and 0 for 'false'. The operands can be any expression evaluating to an absolute number or a relocatable operand.

Examples:

3 GE 4            ; result is 0 (false)
4 EQ COUNT        ; 0ffffH (true), if COUNT is 4.
                  ; 0 otherwise.
9 LT 0Ah          ; result is 0ffffH (true)

4.3.6 Bitwise Operators

Synopsis:

Bitwise AND: operand & operand
operand AND operand
Bitwise OR: operand | operand
operand OR operand
Bitwise XOR: operand ^ operand
operand XOR operand
One's complement ~ operand
NOT operand

The AND, OR and XOR operators take the bitwise AND, OR respectively XOR of the left and right operand. The one's complement (bitwise NOT) operator performs a bitwise complement on its operand. The operands can be any expression evaluating to an (integer) number.

Examples:

0BH&3 ; result is 3
      ;     1011B
      ;     0011B  &
      ;     0011B

~0AH  ; result is 0FFFFFFF5H
      ;     not 00000000 00000000 0000000000001010B
      ;      =  11111111 11111111 1111111111110101B

4.3.7 Logical Operators

Synopsis:

Logical AND: operand && operand
Logical OR: operand || operand
Logical NOT: ! operand

The logical AND operator returns an integer 1 if both operands are non-zero; otherwise it returns an integer 0. The logical OR operator returns an integer 1 if either of its operands is non-zero; otherwise it returns an integer 0. The ! operator performs a logical not on its operand. ! returns an integer 1 (`true) if the operand is 0; otherwise, ! returns 0 (`false'). The operands can be can be any expression evaluating to an integer.

Examples:

0BH&&3      ; result is 1 (true)

!0AH        ; result is 0 (false)
!(4<3)      ; result is 1 (true)
            ; 4 < 3 result is 0 (false)

4.3.8 Selection Operators

Synopsis:

Select high byte: HIGH operand
Select low byte: LOW operand

Select high word: HIWRD operand
Select low word: LOWRD operand

Select segment number: SEG operand
Select segment offset: SOF operand

Dot operator: bitbyte.bitpos

LOW selects the least significant byte of its operand, HIGH selects the most significant byte.

HIWRD and LOWRD are XA operators for 16-bit extracts from 32-bit (or 24-bit) expressions.

The SEG operator takes a 24-bit address as argument and returns the upper 8-bits, i.e., the segment number. The SOF operator takes a 24-bit address as argument and returns the lower 16-bits, i.e., the offset within a segment.

The . (dot) operator singles out the bit number specified by the bitpos from the bitbyte. The result is an address in the BIT addressable memory space.

In the XA bit addressing mode bitbyte can have the following absolute values:

bitpos can have the following values:

Examples:

DB  HIGH  1234H ; stores 12H
DB  LOW   1234H ; stores 34H

MOV R4, #HIGH (_label+3)
MOV R5, #LOW  (_label+3)

MOV R4D, #HIWRD (_label+10)
MOV R6D, #LOWRD (_label+10)
MOV R6,  [R0D]
CLR  064H.1     ; CLR bit   (bit address)
CLR  420H.2     ; CLR bit   (SFR bit address)

4.4 Section Type of Expressions

The section type of an expression involving more than one operand is assigned according to the following rules:

1. The section type of a unary operation (+, -, NOT, LOW, HIGH, LOWRD, HIWRD) will be the same as the section type of the operand.

2. The section type of a binary + or - operation is NUMBER, unless one of the operands has type NUMBER, in which case the section type will be the type of the other operand.

3. The section type of the binary operations except + and - will be NUMBER.

4.5 Functions

The assembler has several built-in functions to support data conversion, string comparison, and math computations. Functions can be used as terms in any arbitrary expression. Functions have the following syntax:

Functions start with the '@' sign and have zero or more arguments, and are always followed by opening and closing parentheses. There must be no intervening spaces between the function name and the opening parenthesis and between the (comma-separated) arguments.

Assembler functions can be grouped into four types:

1. Mathematical functions

2. String functions

3. Macro functions

4. Assembler mode functions

4.5.1 Mathematical Functions

The mathematical functions comprise min/max functions, among others:

ABS - Absolute value

MAX - Maximum value

MIN - Minimum value

SGN - Return sign

4.5.2 String Functions

String functions compare strings, return the length of a string, and return the position of a substring within a string:

CAT - Catenate strings

LEN - Length of string

POS - Position of substring in string

SCP - Compare strings

SUB - Substring from a string

4.5.3 Macro Functions

Macro functions return information about macros:

ARG - Macro argument function

CNT - Macro argument count

MAC - Macro definition function

MXP - Macro expansion function

4.5.4 Assembler Mode Functions

Miscellaneous functions having to do with assembler operation:

ASXA - Assembler executable name

DEF - Symbol definition function

LST - LIST control flag value

4.5.5 Detailed Description

Individual descriptions of each of the assembler functions follow. They include usage guidelines, functional descriptions, and examples.

@ABS(expression)

Returns the absolute value of expression as an integer value.

Example:

@ARG(symbol | expression)

Returns integer 1 if the macro argument represented by symbol or expression is present, 0 otherwise. If the argument is a symbol it must be single-quoted and refer to a dummy argument name. If the argument is an expression it refers to the ordinal position of the argument in the macro dummy argument list. A warning will be issued if this function is used when no macro expansion is active.

Example:

@ASXA()

Returns the name of the assembler executable. This is asxa for the XA family.

Example:

@CAT(str1,str2)

Concatenates the two strings into one string. The two strings must be enclosed with single or double quotes.

Example:

@CNT()

Returns the count of the current macro expansion arguments as an integer. A warning will be issued if this function is used when no macro expansion is active.

Example:

@DEF(symbol)

Returns an integer 1 if symbol has been defined, 0 otherwise. symbol may be any label not associated with a MACRO directive. If symbol is quoted it is looked up as a DEFINE symbol; if it is not quoted it is looked up as an ordinary label.

Example:

@LEN(string)

Returns the length of string as an integer.

Example:

@LST()

Returns the value of the LIST control flag as an integer. Whenever a LIST control is encountered in the assembler source, the flag is incremented; when a NOLIST control is encountered, the flag is decremented.

Example:

@MAC(symbol)

Returns an integer 1 if symbol has been defined as a macro name, 0 otherwise.

Example:

@MAX(expr1[,exprN]...)

Returns the greatest of expr1,...,exprN as an integer.

Example:

@MIN(expr1[,exprN]...)

Returns the least of expr1,...,exprN as an integer.

Example:

@MXP()

Returns an integer 1 if the assembler is expanding a macro, 0 otherwise.

Example:

@POS(str1,str2[,start])

Returns the position str2 in str1 as an integer, starting at position start. If start is not given the search begins at the beginning of str1. If the start argument is specified it must be a positive integer and cannot exceed the length of the source string.

Example:

@SCP(str1,str2)

Returns an integer 1 if the two strings compare, 0 otherwise. The two strings must be separated by a comma.

Example:

@SGN(expression)

Returns the sign of expression as an integer: -1 if the argument is negative, 0 if zero, 1 if positive. The expression may be relative or absolute.

Example:

@SUB(str,expr1, expr2)

Returns the substring from str as a string. expr1 is the starting position within str and expr2 is the length of the desired string. The assembler issues an error if either expr1 or expr2 exceeds the length of str.

Example:

4.6 Predefined Symbols

Built into the assembler are a number of symbol definitions for various XA addresses. These symbols are treated by the assembler as though they were defined with the BIT and DATA assembler directives. The predefined symbols are listed below.

BIT Addresses (XA-G3):

Symbol BIT Address Symbol BIT Address
A1 BIT 389H R3SEG BIT 21BH
A2 BIT 38AH R4SEG BIT 21CH
A3 BIT 38BH R5SEG BIT 21DH
AC BIT 206H R6SEG BIT 21EH
AD0 BIT 380H RB8_0 BIT 302H
AD1 BIT 381H RB8_1 BIT 322H
AD2 BIT 382H RCLK0 BIT 2C5H
AD3 BIT 383H RCLK1 BIT 2CDH
AD4 BIT 384H RD BIT 39FH
AD5 BIT 385H REN_0 BIT 304H
AD6 BIT 386H REN_1 BIT 324H
AD7 BIT 387H RI_0 BIT 300H
BR0 BIT 30AH RI_1 BIT 320H
BR1 BIT 32AH RS0 BIT 20CH
C BIT 207H RS1 BIT 20DH
C/T2 BIT 2C1H RxD0 BIT 398H
CP/RL2 BIT 2C0H RxD1 BIT 38DH
DCEN BIT 2C8H SM BIT 20FH
EA BIT 337H SM0_0 BIT 307H
ERI0 BIT 338H SM0_1 BIT 327H
ERI1 BIT 33AH SM1_0 BIT 306H
ESWEN BIT 21FH SM1_1 BIT 326H
ET0 BIT 331H SM2_0 BIT 305H
ET1 BIT 333H SM2_1 BIT 325H
ET2 BIT 334H STINT0 BIT 308H
ETI0 BIT 339H STINT1 BIT 328H
ETI1 BIT 33BH SWR1 BIT 350H
EX0 BIT 330H SWR2 BIT 351H
EX1 BIT 332H SWR3 BIT 352H
EXEN2 BIT 2C3H SWR4 BIT 353H
EXF2 BIT 2C6H SWR5 BIT 354H
FE0 BIT 30BH SWR6 BIT 355H
FE1 BIT 32BH SWR7 BIT 356H
IDL BIT 220H T0 BIT 39CH
IE0 BIT 281H T0OE BIT 288H
IE1 BIT 283H T1 BIT 39DH
IM0 BIT 208H T1OE BIT 28AH
IM1 BIT 209H T2 BIT 38EH
IM2 BIT 20AH T2EX BIT 38FH
IM3 BIT 20BH T2OE BIT 2C9H
INT0 BIT 39AH TB8_0 BIT 303H
INT1 BIT 39BH TB8_1 BIT 323H
IT0 BIT 280H TCLK0 BIT 2C4H
IT1 BIT 282H TCLK1 BIT 2CCH
N BIT 201H TF0 BIT 285H
OE0 BIT 309H TF1 BIT 287H
OE1 BIT 329H TF2 BIT 2C7H
P2.0 BIT 390H TI_0 BIT 301H
P2.1 BIT 391H TI_1 BIT 321H
P2.2 BIT 392H TM BIT 20EH
P2.3 BIT 393H TR0 BIT 284H
P2.4 BIT 394H TR1 BIT 286H
P2.5 BIT 395H TR2 BIT 2C2H
P2.6 BIT 396H TxD0 BIT 399H
P2.7 BIT 397H TxD1 BIT 38EH
PD BIT 221H V BIT 202H
PRE0 BIT 2FDH WDRUN BIT 2FAH
PRE1 BIT 2FEH WDTOF BIT 2F9H
PRE2 BIT 2FFH WR BIT 39EH
R0SEG BIT 218H WRH BIT 338H
R1SEG BIT 219H Z BIT 200H
R2SEG BIT 21AH

DATA Addresses (XA-G3):

Symbol DATA Address Symbol DATA Address
BCR DATA 46AH RTL1 DATA 457H
BTRH DATA 469H S0CON* DATA 420H
BTRL DATA 468H S0STAT* DATA 421H
CS DATA 443H S0BUF* DATA 460H
DS DATA 441H S0ADDR DATA 461H
ES DATA 442H S0ADEN DATA 462H
IEH* DATA 427H S1CON* DATA 424H
IEL* DATA 426H S1STAT* DATA 425H
IPA0 DATA 4A0H S1BUF DATA 464H
IPA1 DATA 4A1H S1ADDR DATA 465H
IPA2 DATA 4A2H S1ADEN DATA 466H
IPA4 DATA 4A4H SCR DATA 440H
IPA5 DATA 4A5H SSEL* DATA 403H
P0* DATA 430H SWE DATA 47AH
P1* DATA 431H SWR* DATA 42AH
P2* DATA 432H T2CON* DATA 418H
P3* DATA 433H T2MOD* DATA 419H
P0CFGA DATA 470H TH2 DATA 459H
P1CFGA DATA 471H TL2 DATA 458H
P2CFGA DATA 472H T2CAPH DATA 45BH
P3CFGA DATA 473H T2CAPL DATA 45AH
P0CFGB DATA 4F0H TCON* DATA 410H
P1CFGB DATA 4F1H TH0 DATA 451H
P2CFGB DATA 4F2H TH1 DATA 453H
P3CFGB DATA 4F3H TL0 DATA 450H
PCON* DATA 404H TL1 DATA 452H
PSWH* DATA 401H TMOD DATA 45CH
PSWL* DATA 400H TSTAT* DATA 411H
PSW51* DATA 402H WDCON DATA 41FH
RTH0 DATA 455H WDL DATA 45FH
RTH1 DATA 457H WFEED1 DATA 45DH
RTL0 DATA 454H WFEED2 DATA 45EH

* SFRs are bit addressable

Table 4-7: DATA Addresses (XA-G3)


Copyright © 2000 TASKING, Inc.