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

84

Chapter 3. Writing Assembly Programs

 

 

Expressions and Operators

An operand may be a numeric constant, a symbolic name, a character string or an expression.

Operators are used to combine and compare operands within your assembly program. Operators are not assembly language instructions nor do they generate x51 assembly code. They represent operations that are evaluated at assembly-time. Therefore, operators can only handle calculations of values that are known when the program is assembled.

An expression is a combination of numbers, character string, symbols, and 3 operators that evaluate to a single 32-bit binary number (for A51: 16-bit binary

number). Expressions are evaluated at assembly time and can, therefore, be used to calculate values that would otherwise be difficult to determine beforehand.

The following sections describe operators and expressions and how they are used in x51 assembly programs.

Numbers

Numbers can be specified in hexadecimal (base 16), decimal (base 10), octal (base 8), and binary (base 2). The base of a number is specified by the last character in the number. A number that is specified without an explicit base is interpreted as decimal number.

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

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

85

 

 

The following table lists the base types, the base suffix character, and some examples:

 

Base

Suffix

Legal Characters

Examples

 

 

 

Hexadecimal

H, h

0 – 9, A – F, a – f

0x1234 0x99 1234H 0A0F0h 0FFh

 

 

 

Decimal

D, d

0 – 9

1234 65590d 20d 123

 

 

 

Octal

O, o, Q, q

0 – 7

177o 25q 123o 177777q

 

 

 

Binary

B, b

0 and 1

10011111b 101010101b

 

 

 

The first character of a number must be a digit between 0 and 9. Hexadecimal

 

 

numbers which do not have a digit as the first character should be prefixed with

 

 

3

 

a 0. The Ax51 assembler supports also hex numbers written in C notation.

 

The dollar sign character ($) can be used in a number to make it more readable,

 

however, the dollar sign character cannot be the first or last character in the

 

 

 

 

number. A dollar sign used within a number is ignored by the assembler and has

 

 

no impact on the value of the number. For example:

 

 

 

 

 

 

 

1111$0000$1010$0011b

is equivalent to

1111000010100011B

 

 

 

1$2$3$4

 

is equivalent to

1234

 

 

Colon Notation for Numbers (A251 only)

The A251 assembler supports the notation page:number for specifying absolute addresses. Numbers specified with this notation receive the memory type EDATA when page is 0 or ECODE for all other pages. In this way, you can use such numbers for referencing any memory location. For example:

ABSVAL1

EQU

0:20H

; symbol to address location 20H

ABSVAL2

EQU

0:80H

; symbol to address location 80H in EDATA space

PORT0

EQU

S:80H

; symbol to SFR space 80H

ENTRY

EQU

10:2000H

; entry point at location 102000H

 

MOV

WR0,ABSVAL1

 

 

MOV

R1,ABSVAL2

 

 

MOV

PORT0,R1

 

 

EJMP

ENTRY

 

 

MOV

WR0,0:20H

; access to ABSVAL1

 

MOV

R1,0:80H

; access to ABSVAL2

 

MOV

S:80H,R1

 

 

EJMP

10:2000H

 

The colon notation is accepted in several A251 controls and is converted as described.

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

 

86

 

 

Chapter 3. Writing Assembly Programs

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Number in Colon Notation

Replaced with

 

 

 

 

VAL1 EQU 0:20H

VAL1 EQU EDATA 20H

 

 

 

 

VAL2 EQU 0FF:1000H

VAL2 EQU ECODE 0FF1000H

 

 

 

 

ORG 0FE:2000H

?modulename?number SEGMENT ECODE AT 0FE2000H

 

 

 

 

 

RSEG ?modulename?number

 

 

 

 

ORG 0:400H

?modulename?number SEGMENT EDATA AT 400H

 

 

 

 

 

RSEG ?modulename?number

 

 

 

 

CSEG AT 0FE:2000H

?modulename?number SEGMENT ECODE AT 0FE2000H

 

 

 

 

 

RSEG ?modulename?number

 

 

 

 

BVAR1 BIT 0:20H.1

BVAR1 BIT 20H.1

 

 

 

 

BVAR1 BIT 0:30H.1

BVAR1 EQU EBIT 30H.1

 

3

 

 

PUSH.B #13

PUSH BYTE #13

 

 

 

PUSH.W #13

PUSH WORD #13

 

 

 

 

 

 

 

 

NOTE

 

 

The colon notation is provided for source compatibility with other 251 macro assemblers. If you do not need to port your code to other assemblers, it is recommended to use directly the replacement sequence in your assembler source file.

Characters

The Ax51 assembler allows you to use ASCII characters in an expression to generate a numeric value. Up to two characters enclosed within single quotes (') may be included in an expression. More than two characters in single quotes in an expression will cause the Ax51 assembler to generate an error. Following are examples of character expressions:

'A'

evaluates to 0041h

'AB'

evaluates to 4142h

'a'

evaluates to 0061h

'ab'

evaluates to 6162h

''

null string evaluates to 0000h

'abc'

generates an ERROR

Characters may be used anywhere in your program as a immediate data operand. For example:

LETTER_A

EQU

'A'

TEST:

MOV

@R0, #'F'

 

SUBB

A, #'0'

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

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

87

 

 

Character Strings

Character strings can be used in combination with the DB directive to define messages that are used in your x51 assembly program. Character strings must be enclosed within single quotes ('). For example:

KEYMSG:

DB

'Press any key to continue.'

generates the hexadecimal data (50h, 72h, 65h, 73h, 73h, 20h, … 6Eh, 75h, 65h, 2Eh) starting at KEYMSG. You can mix string and numeric data on the same line. For example:

 

 

 

 

3

EOLMSG:

DB

'End of line', 00h

 

appends the value 00h to the end of the string 'End of line'.

 

 

 

 

 

Two successive single quote characters can be used to insert a single quote into a string. For example:

MSGTXT:

DB

'ISN''T A QUOTE REQUIRED HERE?'.

Location Counter

The Ax51 assembler maintains a location counter for each segment. The location counter contains the offset of the instruction or data being assembled and is incremented after each line by the number of bytes of data or code in that line.

The location counter is initialized to 0 for each segment, but can be changed using the ORG directive.

The dollar sign character ($) returns the current value of the location counter. This operator allows you to use the location counter in an expression. For

example, the following code uses

$ to calculate the length of a message string.

 

 

 

MSG:

DB

'This is a message', 0

MSGLEN

EQU

$ – MSG

You can also use

$ in an instruction. For example, the following line of code

will repeat forever.

 

 

 

 

 

 

 

JMP

$

; repeat forever

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

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