Beruflich Dokumente
Kultur Dokumente
TOC
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
1
2
3
4
5
6
7
Introduction
It was time that hardware-oriented to the core made compromise if they wanted to stay in the game.
Namely, unlike other circuits which only need to be connected to other components and powered in order
to be of any use, microcontrollers require to be programmed as well. Fortunately, they still didn't progress
so far in their evolution, so that all microcontroller families understand only one language - machine
language. That's a good thing. The bad one is that, even primitive, this language of zeros and ones can
only be understood by microcontrollers and some of the experts working on its development. In order to
bridge this gap between machine and humans, the first high-level programming language called
Assembly language was created.
The main problem of remembering codes recognized as instructions by electronics was solved therefore,
but another one, equally complicated to both us and them(microcontrollers) arose. This problem was
also easily solved by means of the program for a PC called assembler and a simple device called
programmer.
This program enables the PC to receive commands in the form of abbreviations and convert them
unerringly into so called executable file. The moment of compiling a program into machine language is
crucial as this file, called HEX file, represents a series of binary numbers understandable to
microcontrollers only. The program written in assembly language cannot be executed practically unless
this file is loaded into the microcontroller memory. This is the moment when the last link in the chain - the
programmer - appears on the scene. It is a small device connected to a PC via some of the ports and has
a socket for placing chip in.
Labels;
Orders;
Directives; and
Comments.
directive;
Text following the mark ; in a program line represents a comment ignored (not
least one space character. For the sake of better clearness, a push button TAB on a
keyboard is commonly used instead of it, so that it is easy to delimit columns with
labels, directives etc. in a program.
Numbers
If octal number system, otherwise considered as obsolite, is disregarded, assembly laguage allows
numbers to be used in one out of three number systems:
Decimal Numbers
If not stated otherwise, the assembly language considers all the numbers as decimal. All ten digits are
used (0,1,2,3,4,5,6,7,8,9). Since at most 2 bytes are used for saving them in the microcontroller, the
largest decimal number that can be written in assembly language is 65535. If it is necessary to specify
that some of the numbers is in decimal format, then it has to be followed by the letter D. For example
1234D.
Hexadecimal Numbers
Hexadecimal numbers are commonly used in programming. There are 16 digits in hexadecimal number
system (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F). The largest hexadecimal number that can be written in
assembly language is FFFF. It corresponds to decimal number 65535. In order to distinguish hexadecimal
numbers from decimal, they are followed by the letter h(either in upper- or lowercase). For example 54h.
Binary Numbers
Binary numbers are often used when the value of each individual bit of some of the registers is important,
since each binary digit represents one bit. There are only two digits in use (0 and 1). The largest binary
number written in assembly language is 1111111111111111. In order to distinguish binary numbers from
other numbers, they are followed by the letter b (either in upper- or lowercase). For example
01100101B.
Operators
Some of the assembly-used commands use logical and mathematical expessions instead of symbols
having specific values. For example:
IF (VERSION>1)
LCALL Table_2
USING VERSION+1
ENDIF
...
NAME
O P E R ATI O N
EXAMPLE
RESULT
Addition
10+5
15
Subtraction
25-17
Multiplication
7*4
28
7/4
MOD
Remainder of division
7 MOD 4
SHR
1000B SHR 2
0010B
SHL
1010B SHL 2
101000B
NOT
NOT 1
1111111111111110B
AND
Logical AND
0101B
OR
Logical OR
1101B OR 0101B
1101B
XOR
Exclusive OR
1000B
LOW
LOW(0AADDH)
0DDH
HIGH
HIGH(0AADDH)
0AAH
EQ, =
Equal
7 EQ 4 or 7=4
0 (false)
NE,<>
Not equal
7 NE 4 or 7<>4
0FFFFH (true)
GT, >
Greater than
7 GT 4 or 7>4
0FFFFH (true)
GE, >=
Greater or equal
7 GE 4 or 7>=4
0FFFFH (true)
LT, <
Less than
7 LT 4 or 7<4
0 (false)
LE,<=
Less or equal
7 LE 4 or 7<=4
0 (false)
As seen, the assembly language is capable of computing some values and including them in a program
code, thus using the following mathematical and logical operations:
Symbols
Every register, constant, address or subroutine can be assigned a specific symbol in assembly language,
which considerably facilitates the process of writing a program. For example, if the P0.3 input pin is
connected to a push button used to stop some process manually (push button STOP), the process of
writing a program will be much simpler if the P0.3 bit is assigned the same name as the push button, i.e.
pushbutton_STOP. Of course, like in any other language, there are specific rules to be observed as
well:
For the purpose of writing symbols in assembly language, all letters from alphabet (A-Z,
a-z), decimal numbers (0-9) and two special characters ("?" and "_") can be used.
Assembly language is not case sensitive.
For example, the following symbols will be considered identical:
Serial_Port_Buffer
SERIAL_PORT_BUFFER
In order to distinguish symbols from constants (numbers), every symbol starts with a
account. In the following example, the first two symbols will be considered duplicate
(error), while the third and forth symbols will be considered different:
START_ADDRESS_OF_TABLE_AND_CONSTANTS_1
START_ADDRESS_OF_TABLE_AND_CONSTANTS_2
TABLE_OF_CONSTANTS_1_START_ADDRESS
TABLE_OF_CONSTANTC_2_START_ADDRESS
Some of the symbols cannot be used when writing a program in assembly language
because they are already part of instructions or assembly directives. Thus, for example,
AB
ACALL
ADD
ADDC
AJMP
AND
ANL
AR0
AR1
AR2
AR3
AR4
AR5
AR6
AR7
BIT
BSEG
CALL
CJNE
CLR
CODE
CPL
CSEG
DA
DATA
DB
DBIT
DEC
DIV
DJNZ
DPTR
DS
DSEG
DW
END
EQ
EQU
GE
GT
HIGH
IDATA
INC
ISEG
JB
JBC
JC
JMP
JNB
JNC
JNZ
JZ
LCALL
LE
LJMP
LOW
LT
MOD
MOV
MOVC
MOVX
MUL
NE
NOP
NOT
OR
ORG
ORL
PC
POP
PUSH
R0
R1
R2
R3
R4
R5
R6
R7
RET
RETI
RL
RLC
RR
RRC
SET
SETB
SHL
SHR
SJMP
SUBB
SWAP
USING
XCH
XCHD
XDATA
XOR
XRL
XSEG
Labels
A label is a special type of symbols used to represent a textual version of an address in ROM or RAM
memory. They are always placed at the beginning of a program line. It is very complicated to call a
subroutine or execute some of the jump or branch instructions without them. They are easily used:
A symbol (label) with some easily recognizable name should be written at the beginning
of a program line from which a subroutine starts or where jump should be executed.
It is sufficient to enter the name of label instead of address in the form of 16-bit number
Directives
Unlike instructions being compiled and written to chip program memory, directives are commands of
assembly language itself and have no influence on the operation of the microcontroller. Some of them are
obligatory part of every program while some are used only to facilitate or speed up the operation.
Directives are written in the column reserved for instructions. There is a rule allowing only one directive
per program line.
EQU directive
The EQU directive is used to replace a number by a symbol. For example:
MAXIMUM EQU 99
After using this directive, every appearance of the label MAXIMUM in the program will be interpreted by
the assembler as the number 99 (MAXIMUM = 99). Symbols may be defined this way only once in the
program. The EQU directive is mostly used at the beginning of the program therefore.
SET directive
The SET directive is also used to replace a number by a symbol. The significant difference compared to
the EQU directive is that the SET directive can be used an unlimited number of times:
SPEED SET 45
SPEED SET 46
SPEED SET 57
BIT directive
The BIT directive is used to replace a bit address by a symbol. The bit address must be in the range of 0
to 255. For example:
TRANSMIT BIT PSW.7 ;Transmit bit (the seventh bit in PSW register)
;is assigned the name "TRANSMIT"
OUTPUT BIT 6 ;Bit at address 06 is assigned the name "OUTPUT"
RELAY BIT 81 ;Bit at address 81 (Port 0)is assigned the name ;"RELAY"
CODE directive
The CODE directive is used to assign a symbol to a program memory address. Since the maximum
capacity of program memory is 64K, the address must be in the range of 0 to 65535. For example:
DATA directive
The DATA directive is used to assign a symbol to an address within internal RAM. The address must be in
the range of 0 to 255. It is possible to change or assign a new name to any register. For example:
IDATA directive
The IDATA directive is used to change or assign a new name to an indirectly addressed register. For
example:
XDATA directive
The XDATA directive is used to assign a name to registers within external (additional) RAM memory. The
addresses of these registers cannot be larger than 65535. For example:
ORG directive
The ORG directive is used to specify a location in program memory where the program following directive
is to be placed. For example:
...
...
This program starts at location 100. The table containing data is to be stored at location 1024 (1000h).
USING directive
The USING directive is used to define which register bank (registers R0-R7) is to be used in the program.
END directive
The END directive is used at the end of every program. The assembler will stop compiling once the
program encounters this directive. For example:
...
END ;End of program
DSEG
A dollar symbol "$" denotes current value of address counter in the currently active segment. The
following two examples illustrate how this value can be used practically:
Example 1:
Example 2:
These two program lines can be used for computing exact number of characters in the message ALARM
turn off engine which is defined at the address assigned the name MESSAGE.
DS directive
The DS directive is used to reserve memory space expressed in bytes. It is used if some of the following
segments ISEG, DSEG or XSEG is currently active. For example:
Example 1:
DSEG
DS 32
Example 2:
ORG 100
DS 8
DBIT directive
The DBIT directive is used to reserve space within bit-addressable part of RAM. The memory size is
expressed in bits. It can be used only if the BSEG segment is active. For example:
DB directive
The DB directive is used for writing specified value into program memory. If several values are specified,
then they are separated by a comma. If ASCII array is specified, it should be enclosed within single
quotation marks. This directive can be used only if the CSEG segment is active. For example:
CSEG
DB 22,33,Alarm,44
If this directive is preceeded by a lable, then the label will point to the first element of the array. It is the
number 22 in this example.
DW directive
The DW directive is similar to the DB directive. It is used for writing a two-byte value into program
memory. The higher byte is written first, then the lower one.
ignored, i.e. not compiled, and the program continues with instructions following
directives ELSE or ENDIF.
Example 1:
IF (VERSION>3)
LCALL Table_2
LCALL Addition
ENDIF
...
If the program is of later date than version 3 (statement is correct), subroutines Table 2 and Addition
will be executed. If the statement in parentheses is not correct (VERSION<3), two instructions calling
subroutines will not be compiled.
Example 2:
If the value of the symbol called Model is equal to one, the first two instructions following directive IF will
be compiled and the program continues with instructions following directive ENDIF (all instructions
between ELSE and ENDIF are ignored). Otherwise, if Model=0, instructions between IF and ELSE are
ignored and the assembler compiles only instructions following directive ELSE.
IF (Model)
MOV R0,#BUFFER
MOV A,@R0
ELSE
MOV R0,#EXT_BUFFER
MOVX A,@R0
ENDIF
...
Control directives
Control directives start with a dollar symbol $. They are used to determine which files are to be used by
the assembler during compilation, where the executable file is to be stored as well as the final layout of
the compiled program called Listing. There are many control directives, but only few of them is of
importance:
\$INCLUDE directive
This directive enables the assembler to use data stored in other files during compilation. For example:
\$INCLUDE(TABLE.ASM)
\$MOD8253 directive
This $MOD8253 directive is a file containing names and addresses of all SFRs of 8253 microcontrollers.
By means of this file and directive having the same name, the assembler can compile the program on the
basis of register names. If they are not used, it is necessary to specify name and address of every SFRs
to be used at the beginning of the program.
previous chapter | table of contents | next chapter
The Easy8051 v6 is compatible with 14-, 16-, 20-, 28-, 40-pin PLCC44 and PLCC32 MCUs. It comes with an AT89S8253.
The board has a USB 2.0 programmer and many peripherals such as COG, port expander, MENU and 4x4 keypads etc.
[more info]
mikroProg for 8051 is supported with mikroC, mikroBasic and mikroPascal compilers for 8051. You