C51
C51
ii
Information in this document is subject to change without notice and does not
represent a commitment on the part of the manufacturer. The software described
in this document is furnished under license agreement or nondisclosure
agreement and may be used or copied only in accordance with the terms of the
agreement. It is against the law to copy the software on any medium except as
specifically allowed in the license or nondisclosure agreement. The purchaser
may make one copy of the software for backup purposes. No part of this manual
may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording, or information storage and
retrieval systems, for any purpose other than for the purchasers personal use,
without written permission.
Copyright 1988-1997 Keil Elektronik GmbH., and Keil Software, Inc.
All rights reserved.
Every effort was made to ensure accuracy in this manual and to give appropriate
credit to persons, companies, and trademarks referenced herein.
Preface
This manual describes how to use the C51 Optimizing C Compiler to compile C
programs for your target 8051 environment. The C51 Compiler package can be
used on all 8051 family processors and is executable under MS-DOS. This
manual assumes that you are familiar with the MS-DOS operating system, know
how to program 8051 processors, and have a working knowledge of the C
language.
NOTE
MS-DOS and PC-DOS are, in essence, the same operating system. This manual
uses MS-DOS or just DOS when referring to either system.
If you have questions about programming in C, or if you would like more
information about the C programming language, refer to Books About the C
Language on page 2.
Many of the examples and descriptions in this manual discuss invoking the
compiler from the DOS command prompt. While this may not be applicable to
you if you are running C51 within an integrated development environment,
examples in this manual are universal in that they apply to all programming
environments.
iii
iv
Contents
Manual Organization
This users guide is divided into eight chapters and six appendices:
Chapter 1. Introduction, describes the C51 compiler.
Chapter 2. Compiling with C51, explains how to compile a source file using
the C51 cross compiler. This chapter describes the command-line directives that
control file processing, compiling, and output.
Chapter 3. Language Extensions, describes the C language extensions
required to support the 8051 system architecture. This chapter provides a
detailed list of commands, functions, and controls not found in ANSI C
compilers.
Chapter 4. Preprocessor, describes the components of the C51 preprocessor
and includes examples.
Chapter 5. 8051 Derivatives, describes the 8051 family derivatives that the
C51 compiler supports. This chapter also includes tips for improving target
performance.
Chapter 6. Advanced Programming Techniques, lists important information
for the experienced developer. This chapter includes customization file
descriptions, and optimizer and segment names. This chapter also discusses how
to interface C51 with other 8051 programming languages.
Chapter 7. Error Messages, lists the fatal errors, syntax errors, and warnings
that you may encounter while using C51.
Chapter 8. Library Reference, provides you with extensive C51 library routine
reference material. The library routines are listed by category and include file.
An alphabetical reference section, which includes example code for each of the
library routines, concludes the chapter.
The Appendix includes information on the differences between compiler
versions, writing code, and other items of interest.
Document Conventions
This document uses the following conventions:
Examples
Description
README.TXT
Bold capital text is used for the names of executable programs, data
files, source files, environment variables, and commands you enter at
the MS-DOS command prompt. This text usually represents commands
that you must type in literally. For example:
CLS
DIR
BL51.EXE
Note that you are not required to enter these commands using all capital
letters.
Language Elements
Courier
!=
main
long
>>
Variables
Omitted code
.
.
.
!Optional Items"
Keys
Text in this sans serif typeface represents actual keys on the keyboard.
For example, Press Enter to continue.
vi
Contents
Contents
Chapter 1. Introduction......................................................................................1
Books About the C Language ....................................................................................... 2
vii
viii
Contents
SMALL .................................................................................................................52
SRC .......................................................................................................................53
SYMBOLS ............................................................................................................54
WARNINGLEVEL ...............................................................................................55
ix
Contents
ASSERT.H.......................................................................................................... 192
CTYPE.H............................................................................................................ 192
INTRINS.H......................................................................................................... 192
MATH.H............................................................................................................. 192
SETJMP.H .......................................................................................................... 193
STDARG.H......................................................................................................... 193
STDDEF.H ......................................................................................................... 193
STDIO.H............................................................................................................. 193
STDLIB.H........................................................................................................... 194
STRING.H .......................................................................................................... 194
Reference .................................................................................................................. 195
abs ....................................................................................................................... 196
acos / acos517 ..................................................................................................... 197
asin / asin517....................................................................................................... 198
assert ................................................................................................................... 199
atan / atan517 ...................................................................................................... 200
atan2.................................................................................................................... 201
atof / atof517 ....................................................................................................... 202
atoi ...................................................................................................................... 203
atol ...................................................................................................................... 204
cabs ..................................................................................................................... 205
calloc................................................................................................................... 206
ceil....................................................................................................................... 207
_chkfloat_ ........................................................................................................... 208
cos / cos517......................................................................................................... 209
cosh ..................................................................................................................... 210
_crol_ .................................................................................................................. 211
_cror_ .................................................................................................................. 212
exp / exp517........................................................................................................ 213
fabs...................................................................................................................... 214
floor..................................................................................................................... 215
free ...................................................................................................................... 216
getchar................................................................................................................. 217
_getkey................................................................................................................ 218
gets ...................................................................................................................... 219
init_mempool ...................................................................................................... 220
_irol_................................................................................................................... 221
_iror_................................................................................................................... 222
isalnum................................................................................................................ 223
isalpha ................................................................................................................. 224
iscntrl .................................................................................................................. 225
isdigit .................................................................................................................. 226
isgraph................................................................................................................. 227
islower................................................................................................................. 228
isprint .................................................................................................................. 229
ispunct ................................................................................................................. 230
isspace................................................................................................................. 231
xi
xii
Contents
isupper .................................................................................................................232
isxdigit.................................................................................................................233
labs ......................................................................................................................234
log / log517 .........................................................................................................235
log10 / log10517 .................................................................................................236
longjmp ...............................................................................................................237
_lrol_ ...................................................................................................................239
_lror_...................................................................................................................240
malloc..................................................................................................................241
memccpy .............................................................................................................242
memchr................................................................................................................243
memcmp ..............................................................................................................244
memcpy ...............................................................................................................245
memmove ............................................................................................................246
memset ................................................................................................................247
modf ....................................................................................................................248
_nop_...................................................................................................................249
offsetof ................................................................................................................250
pow......................................................................................................................251
printf / printf517 ..................................................................................................252
putchar.................................................................................................................258
puts ......................................................................................................................259
rand......................................................................................................................260
realloc..................................................................................................................261
scanf ....................................................................................................................262
setjmp ..................................................................................................................266
sin / sin517 ..........................................................................................................267
sinh ......................................................................................................................268
sprintf / sprintf517 ...............................................................................................269
sqrt / sqrt517........................................................................................................271
srand ....................................................................................................................272
sscanf / sscanf517 ................................................................................................273
strcat ....................................................................................................................275
strchr....................................................................................................................276
strcmp ..................................................................................................................277
strcpy ...................................................................................................................278
strcspn .................................................................................................................279
strlen....................................................................................................................280
strncat ..................................................................................................................281
strncmp ................................................................................................................282
strncpy .................................................................................................................283
strpbrk .................................................................................................................284
strpos ...................................................................................................................285
strrchr ..................................................................................................................286
strrpbrk ................................................................................................................287
strrpos..................................................................................................................288
strspn ...................................................................................................................289
xiii
xiv
Contents
Glossary.............................................................................................................335
Index ..................................................................................................................343
xv
Chapter 1. Introduction
The C programming language is a general-purpose, programming language that
provides code efficiency, elements of structured programming, and a rich set of
operators. C is not a big language and is not designed for any one particular area
of application. Its generality, combined with its absence of restrictions, makes C
a convenient and effective programming solution for a wide variety of software
tasks. Many applications can be solved more easily and efficiently with C than
with other more specialized languages.
The C51 Optimizing C Compiler for the MS-DOS operating system is a
complete implementation of the American National Standards Institute (ANSI)
standard for the C language. C51 is not a universal C compiler adapted for the
8051 target. It is a ground-up implementation dedicated to generating extremely
fast and compact code for the 8051 microprocessor. C51 provides you the
flexibility of programming in C and the code efficiency and speed of assembly
language.
The C language on its own is not capable of performing operations (such as input
and output) that would normally require intervention from the operating system.
Instead, these capabilities are provided as part of the standard library. Because
these functions are separate from the language itself, C is especially suited for
producing code that is portable across a wide number of platforms.
Since C51 is a cross compiler, some aspects of the C programming language and
standard libraries are altered or enhanced to address the peculiarities of an
embedded target processor. Refer to Chapter 3. Language Extensions on page
57 for more detailed information.
Chapter 1. Introduction
Environment Settings
To run the compiler and the utilities, you must create new entries in the DOS
environment table. In addition, you must specify a PATH for the compiler
directory. The following table lists the environment variables, their default
paths, and a brief description.
Variable
Path
Description
PATH
\C51\BIN
TMP
C51INC
\C51\INC
C51LIB
\C51\LIB
Running C51
To invoke the C51 compiler, type C51 at the DOS prompt. On this command
line, you must include the name of the C source file to be compiled, as well as
any other necessary control directives required to compile your source file. The
format for the C51 command line is:
directives
The following command line example invokes C51, specifies the source file
SAMPLE.C, and uses the controls DEBUG, CODE, and PREPRINT.
C51 SAMPLE.C DEBUG CODE PREPRINT
The C51 compiler displays the following information upon successful invocation
and compilation.
MS-DOS C51 COMPILER V5.0
C51 COMPILATION COMPLETE.
0 WARNING(S),
0 ERROR(S)
DOS ERRORLEVEL
After compilation, the number of errors and warnings detected is output to the
screen. C51 then sets the DOS ERRORLEVEL to indicate the status of the
compilation. Values are listed in the following table:
ERRORLEVEL
Meaning
No errors or warnings
Warnings only
Fatal errors
You can access the ERRORLEVEL variable in DOS batch files. Refer to your
DOS users guide for more information on ERRORLEVEL or batch files.
Description
basename.LST
Files with this extension are listing files that contain the formatted source
text along with any errors detected by the compiler. Listing files may
optionally contain the used symbols and the generated assembly code.
See the PRINT directive in the following sections for more information.
basename.OBJ
Files with this extension are object modules that contain relocatable object
code. Object modules may be linked to an absolute object module by the
BL51 Linker/Locator.
basename.I
Files with this extension contain the source text as expanded by the
preprocessor. All macros are expanded and all comments are deleted in
this listing. See the PREPRINT directive in the following sections for more
information.
basename.SRC
Files with this extension are assembly source files generated from your C
source code. These files can be assembled with the A51 assembler. See
the SRC directive in the following sections for more information.
Control Directives
C51 offers a number of control directives that you can use to control the
operation of the compiler. Directives are composed of one or more letters or
digits and, unless otherwise specified, can be specified after the filename on the
command line or within a source file using the #pragma directive.
Example
C51
testfile.c
SYMBOLS
CODE
DEBUG
In the above examples, SYMBOLS, CODE, and DEBUG are all control directives.
testfile.c is the source file to be compiled.
NOTE
The syntax is the same for the command line and the #pragma directive.
Multiple options, however, may be specified on the #pragma line.
Typically, each control directive may be specified only once at the beginning of
a source file. If a directive is specified more than once, the compiler generates a
fatal error and aborts compilation. Directives that may be specified more than
once are so noted in the following sections.
Directive Categories
Control directives can be divided into three groups: source controls, object
controls, and listing controls.
!
Source controls define macros on the command line and determine the name
of the file to be compiled.
Object controls affect the form and content of the generated object module
(*.OBJ). These directives allow you to specify the optimizing level or
include debugging information in the object file.
The following table is an alphabetical list of the control directives. This list
shows each directives abbreviation, class, and description.
Directive and
(Abbreviation)
Class
Description
AREGS (AR),
NOAREGS (NOAR)
Object
ASM, ENDASM
Object
CODE (CD)
Listing
COMPACT (CP)
Object
COND (CO),
NOCOND (NOCO)
Listing
DEBUG (DB)
Object
DEFINE (DF)
Source
DISABLE
Object
EJECT (EJ)
Listing
FLOATFUZZY (FF)
Object
INTERVAL
Object
INTPROMOTE (IP),
NOINTPROMOTE (NOIP)
Object
INTVECTOR (IV),
NOINTVECTOR (NOIV)
Object
LARGE (LA)
Object
LISTINCLUDE (LC)
Listing
MAXARGS (MA)
Object
MOD517,
NOMOD517
Object
MODDP2,
NOMODDP2
Object
NOAMAKE (NOAM)
Object
NOEXTEND
Source
OBJECT (OJ),
NOOBJECT (NOOJ)
Object
OBJECTEXTEND (OE)
Object
OPTIMIZE (OT)
Object
ORDER (OR)
Object
PAGELENGTH (PL)
Listing
PAGEWIDTH (PW)
Listing
Directive and
(Abbreviation)
Class
Description
PREPRINT (PP)
Listing
PRINT (PR),
NOPRINT (NOPR)
Listing
Specify a name for the listing file or disable the listing file.
REGFILE (RF)
Object
REGISTERBANK (RB)
Object
REGPARMS,
NOREGPARMS
Object
ROM
Object
SAVE,
RESTORE
Object
SMALL (SM)
Object
SRC
Object
SYMBOLS (SB)
Listing
WARNINGLEVEL (WL)
Listing
These directives may be specified only once on the command line or at the beginning of a source
file using in the #pragma statement. They may not be used more than once in a source file.
Reference
The remainder of this chapter is devoted to describing each of the available C51
compiler control directives. The directives are listed in alphabetical order, and
each is divided into the following sections:
Abbreviation:
Arguments:
Default:
Description:
See Also:
Example:
10
AREGS / NOAREGS
Abbreviation:
None.
Arguments:
None.
Default:
AREGS
Description:
Example:
11
0000
0003
0004
0006
0009
000B
000C
000E
0000
0003
0005
0008
000A
000B
000D
source
extern char func ();
char k;
#pragma NOAREGS
noaregfunc () {
k = func () + func ();
}
#pragma AREGS
aregfunc () {
k = func () + func ();
}
12
ASM / ENDASM
Abbreviation:
None.
Arguments:
None.
Default:
None.
Description:
Example:
13
source
extern void test ();
main () {
test ();
#pragma asm
JMP
$ ; endless loop
#pragma endasm
}
14
CODE
Abbreviation:
CD
Arguments:
None.
Default:
Description:
Example:
C51 SAMPLE.C CD
#pragma code
0000
0002
0005
0006
0009
000A
000C
000E
15
COMPACT
Abbreviation:
CP
Arguments:
None.
Default:
SMALL
Description:
See Also:
Example:
16
COND / NOCOND
Abbreviation:
CO
Arguments:
None.
Default:
COND
Description:
Example:
17
9
10
11
12
1
1
1
1
source
extern unsigned char
unsigned char
a, b;
c;
main()
{
#if defined (VAX)
c = 13;
#elif defined (_ _TIME_ _)
b = 14;
a = 15;
#endif
}
.
.
.
source
extern unsigned char
unsigned char
main()
{
#if defined (VAX)
b = 14;
a = 15;
#endif
}
a, b;
c;
18
DEBUG
Abbreviation:
DB
Arguments:
None.
Default:
Description:
See Also:
OBJECTEXTEND
Example:
19
DEFINE
Abbreviation:
DF
Arguments:
Default:
None.
Description:
Example:
20
DISABLE
Abbreviation:
None.
Arguments:
None.
Default:
None.
Description:
Example:
21
source
typedef unsigned char
uchar;
#pragma disable
/* Disable Interrupts */
uchar dfunc (uchar p1, uchar p2) {
return (p1 * p2 + p2 * p1);
}
22
EJECT
Abbreviation:
EJ
Arguments:
None.
Default:
None.
Description:
Example:
#pragma eject
23
FLOATFUZZY
Abbreviation:
FF
Arguments:
Default:
FLOATFUZZY (3)
Description:
Example:
24
INTERVAL
Abbreviation:
None
Arguments:
Default:
INTERVAL (8)
Description:
offset
See Also:
INTVECTOR / NOINTVECTOR
Example:
25
INTPROMOTE / NOINTPROMOTE
Abbreviation:
IP / NOIP
Arguments:
None.
Default:
INTPROMOTE
Description:
Example:
source
char c;
unsigned char
int i;
1
1
1
1
1
c1,c2;
main () {
if (c == 0xff) c = 0;
if (c == -1) c = 1;
i = c + 5;
if (c1 < c2 +4) c1 = 0;
}
/* never true! */
/* works */
26
0000
0002
0003
0004
0006
0007
0008
0009
000A
000C
000E
000E
0010
0013
0016
0016
0018
0019
001A
001C
001D
001E
0020
0022
0023
0024
0026
0028
002A
002B
002C
002D
002E
002F
0031
0032
0033
0035
0036
0038
0039
003B
003C
003E
003E
0000
0002
0003
0004
0006
0007
0008
0009
000A
000C
000E
AF00
EF
33
95E0
FE
EF
F4
4E
7002
F500
000E E500
0010 B4FF03
0013 750001
0016
0016
0018
001A
001B
001C
001E
0020
E500
2405
FF
33
95E0
F500
8F00
; SOURCE LINE # 8
MOV A,c
ADD A,#05H
MOV R7,A
RLC A
SUBB A,ACC
MOV i,A
MOV i+01H,R7
0022
0024
0026
0027
0029
002A
002B
002D
002E
E500
2404
FF
E500
C3
9F
5003
E4
F500
; SOURCE LINE # 9
MOV A,c2
ADD A,#04H
MOV R7,A
MOV A,c1
CLR C
SUBB A,R7
JNC ?C0004
CLR A
MOV c1,A
0030
0030 22
; SOURCE LINE # 10
?C0004:
RET
; FUNCTION main (END)
27
INTVECTOR / NOINTVECTOR
Abbreviation:
IV / NOIV
Arguments:
Default:
INTVECTOR (0)
Description:
See Also:
interval
offset
INTERVAL
28
Example:
29
LARGE
Abbreviation:
LA
Arguments:
None.
Default:
SMALL
Description:
See Also:
Example:
30
LISTINCLUDE
Abbreviation:
LC
Arguments:
None.
Default:
NOLISTINCLUDE
Description:
Example:
31
MAXARGS
Abbreviation:
None.
Arguments:
Default:
MAXARGS(15)
MAXARGS(40)
Description:
Example:
#include <stdarg.h>
void func (char typ, ...)
va_list ptr;
char c;
int i;
/* a CHAR is passed */
break;
case 1:
i = va_arg (ptr, int);
/* an INT is passed */
break;
}
}
{
/* pass a char variable */
/* pass an int variable */
32
MOD517 / NOMOD517
Abbreviation:
None.
Arguments:
Default:
NOMOD517
Description:
Description
NOAU
NODP8
33
MODDP2
Example:
C51 SAMPL517.C
MOD517
NOMOD517
#pragma NOMOD517
34
MODDP2 / NOMODDP2
Abbreviation:
None.
Arguments:
MODDP2
Default:
NOMODDP2
Description:
See Also:
MOD517 / NOMOD517
Example:
C51 SAMPL517.C
MODDP2
#pragma moddp2
C51 SAMPL517.C
NOMODDP2
#pragma nomoddp2
35
NOAMAKE
Abbreviation:
NOAM
Arguments:
None.
Default:
Description:
Example:
C51 SAMPLE.C
#pragma NOAM
NOAMAKE
36
NOEXTEND
Abbreviation:
None.
Arguments:
None.
Default:
Description:
Example:
37
OBJECT / NOOBJECT
Abbreviation:
OJ / NOOJ
Arguments:
Default:
OBJECT (basename.OBJ)
Description:
Example:
38
OBJECTEXTEND
Abbreviation:
OE
Arguments:
None.
Default:
None.
Description:
See Also:
DEBUG
Example:
39
OPTIMIZE
Abbreviation:
OT
Arguments:
Default:
Description:
Description
Constant Folding: The compiler performs calculations that
reduce expressions to numeric constants, where possible.
This includes calculations of run-time addresses.
Simple Access Optimizing: The compiler optimizes access
of internal data and bit addresses in the 8051 system.
Jump Optimizing: The compiler always extends jumps to the
final target. Jumps to jumps are deleted.
40
Level
4
Description
Register Variables: Automatic variables and function
arguments are located in registers when possible.
Reservation of data memory for these variables is omitted.
Extended Access Optimizing: Variables from the IDATA,
XDATA, PDATA and CODE areas are directly included in
operations. The use of intermediate registers is not necessary
most of the time.
Example:
41
42
ORDER
Abbreviation:
OR
Arguments:
None.
Default:
Description:
Example:
43
PAGELENGTH
Abbreviation:
PL
Arguments:
Default:
PAGELENGTH (60)
Description:
See Also:
PAGEWIDTH
Example:
44
PAGEWIDTH
Abbreviation:
PW
Arguments:
Default:
PAGEWIDTH (132)
Description:
See Also:
PAGELENGTH
Example:
45
PREPRINT
Abbreviation:
PP
Arguments:
Default:
Description:
Example:
46
PRINT / NOPRINT
Abbreviation:
PR / NOPR
Arguments:
Default:
PRINT (basename.LST)
Description:
Example:
47
REGFILE
Abbreviation:
RF
Arguments:
Default:
None.
Description:
Example:
48
REGISTERBANK
Abbreviation:
RB
Arguments:
Default:
REGISTERBANK (0)
Description:
Example:
49
REGPARMS / NOREGPARMS
Abbreviation:
None.
Arguments:
None.
Default:
REGPARMS
Description:
() {
char
int
x1 =
x1 =
a;
x1, x2;
old_func (x2, a);
new_func (x2, a);
Example:
50
ROM
Abbreviation:
None.
Arguments:
Default:
ROM (LARGE)
Description:
Description
SMALL
COMPACT
LARGE
See Also:
Example:
51
SAVE / RESTORE
Abbreviation:
None.
Arguments:
None.
Default:
None.
Description:
Example:
#pragma save
#pragma noregparms
extern void test1 (char c, int i);
extern char test2 (long l, float f);
#pragma restore
52
SMALL
Abbreviation:
SM
Arguments:
None.
Default:
SMALL
Description:
See Also:
Example:
53
SRC
Abbreviation:
None.
Arguments:
Default:
None.
Description:
See Also:
ASM, ENDASM
Example:
54
SYMBOLS
Abbreviation:
SB
Arguments:
None.
Default:
Description:
Example:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
CLASS
=====
ABSBIT
PUBLIC
PARAM
PUBLIC
PARAM
PARAM
AUTO
* TAG *
MEMBER
MEMBER
MEMBER
SFR
PUBLIC
SFR
SFR
SFR
PUBLIC
MSPACE
======
----CODE
DATA
CODE
DATA
DATA
DATA
----DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
CODE
TYPE
====
BIT
PROC
PTR
PROC
PTR
PTR
STRUCT
STRUCT
U_CHAR
U_CHAR
U_CHAR
U_CHAR
BIT
U_CHAR
U_CHAR
U_CHAR
ARRAY
OFFSET
======
00AFH
----0000H
----0000H
0003H
0006H
----0000H
0001H
0002H
0099H
0001H
0098H
0089H
0088H
00FDH
SIZE
====
1
----3
----3
3
3
3
1
1
1
1
1
1
1
1
119
55
WARNINGLEVEL
Abbreviation:
WL
Arguments:
Default:
WARNINGLEVEL (2)
Description:
Example:
Description
2 (Default)
56
57
Memory Models
Pointers
Function Attributes
Keywords
To facilitate many of the features of the 8051, C51 adds a number of new
keywords to the scope of the C language. The following is a list of the keywords
available in C51, Version 4:
_at_
alien
bdata
bit
code
compact
data
idata
interrupt
large
pdata
_priority_
reentrant
sbit
sfr
sfr16
small
_task_
using
xdata
You can disable these extensions using the NOEXTEND control directive.
Refer to Chapter 2. Compiling with C51 on page 3 for more information.
58
Program Memory
Program (CODE) memory is read only; it cannot be written to. Program memory
may reside within the 8051 CPU, it may be external, or it may be both,
depending upon the 8051 derivative and the hardware design. There may be up
to 64 KBytes of program memory. Program code including all functions and
library routines are stored in program memory. Constant variables may be stored
in program memory, as well. The 8051 executes programs stored in program
memory only.
Program memory can be accessed by using the code memory type specifier in
C51.
59
60
61
Memory Models
The memory model determines which default memory type to use for function
arguments, automatic variables, and declarations with no explicit memory type
specifier. You specify the memory model on the C51 command line using the
SMALL, COMPACT, and LARGE control directives. Refer to Control
Directives on page 6 for more information about these directives.
NOTE
Except in very special selected applications, always use the default SMALL
memory model. It generates the fastest, most efficient code.
By explicitly declaring a variable with a memory type specifier, you may
override the default memory type imposed by the memory model . Refer to
Memory Types on page 62 for more information.
Small Model
In this model, all variables, by default, reside in the internal data memory of the
8051 system. (This is the same as if they were declared explicitly using the data
memory type specifier.) In this memory model, variable access is very efficient.
However, all objects, as well as the stack must fit into the internal RAM. Stack
size is critical because the real stack size depends upon the nesting depth of the
various functions. Typically, if the linker/locator is configured to overlay
variables in the internal data memory, the small model is the best model to use.
62
Compact Model
Using the compact model, all variables, by default, reside in one page of external
data memory. (This is as if they were explicitly declared using the pdata
memory type specifier.) This memory model can accommodate a maximum of
256 bytes of variables. The limitation is due to the addressing scheme used,
which is indirect through registers R0 and R1 (@R0, @R1). This memory
model is not as efficient as the small model, therefore, variable access is not as
fast. However, the compact model is faster than the large model.
When using the compact model, C51 accesses external memory with instructions
that utilize the @R0 and @R1 operands. R0 and R1 are byte registers and
provide only the low-order byte of the address. If the compact model is used
with more than 256 bytes of external memory, the high-order address byte
(or page) is provided by Port 2 on the 8051. In this case, you must initialize Port
2 with the proper external memory page to use. This can be done in the startup
code. You must also specify the starting address for PDATA to the linker.
Refer to STARTUP.A51 on page 114 for more information on using the
compact model.
Large Model
In the large model, all variables, by default, reside in external data memory (up
to 64 KBytes). (This is the same as if they were explicitly declared using the
xdata memory type specifier.) The data pointer (DPTR) is used for addressing.
Memory access through this data pointer is inefficient, especially on variables
with a length of two or more bytes. This type of data access mechanism
generates more code than the small or compact models.
Memory Types
The C51 compiler explicitly supports the architecture of the 8051 and its
derivatives and provides access to all memory areas of the 8051. Each variable
may be explicitly assigned to a specific memory space.
Accessing the internal data memory is considerably faster than accessing the
external data memory. For this reason, place frequently used variables in
internal data memory. Place larger, less frequently used variables in external
data memory.
63
Description
code
data
idata
Indirectly addressable internal data memory; accessed across the full internal
address space (256 bytes).
bdata
Bit-addressable internal data memory; allows mixed bit and byte access
(16 bytes).
xdata
pdata
Paged (256 bytes) external data memory; accessed by opcode MOVX @Rn.
As with the signed and unsigned attributes, you may include memory type
specifiers in the variable declaration.
Example:
char data var1;
char code text[] = "ENTER PARAMETER:";
unsigned long xdata array[100];
float idata x,y,z;
unsigned int pdata dimension;
unsigned char xdata vector[10][4][4];
char bdata flags;
NOTE
For compatibility with previous versions of the C51 compiler, you may specify
the memory area before the data type. For example, the following declaration
data char x;
is equivalent to
char data x;
Nonetheless, this feature should not be used in new programs because it may not
be supported in future versions of the C51 compiler.
64
Data Types
C51 provides you with a number of basic data types to use in your C programs.
C51 offers you the standard C data types and also supports several data types
that are unique to the 8051 platform. The following table lists the available C51
data types.
Data Type
Bits
Bytes
Value Range
bit
signed char
-128 to +127
unsigned char
0 to 255
enum
16
-32768 to +32767
signed short
16
-32768 to +32767
unsigned short
16
0 to 65535
signed int
16
-32768 to +32767
unsigned int
16
0 to 65535
signed long
32
-2147483648 to 2147483647
unsigned long
32
0 to 4294967295
float
32
1.175494E-38 to 3.402823E+38
sbit
sfr
0 to 255
sfr16
16
0 to 65535
0 to 1
0 to 1
The bit, sbit, sfr, and sfr16 data types are not provided in ANSI C and are unique to C51.
These data types are described in detail in the following sections.
65
Bit Types
C51 provides you with a bit data type which may be used for variable
declarations, argument lists, and function return values. A bit variable is
declared just as other C data types are declared.
Example:
static bit done_flag = 0;
/* bit variable */
bit testfunc (
bit flag1,
bit flag2)
{
.
.
.
return (0);
}
/* bit function */
/* bit arguments */
3
/* bit return value */
All bit variables are stored in a bit segment located in the internal memory area
of the 8051. Because this area is only 16 bytes long, a maximum of 128 bit
variables may be declared within any one scope.
Memory types may be included in the declaration of a bit variable. However,
because bit variables are stored in the internal data area of the 8051, the data
and idata memory types only may be included in the declaration. Any other
memory types are invalid.
The following restrictions apply to bit variables and bit declarations:
!
/* invalid */
/* invalid */
66
Bit-addressable Objects
Bit-addressable objects are objects which can be addressed as bytes or as bits.
Only data objects that occupy the bit-addressable area of the 8051 internal
memory fall into this category. The C51 compiler places variables declared with
the bdata memory type into this bit-addressable area. You may declare these
variables as shown below:
/* Bit-addressable int */
/* Bit-addressable array */
The variables ibase and bary are bit-addressable. Therefore, the individual
bits of these variables may be directly accessed and modified. Use the sbit
keyword to declare new variables that access the bits of variables declared using
bdata. For example:
sbit mybit0 = ibase ^ 0;
sbit mybit15 = ibase ^ 15;
/* bit 0 of ibase */
/* bit 15 of ibase */
/* bit 7 of bary[0] */
/* bit 7 of bary[3] */
The above example represents declarations, not assignments to the bits of the
ibase and bary variables declared above. The expression following the carat
symbol (^) in the example, specifies the position of the bit to access with this
declaration. This expression must be a constant value. The range depends on
the type of the base variable included in the declaration. The range is 0 to 7 for
char and unsigned char, 0 to 15 for int, unsigned int, short, and unsigned
short, and 0 to 31 for long and unsigned long.
You may provide external variable declarations for the sbit type to access these
types in other modules. For example:
extern bit mybit0;
extern bit mybit15;
/* bit 0 of ibase */
/* bit 15 of ibase */
/* bit 7 of bary[0] */
/* bit 7 of bary[3] */
Declarations involving the sbit type require that the base object be declared with
the memory type bdata. The only exceptions are the variants for special
function bits. Refer to Special Function Registers on page 68 for more
information.
67
The following example shows how to change the ibase and bary bits using the
above declarations.
Ary37 =
bary[3]
ibase =
mybit15
0;
= 'a';
-1;
= 1;
/*
/*
/*
/*
The bdata memory type is handled like the data memory type except that
variables declared with bdata reside in the bit-addressable portion of the
internal data memory. Note that the total size of this area of memory may not
exceed 16 bytes.
In addition to declaring sbit variables for scalar types, you may also declare sbit
variables for structures and unions. For example:
union lft
{
float mf;
long ml;
};
bdata struct bad
{
char m1;
union lft u;
} tcp;
sbit tcpf31 = tcp.u.ml ^ 31;
sbit tcpm10 = tcp.m1 ^ 0;
sbit tcpm17 = tcp.m1 ^ 7;
/* bit 31 of float */
NOTE
You may not specify bit variables for the bit positions of a float. However, you
may include the float and a long in a union. Then, you may declare bit
variables to access the bits in the long type.
The sbit data type uses the specified variable as a base address and adds the bit
position to obtain a physical bit address. Physical bit addresses are not
equivalent to logical bit positions for certain data types. Physical bit position 0
refers to bit position 0 of the first byte. Physical bit position 8 refers to bit
position 0 of the second byte. Because int variables are stored high-byte first,
bit 0 of the integer is located in bit position 0 of the second byte. This is physical
bit position 8 when accessed using an sbit data type.
68
Within the 8051 family, the number and type of SFRs vary. Note that no SFR
names are predefined by the C51 compiler. However, declarations for SFRs are
provided in include files.
C51 provides you with a number of include files for various 8051 derivatives.
Each file contains declarations for the SFRs available on that derivative. See
8051 Special Function Register Include Files on page 191 for more
information about include files.
C51 provides access to SFRs with the sfr, sfr16, and sbit data types. The
following sections describe each of these data types.
sfr
SFRs are declared in the same fashion as other C variables. The only difference
is that the data type specified is sfr rather than char or int. For example:
sfr
sfr
sfr
sfr
P0
P1
P2
P3
=
=
=
=
0x80;
0x90;
0xA0;
0xB0;
/*
/*
/*
/*
Port-0,
Port-1,
Port-2,
Port-3,
address
address
address
address
80h */
90h */
0A0h */
0B0h */
P0, P1, P2, and P3 are the SFR name declarations. Names for sfr variables are
defined just like other C variable declarations. Any symbolic name may be used
in an sfr declaration.
The address specification after the equal sign (=) must be a numeric constant.
(Expressions with operators are not allowed.) This constant expression must lie
in the SFR address range (0x80 to 0xFF).
69
sfr16
Many of the newer 8051 derivatives use two SFRs with consecutive addresses to
specify 16-bit values. For example, the 8052 uses addresses 0xCC and 0xCD for
the low and high bytes of timer/counter 2. C51 provides the sfr16 data type to
access 2 SFRs as a 16-bit SFR.
Access to 16-bit SFRs is possible only when the low byte immediately precedes
the high byte. The low byte is used as the address in the sfr16 declaration. For
example:
sfr16 T2 = 0xCC;
sfr16 RCAP2 = 0xCA;
In this example, T2 and RCAP2 are declared as 16-bit special function registers.
The sfr16 declarations follow the same rules as outlined for sfr declarations.
Any symbolic name can be used in an sfr16 declaration. The address
specification after the equal sign (=) must be a numeric constant. Expressions
with operators are not allowed. The address must be the low byte of the SFR
low-byte, high-byte pair.
sbit
With typical 8051 applications, it is often necessary to access individual bits
within an SFR. The C51 compiler makes this possible with the sbit data type.
The sbit data type allows you to access bit-addressable SFRs. For example:
sbit EA = 0xAF;
This declaration defines EA to be the SFR bit at address 0xAF. On the 8051,
this is the enable all bit in the interrupt enable register.
NOTE
Not all SFRs are bit-addressable. Only those SFRs whose address is evenly
divisible by 8 are bit-addressable. The lower nibble of the SFRs address must
be 0 or 8. For example, SFRs at 0xA8 and 0xD0 are bit-addressable, whereas
SFRs at 0xC7 and 0xEB are not. To calculate an SFR bit address, add the bit
position to the SFR byte address. So, to access bit 6 in the SFR at 0xC8, the SFR
bit address would be 0xCE (0xC8 + 6).
70
Any symbolic name can be used in an sbit declaration. The expression to the
right of the equal sign (=) specifies an absolute bit address for the symbolic
name. There are three variants for specifying the address:
Variant 1:
sfr_name ^ int_constant
This variant uses a previously declared sfr (sfr_name) as the
base address for the sbit. The address of the existing SFR
must be evenly divisible by 8. The expression following the
carat symbol (^) specifies the position of the bit to access
with this declaration. The bit position must be a number in
the 0 to 7 range. For example:
sfr
sfr
sbit
sbit
sbit
3
Variant 2:
PSW = 0xD0;
IE = 0xA8;
OV = PSW ^ 2;
CY = PSW ^ 7;
EA = IE ^ 7;
int_constant ^ int_constant
This variant uses an integer constant as the base address for
the sbit. The base address value must be evenly divisible by
8. The expression following the carat symbol (^) specifies
the position of the bit to access with this declaration. The
bit position must be a number in the 0 to 7 range. For
example:
sbit OV = 0xD0 ^ 2;
sbit CY = 0xD0 ^ 7;
sbit EA = 0xA8 ^ 7;
Variant 3:
int_constant
This variant uses an absolute bit address for the sbit. For
example:
sbit OV = 0xD2;
sbit CY = 0xD7;
sbit EA = 0xAF;
NOTE
Special function bits represent an independent declaration class that may not be
interchangeable with other bit declarations or bit fields.
The sbit data type declaration may be used to access individual bits of variables
declared with the bdata memory type specifier. Refer to Bit-addressable
Objects on page 66 for more information.
71
where:
memory_space
type
variable_name
constant
The absolute address following _at_ must conform to the physical boundaries of
the memory space for the variable. C51 checks for invalid address
specifications.
The following restrictions apply to absolute variable location:
1. Absolute variables cannot be initialized.
2. Functions and variables of type bit cannot be located at an absolute address.
72
Often, you may wish to declare your variables in one source module and access
them in another. Use the following external declarations to access the _at_
variables defined above in another source file.
struct link
{
struct link idata *next;
char
code *test;
};
extern idata struct link list;
extern xdata char text[256];
extern xdata int i1;
73
Pointers
C51 supports the declaration of variable pointers using the * character. C51
pointers can be used to perform all operations available in standard C. However,
because of the unique architecture of the 8051 and its derivatives, C51 provides
two different types of pointers: memory-specific pointers and generic pointers.
Each of these pointer types, as well as conversion methods are discussed in the
following sections.
Generic Pointers
Generic pointers are declared in the same fashion as standard C pointers. For
example:
char *s;
int *numptr;
long *state;
/* string ptr */
/* int ptr */
/* Texas */
Generic pointers are always stored using three bytes. The first byte is for the
memory type, the second is for the high-order byte of the offset, and the third is
for the low-order byte of the offset. The following table contains the memory
type byte values and their associated memory type.
Memory Type
xdata
pdata
code
Value
0x00
0x01
0xFE
0xFF
Generic pointers may be used to access any variable regardless of its location in
8051 memory space. Many of the C51 library routines use these pointer types
for this reason. By using these generic pointers, a function can access data
regardless of the memory in which it is stored.
NOTE
The code generated for a generic pointer executes more slowly than the
equivalent code generated for a memory-specific pointer. This is because the
memory area is not known until run-time. The compiler cannot optimize memory
accesses and must generate generic code that can access any memory area. If
execution speed is a priority, you should use memory-specific pointers instead of
generic pointers wherever possible.
74
The following code and assembly listing shows the values assigned to generic
pointers for variables in different memory areas. Note that the first value is the
memory space followed by the high-order byte and low-order byte of the
address.
stmt level
1
2
3
4
5
6
7
1
8
1
9
1
10
1
11
1
12
1
13
1
14
1
15
1
16
1
17
1
18
1
19
1
20
1
21
1
22
1
23
1
24
1
25
1
26
1
27
1
28
1
29
1
30
1
31
1
source
char *c_ptr;
int *i_ptr;
long *l_ptr;
void
{
char
int
long
/* char ptr */
/* int ptr */
/* long ptr */
main (void)
data dj;
data dk;
data dl;
/* data vars */
/* xdata vars */
char code cj = 9;
/* code vars */
int code ck = 357;
long code cl = 123456789;
c_ptr = &dj;
i_ptr = &dk;
l_ptr = &dl;
/* data ptrs */
c_ptr = &xj;
i_ptr = &xk;
l_ptr = &xl;
/* xdata ptrs */
c_ptr = &cj;
i_ptr = &ck;
l_ptr = &cl;
}
/* code ptrs */
0000
0003
0006
0009
000C
000F
0012
0015
0018
001B
001E
c_ptr+02H,#LOW xj
; SOURCE LINE # 25
750001 R
MOV
i_ptr,#01H
750000 R
MOV
i_ptr+01H,#HIGH xk
750000 R
MOV
i_ptr+02H,#LOW xk
; SOURCE LINE # 26
750001 R
MOV
l_ptr,#01H
750000 R
MOV
l_ptr+01H,#HIGH xl
750000 R
MOV
l_ptr+02H,#LOW xl
; SOURCE LINE # 28
7500FF R
MOV
c_ptr,#0FFH
750000 R
MOV
c_ptr+01H,#HIGH cj
750000 R
MOV
c_ptr+02H,#LOW cj
; SOURCE LINE # 29
7500FF R
MOV
i_ptr,#0FFH
750000 R
MOV
i_ptr+01H,#HIGH ck
750000 R
MOV
i_ptr+02H,#LOW ck
; SOURCE LINE # 30
7500FF R
MOV
l_ptr,#0FFH
750000 R
MOV
l_ptr+01H,#HIGH cl
750000 R
MOV
l_ptr+02H,#LOW cl
; SOURCE LINE # 31
22
RET
; FUNCTION main (END)
75
MOV
In the above example listing, the generic pointers c_ptr, i_ptr, and l_ptr
are all stored in the internal data memory of the 8051. However, you may
specify the memory area in which a generic pointer is stored by using a memory
type specifier. For example:
char * xdata strptr;
int * data numptr;
long * idata varptr;
These examples are pointers to variables that may be stored in any memory area.
The pointers, however, are stored in xdata, data, and idata respectively.
76
Memory-specific Pointers
Memory-specific pointers always include a memory type specification in the
pointer declaration and always refer to a specific memory area. For example:
char data *str;
int xdata *numtab;
long code *powtab;
Because the memory type is specified at compile-time, the memory type byte
required by generic pointers is not needed by memory-specific pointers.
Memory-specific pointers can be stored using only one byte (idata, data, bdata,
and pdata pointers) or two bytes (code and xdata pointers).
NOTE
The code generated for a memory-specific pointer executes more quickly than
the equivalent code generated for a generic pointer. This is because the memory
area is known at compile-time rather than at run-time. The compiler can use
this information to optimize memory accesses. If execution speed is a priority,
you should use memory-specific pointers instead of generic pointers wherever
possible.
Like generic pointers, you may specify the memory area in which a
memory-specific pointer is stored. To do so, prefix the pointer declaration with
a memory type specifier. For example:
char data * xdata str;
int xdata * data numtab;
long code * idata powtab;
77
The following code and assembly listing shows how pointer values are assigned
to memory-specific pointers. Note that the code generated for these pointers is
much less involved than the code generated in the generic pointers example
listing in the previous section.
stmt level
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
source
char data *c_ptr;
int xdata *i_ptr;
long code *l_ptr;
1
1
1
1
1
1
1
0000
0003
0006
0009
000C
000F
78
Pointer Conversions
C51 can convert between memory-specific pointers and generic pointers.
Pointer conversions can be forced by explicit program code using type casts or
can be coerced by the compiler.
The C51 compiler coerces a memory-specific pointer into a generic pointer when
the memory-specific pointer is passed as an argument to a function which
requires a generic pointer. This is the case for functions such as printf, sprintf,
and gets which use generic pointers as arguments. For example:
extern int printf (void *format, ...);
/* fmt is converted */
/* no conversions */
In the call to printf, the argument fmt which represents a 2-byte code pointer is
automatically converted or coerced into a 3-byte generic pointer. This is done
because the prototype for printf requires a generic pointer as the first argument.
NOTE
A memory-specific pointer used as an argument to a function is always
converted into a generic pointer if no function prototype is present. This can
cause errors if the called function actually expects a shorter pointer as an
argument. In order to avoid these kinds of errors in programs, use #include
files, and prototype all external functions. This guarantees conversion of the
necessary types by the compiler and increases the likelihood that the compiler
detects type conversion errors.
79
The following table details the process involved in converting generic pointers
(generic *) to memory-specific pointers (code *, xdata *, idata *, data *,
pdata *).
Conversion Type
Description
generic * to code *
generic * to xdata *
generic * to data *
generic * to idata *
generic * to pdata *
Description
xdata * to generic *
The memory type of the generic pointer is set to 0x01 for xdata.
The 2-byte offset of the xdata * is used.
code * to generic *
The memory type of the generic pointer is set to 0xFF for code.
The 2-byte offset of the code * is used.
idata * to generic *
data * to generic *
The memory type of the generic pointer is set to 0x00 for idata / data.
The 1-byte offset of the idata * / data * is converted to an unsigned
int and used as the offset.
pdata * to generic *
The memory type of the generic pointer is set to 0xFE for pdata.
The 1-byte offset of the pdata * is converted to an unsigned int and
used as the offset.
80
The following listing illustrates a few pointer conversions and the resulting code:
stmt level
1
2
3
4
5
6
7
1
8
1
9
1
10
1
11
1
12
1
13
1
14
1
15
1
*** WARNING
16
1
*** WARNING
17
1
source
int *p1;
int xdata *p2;
int idata *p3;
int code *p4;
void
p1 =
p1 =
p1 =
/*
/*
/*
/*
pconvert (void) {
p2;
/* xdata* to generic* */
p3;
/* idata* to generic* */
p4;
/* code* to generic* */
p4 = p1;
p3 = p1;
p2 = p1;
/* generic* to code* */
/* generic* to idata* */
/* generic* to xdata* */
p2 = p3;
/* idata* to
259 IN LINE 15 OF P.C: pointer:
p3 = p4;
/* code* to
259 IN LINE 16 OF P.C: pointer:
}
xdata* (WARN) */
different mspace
idata* (WARN) */
different mspace
81
Abstract Pointers
Abstract pointer types let you access fixed memory locations in any memory
area. You may also use abstract pointers to call functions located at absolute or
fixed addresses.
Abstract pointer types are described here through code examples which use the
following variables.
char xdata *px;
char idata *pi;
char code *pc;
/* ptr to xdata */
/* ptr to idata */
/* ptr to code */
char c;
int i;
The following example assigns the address of the main C function to a pointer
(stored in data memory) to a char stored in code memory.
Source
pc = (void *) main;
Object
0000 750000
0003 750000
R
R
MOV
MOV
pc,#HIGH main
pc+01H,#LOW main
Object
0000 750000
MOV
pi,#LOW i
Object
0000 850000
MOV
pi,px+01H
Object
0000 750012
0003 750034
R
R
MOV
MOV
pc,#012H
pc+01H,#034H
82
Source
Object
0000 12FF00
0003 8E00
0005 8F00
R
R
LCALL
MOV
MOV
0FF00H
i,R6
i+01H,R7
Object
0000
0003
0004
0005
908000
E4
93
F500
MOV
CLR
MOVC
MOV
DPTR,#08000H
A
A,@A+DPTR
c,A
Object
0000
0003
0004
0006
90FF00
E0
2500
F500
R
R
MOV
MOVX
ADD
MOV
DPTR,#0FF00H
A,@DPTR
A,c
c,A
Object
0000
0002
0003
0005
78F0
E6
2500
F500
R
R
MOV
MOV
ADD
MOV
R0,#0F0H
A,@R0
A,c
c,A
83
Object
0000
0002
0003
0005
78E8
E2
2500
F500
R
R
MOV
MOVX
ADD
MOV
R0,#0E8H
A,@R0
A,c
c,A
Object
0000
0003
0004
0005
0006
0008
0009
000B
902100
E4
93
FE
7401
93
8E00
F500
R
R
MOV
CLR
MOVC
MOV
MOV
MOVC
MOV
MOV
DPTR,#02100H
A
A,@A+DPTR
R6,A
A,#01H
A,@A+DPTR
i,R6
i+01H,A
Object
0000
0003
0004
0005
0006
0007
0009
904000
E0
FE
A3
E0
8E00
F500
R
R
MOV
MOVX
MOV
INC
MOVX
MOV
MOV
DPTR,#04000H
A,@DPTR
R6,A
DPTR
A,@DPTR
px,R6
px+01H,A
84
Like the previous example, this example casts 0x4000 as a pointer to a pointer in
xdata that points to a char in xdata. However, the pointer is accessed as an
array of pointers in xdata. The assignment accesses array element 0 (which is
stored at 0x4000 in xdata) and extracts the pointer there that points to the char
stored in xdata.
Source
Object
0000
0003
0004
0005
0006
0007
0009
904000
E0
FE
A3
E0
8E00
F500
R
R
MOV
MOVX
MOV
INC
MOVX
MOV
MOV
DPTR,#04000H
A,@DPTR
R6,A
DPTR
A,@DPTR
px,R6
px+01H,A
The following example is identical to the previous one except that the
assignment accesses element 1 from the array. Since the object pointed to is a
pointer in xdata (to a char), the size of each element in the array is 2 bytes. The
assignment accesses array element 1 (which is stored at 0x4002 in xdata) and
extracts the pointer there that points to the char stored in xdata.
Source
Object
0000
0003
0004
0005
0006
0007
0009
904002
E0
FE
A3
E0
8E00
F500
R
R
MOV
MOVX
MOV
INC
MOVX
MOV
MOV
DPTR,#04002H
A,@DPTR
R6,A
DPTR
A,@DPTR
px,R6
px+01H,A
85
Function Declarations
C51 provides you with a number of extensions for standard C function
declarations. These extensions allow you to:
!
Specify reentrancy
funcname (!args")
!{small
| compact | large}"
!reentrant" !interrupt
where:
return_type
funcname
args
reentrant
interrupt
using
Descriptions of these attributes and other features are described in detail in the
following sections.
86
The total stack space is quite limited: only 256 bytes maximum. Rather than
consume stack space with function parameters or arguments, C51 assigns a fixed
memory location for each function parameter. When a function is called, the
caller must copy the arguments into the assigned memory locations before
transferring control to the desired function. The function then extracts its
parameters, as needed, from these fixed memory locations. Only the return
address is stored on the stack during this process. Interrupt functions require
more stack space because they must switch register banks and save the values of
a few registers on the stack.
By default, the C51 compiler passes up to three function arguments in registers.
This enhances speed performance. For more information, refer to Passing
Parameters in Registers on page 87.
NOTE
Some 8051 derivatives provide as little as 64 bytes of internal memory. The
8051 provides 128 and the 8052 provides 256. Take this into consideration
when determining which memory model to use, because the amount of internal
data memory directly affects the amount of stack space.
87
long, float
generic ptr
R7
R6 & R7
R4R7
R1R3
R5
R4 & R5
R4R7
R1R3
R3
R2 & R3
R1R3
If no registers are available for argument passing, fixed memory locations are
used for function parameters.
Register
Description
bit
Carry Flag
R7
R6 & R7
R4-R7
float
R4-R7
generic ptr
R1-R3
NOTE
If the first parameter of a function is of type bit, other parameters are not passed
in registers. This is because the parameters that can be passed in registers are
out of sequence with the numbering scheme shown above. For this reason, bit
parameters should be declared at the end of the argument list.
88
The advantage of functions using the SMALL memory model is that the local
data and function argument parameters are stored in the internal 8051 RAM.
Therefore, data access is very efficient. The internal memory is limited,
however. Occasionally, the limited amount of internal data memory available
when using the small model cannot satisfy the requirements of a very large
program, and other memory models must be used. In this situation, you may
declare that a function use a different memory model, as shown above.
By specifying the function model attribute in the function declaration, you can
select which of the three possible reentrant stacks and frame pointers are used.
Stack access in the SMALL model is more efficient than in the LARGE model.
89
The currently selected register bank is saved on the stack at function entry.
90
The following example shows how to specify the using function attribute and
what the generated assembly code for the function entry and exit looks like.
stmt level
1
2
3
4
5
6
7
8
9
source
1
1
1
In the previous example, the code starting at offset 0000h saves the initial PSW
on the stack and sets the new register bank. The code starting at offset 0015h
restores the original register bank by popping the original PSW from the stack.
The using attribute may not be used in functions that return a value in registers.
You must exercise extreme care to ensure that register bank switches are
performed only in carefully controlled areas. Failure to do so may yield
incorrect function results. Even when you use the same register bank, functions
declared with the using attribute cannot return a bit value.
Typically, the using attribute is most useful in functions that also specify the
interrupt attribute. It is most common to specify a different register bank for
each interrupt priority level. Therefore, you could use one register bank for all
non-interrupt code, one for the high-level interrupt, and one for the low-level
interrupt.
91
Specify the REGISTERBANK control directive along with the new register
bank number
By default, the C51 compiler generates code that accesses the registers R0R7
using absolute addresses. This is done for maximum performance. Absolute
register accesses are controlled by the AREGS and NOAREGS control
directives. Functions which employ absolute register accesses must not be called
from another function that uses a different register bank. Doing so causes
unpredictable results because the called function assumes that a different register
bank is selected. To make a function insensitive to the current register bank, the
function must be compiled using the NOAREGS control directive. This would
be useful for a function that was called from the main program and also from an
interrupt function that uses a different register bank.
NOTE
The C51 compiler does not and cannot detect a register bank mismatch between
functions. Therefore, make sure that functions using alternate register banks
call only other functions that do not assume a default register bank.
Refer to Chapter 2. Compiling with C51 on page 3 for more information
regarding the REGISTERBANK, AREGS, and NOARGES directives.
92
Interrupt Functions
The 8051 and its derivatives provide a number of hardware interrupts that may
be used for counting, timing, detecting external events, and sending and
receiving data using the serial interface. The standard interrupts found on an
8051 are listed in the following table:
Interrupt Number
Interrupt Description
Address
EXTERNAL INT 0
0003h
TIMER/COUNTER 0
000Bh
EXTERNAL INT 1
0013h
TIMER/COUNTER 1
001Bh
SERIAL PORT
0023h
As 8051 vendors created new parts, more interrupts were added. The Keil C51
compiler supports interrupt functions for 32 interrupts (0-31). Use the interrupt
vector address in the following table to determine the interrupt number.
Interrupt Number
Address
Interrupt Number
Address
0003h
16
0083h
000Bh
17
008Bh
0013h
18
0093h
001Bh
19
009Bh
0023h
20
00A3h
002Bh
21
00ABh
0033h
22
00B3h
003Bh
23
00BBh
0043h
24
00C3h
004Bh
25
00CBh
10
0053h
26
00D3h
11
005Bh
27
00DBh
12
0063h
28
00E3h
13
006Bh
29
00EBh
14
0073h
30
00F3h
15
007Bh
31
00FBh
93
The C51 compiler provides you with a method of calling a C function when an
interrupt occurs. This support lets you create interrupt service routines in C.
You need only be concerned with the interrupt number and register bank
selection. The compiler automatically generates the interrupt vector and entry
and exit code for the interrupt routine. The interrupt function attribute, when
included in a declaration, specifies that the associated function is an interrupt
function. For example:
unsigned int interruptcnt;
unsigned char second;
void timer0 (void) interrupt 1 using 2 {
if (++interruptcnt == 4000) {
/* count to 4000 */
second++;
/* second counter
*/
interruptcnt = 0;
/* clear int counter */
}
}
The contents of the SFR ACC, B, DPH, DPL, and PSW, when required, are
saved on the stack at the function invocation time.
All working registers that are used in the interrupt function are stored on the
stack if a register bank is not specified with the using attribute.
The working registers and special registers that were saved on the stack are
restored before exiting the function.
94
The following sample program shows you how to use the interrupt attribute.
The program also shows you what the code generated to enter and exit the
interrupt function looks like. The using function attribute is also used in the
example to select a register bank different from that of the non-interrupt program
code. However, because no working registers are needed in this function, the
code generated to switch the register bank is eliminated by the optimizer.
stmt level
1
2
3
4
5
6
7
8
source
extern bit alarm;
int alarm_count;
1
1
1
In the example above, note that the ACC and PSW registers are saved at offset
0000h and restored at offset 0011h. Note also the RETI instruction generated
to exit the interrupt.
95
Interrupt function declarations may not include a return value. They must be
declared as void (see the above examples). The compiler emits an error
message if any attempt is made to define a return value for the interrupt
function. The implicit int return value, however, is ignored by the compiler.
The compiler generates an interrupt vector for each interrupt function. The
code generated for the vector is a jump to the beginning of the interrupt
function. Generation of interrupt vectors can be suppressed by including the
NOINTVECTOR control directive in the C51 command line. In this case,
you must provide interrupt vectors from separate assembly modules. Refer to
the INTVECTOR and INTERVAL control directives for more information
about the interrupt vector table.
The C51 compiler allows interrupt numbers within the 0 to 31 range. Refer
to your 8051 derivative document to determine which interrupts are available.
Functions that are invoked from an interrupt procedure must function with the
same register bank as the interrupt procedure. When the NOAREGS
directive is not explicitly specified, the compiler may generate absolute
register accesses using the register bank selected (by the using attribute or by
the REGISTERBANK control) for that function. Unpredictable results may
occur when a function assumes a register bank other than the one currently
selected. Refer to Register Bank Access on page 91 for more information.
96
Reentrant Functions
A reentrant function can be shared by several processes at the same time. When
a reentrant function is executing, another process can interrupt the execution and
then begin to execute that same reentrant function. Normally, functions in C51
cannot be called recursively or in a fashion which causes reentrancy. The reason
for this limitation is that function arguments and local variables are stored in
fixed memory locations. The reentrant function attribute allows you to declare
functions that may be reentrant and, therefore, may be called recursively. For
example:
97
Reentrant functions use the default memory model to determine which memory
space to use for the reentrant stack. You may specify (with the small, compact,
and large function attributes) which memory model to use for a function. Refer
to Specifying the Memory Model for a Function on page 88 for more
information about memory models and function declarations.
The following rules apply to functions declared with the reentrant attribute.
!
bit type function arguments may not be used. Local bit scalars are also not
available. The reentrant capability does not support bit-addressable variables.
Reentrant function cannot use the alien attribute specifier to enable PL/M-51
argument passing conventions.
A reentrant function may simultaneously have other attributes like using and
interrupt and may include an explicit memory model attribute (small,
compact, large).
Return addresses are stored in the 8051 hardware stack. Any other required
PUSH and POP operations also affect the 8051 hardware stack.
Each of the three possible reentrant models contains its own reentrant stack
area and stack pointer. For example, if small and large reentrant functions
are declared in a module, both small and large reentrant stacks are created
along with two associated stack pointers (one for small and one for large).
98
The following table details the stack pointer assembler variable name, data area,
and size for each of the three memory models.
Model
Stack Pointer
Stack Area
SMALL
?C_IBP (1 Byte)
COMPACT
?C_PBP (1 Byte)
LARGE
?C_XBP (2 Bytes)
The simulated stack area for reentrant functions is organized from top to bottom.
The 8051 hardware stack is just the opposite and is organized bottom to top.
When using the SMALL memory model, both the simulated stack and the 8051
hardware stack share the same memory area but from opposite directions.
The simulated stack and stack pointers are declared and initialized in the C51
startup code in STARTUP.A51 which can be found in the LIB subdirectory. You
must modify the startup code to specify which simulated stack(s) to initialize in
order to use reentrant functions. You can also modify the starting address for the
top of the simulated stack(s) in the startup code. Refer to STARTUP.A51 on
page 114 for more information on reentrant function stack areas.
99
You may also create functions in C that can be invoked by PL/M-51 routines.
To do this, use the alien function type specifier in the C function declaration.
For example:
alien char c_func (char a,
return (a * b);
}
int b)
Parameters and return values of PL/M-51 functions may be any of the following
types: bit, char, unsigned char, int, and unsigned int. Other types, including
long, float, and all types of pointers, can be declared in C functions with the
alien type specifier. However, use these types with care because PL/M-51 does
not directly support 32-bit binary integers or floating-point numbers.
Public variables declared in the PL/M-51 module are available to your C
programs by declaring them external like you would for any C variable.
100
where:
num
pri
is the priority for the task. Refer to the RTX51 Users Guide
or the RTX51 Tiny Users Guide for more information.
Task functions must be declared with a void return type and a void argument list.
101
Chapter 4. Preprocessor
The preprocessor built into the C51 compiler handles directives found in the
source file. C51 supports all of the ANSI Standard C directives. This chapter
gives a brief overview of the directives and elements provided by the
preprocessor.
Directives
Preprocessor directives must be the first non-whitespace text specified on a line.
All directives are prefixed with the pound or number-sign character (#). For
example:
#pragma
#include <stdio.h>
#define DEBUG 1
The following table lists the preprocessor directives and gives a brief description
of each.
Directive
define
Description
Defines a preprocessor macro or constant.
elif
Initiates an alternative branch of the if condition, when the previous if, ifdef, ifndef,
or elif branch was not taken.
else
Initiates an alternative branch when the previous if, ifdef, or ifndef branch was not
taken.
endif
error
Outputs an error message defined by the user. This directive instructs the
compiler to emit the specified error message.
ifdef
ifndef
Same as ifdef but the evaluation succeeds if the definition is not defined.
if
include
line
pragma
undef
102
Chapter 4. Preprocessor
Stringize Operator
The stringize or number-sign operator (#), when used within a macro
definition, converts a macro parameter into a string constant. This operator may
be used only in a macro that has a specified argument or parameter list.
When the stringize operator immediately precedes the name of one of the macro
parameters, the parameter passed to the macro is enclosed within quotation
marks and is treated as a string literal. For example:
#define stringer(x)
stringer (text)
103
Token-pasting Operator
The token-pasting operator (##) within a macro definition combines two
arguments. It permits two separate tokens in the macro definition to be joined
into a single token.
If the name of a macro parameter used in the macro definition is immediately
preceded or followed by the token-pasting operator, the macro parameter and the
token-pasting operator are replaced by the value of the passed parameter. Text
that is adjacent to the token-pasting operator that is not the name of a macro
parameter is not affected. For example:
#define paster(n) printf ("token" #n " = %d", token##n)
paster (9);
This example shows the concatenation of token##n into token9. Both the
stringize and the token-pasting operators are used in this example.
104
Chapter 4. Preprocessor
Constant
Description
_ _C51_ _
Version number of the C51 compiler (for example, 300 for version 3.00).
_ _DATE_ _
_ _FILE_ _
_ _LINE_ _
_ _MODEL_ _
_ _TIME_ _
__STDC_ _
105
Siemens 80C517 and 80C537 (high-speed 32-bit and 16-bit binary arithmetic
operations, 8 data pointers).
The C51 compiler provides you with support for these CPUs through the use of
special libraries, library routines, and the command-line directives MODDP2
and MOD517. These directives enable C51 to generate object code that takes
advantage of the enhancements mentioned above. Refer to Chapter 3.
Language Extensions on page 57 for more information about these directives.
106
The Dallas Semiconductor 80C320, 80C520, and 80C530 provide 2 data pointers
which can be used for memory access. Using multiple data pointers can improve
the speed of library functions like memcpy, memmove, memcmp, strcpy, and
strcmp.
The MODDP2 control directive instructs the C51 compiler to generate code that
uses both data pointers in your program.
The C51 compiler uses at least one data pointer in an interrupt function. If an
interrupt function is compiled using the MODDP2 directive, both data pointers
are saved on the stack. This happens even if the interrupt function uses only one
data pointer.
To conserve stack space, you may compile interrupt functions with the
NOMODDP2 directive. The C51 compiler does not use the second data pointer
when this directive is used.
107
Data Pointers
The Siemens 80C517 and 80C537 provide 8 data pointers which can be used to
improve memory accesses. Using multiple data pointers can improve the
execution of library functions such as: memcpy, memmove, memcmp, strcpy,
and strcmp. The 8 data pointers of the 80C517 and 80C537 can also reduce the
stack load of interrupt functions.
C51 uses only 2 of the 8 data pointers of the 80C517 at a time. In order to keep
the stack load in the interrupt routines low, C51 switches to 2 unused data
pointers when switching the register bank. In this case, the contents of the
register DPSEL are saved on the stack, and a new pair of data pointers is
selected. Saving the data pointers on the stack is no longer required.
If an interrupt routine does not switch to another register bank (for example, the
function is declared without the using attribute), the data pointers must be saved
on the stack (using 4 bytes of stack space). To keep the size of the stack as small
as possible, use the MOD517(NODP8) directive to compile the interrupt routine
and the functions called from within the interrupt. This generates code for the
interrupt that uses only one data pointer and, therefore, only 2 bytes of stack
space.
108
High-speed Arithmetic
C51 uses the 32-bit and 16-bit arithmetic operations of the 80C517 to improve
performance of a number of math-intensive operations. C language programs
execute considerably faster when using either of these CPUs.
The following tables show execution times for various arithmetic operations and
compare the performance of the standard 8051 to that of the 80C517 CPU.
16-bit Binary Integer Operations
Operation
CPU
Routine
Min.
Avg.
Max.
Signed/unsigned multiplication
8051
80517
IMUL
intrinsic
29
17
29
17
29
17
Unsigned division
8051
80517
UIDIV
UIDIV517
16
22
128
22
153
22
Signed division
8051
80517
SIDIV
SIDIV517
53
35
141
52
181
60
Operation
CPU
Routine
Min.
Avg.
Max.
Signed/unsigned multiplication
8051
80517
LMUL
LMUL517
106
62
106
62
106
62
Unsigned division
8051
80517
ULDIV
ULDIV517
227
36
497
52
650
101
Signed division
8051
80517
SLDIV
SLDIV517
267
49
564
75
709
141
Left shift
8051
80517
LSHL
LSHL517
5
5
237
28
470
29
8051
80517
ULSHR
ULSHR517
5
5
237
29
470
30
8051
80517
SLSHR
237
470
109
Floating-point Operations
Operation
CPU
Routine
Min.
Avg.
Max.
Addition
8051
80517
FPADD
FPADD517
8
8
107
107
202
202
Subtraction
8051
80517
FPSUB
FPSUB517
11
11
113
113
214
214
Multiplication
8051
80517
FPMUL
FPMUL517
13
13
114
86
198
141
Division
8051
80517
FPDIV
FPDIV517
48
48
687
165
999
209
Comparison
8051
80517
FPCMP
FPCMP517
42
42
54
54
59
59
Square root
8051
80517
SQRT
SQRT517
12
12
1936
755
2360
882
Sine
8051
80517
SIN
SIN517
1565
1422
2928
2519
3476
3048
Cosine
8051
80517
COS
COS517
1601
1458
2921
2514
3665
3180
Tangent
8051
80517
TAN
TAN517
1982
1839
4966
3753
5699
4329
Arcsine
8051
80517
ASIN
ASIN517
912
912
6991
3984
8554
4717
Arccosine
8051
80517
ACOS
ACOS517
796
796
7578
4255
8579
4871
Arctangent
8051
80517
ATAN
ATAN517
1069
1037
3320
2444
3712
2737
Exponential
8051
80517
EXP
EXP517
233
176
3314
2879
5308
4724
Natural Logarithm
8051
80517
LOG
LOG517
32
32
3432
2405
4128
2926
Common Logarithm
8051
80517
LOG10
LOG10517
34
34
3607
2530
4328
3069
8051
80517
FPATOF
FPATOF517
960
722
3006
2202
5611
4144
110
NOTES
The execution times specified in the preceding tables do not take access times for
variables or stack operations into consideration. Actual processing times may
consume up to 100 additional cycles depending on the stack load and address
space used.
When using the arithmetic features of the 80C517 and 80C537, note that
operations involving the arithmetic processor are exclusive and may not be
interrupted. Do not use the arithmetic extensions in both the main program and
an interrupt service routine.
Use the following suggestions to help guarantee that only one thread of
execution uses the arithmetic processor:
!
Library Routines
The extra features of the 80C517 and 80C537 are used in several library routines
to enhance performance. These routines are listed below and are described in
detail in Chapter 8. Library Reference on page 175.
acos517
asin517
atan517
atof517
cos517
exp517
log10517
log517
printf517
scanf517
sin517
sprintf517
sqrt517
sscanf517
tan517
111
The C51 compiler must be set to avoid using LJMP and LCALL
instructions. This is accomplished using the ROM(SMALL) directive.
Note that the following restrictions apply when creating programs for the
8xC750, 8xC751, and 8xC752:
!
Stream functions such as printf and putchar may not be used. These
functions are usually not necessary for this chip because it is only equipped
with a maximum of 2 KBytes and has no serial interface.
The library file 80C751.LIB must be included in the input module list of the
linker. For example:
BL51 myprog.obj, startup751.obj, 80C751.LIB
112
113
Files you can alter to customize the startup procedures or run-time execution
of several library routines in your target program
Customization Files
The C51 compiler includes a number of source files you can modify to adapt
your target program to a specific hardware platform. These files contain: code
that is executed upon startup (STARTUP.A51), code that is used to initialize static
variables (INIT.A51), and code that is used to perform low-level stream I/O
(GETKEY.C and PUTCHAR.C). Source code for the memory allocation routines
is also included in the files CALLOC.C, FREE.C, INIT_MEM.C, MALLOC.C, and
REALLOC.C. All of these source files are described in detail in the sections that
follow.
The code contained in these files is already compiled or assembled and included
in the C library. When you link, the code from the library is automatically
included.
To include custom startup or initialization routines, you must include them in the
linker command line. The following example shows you how to include custom
replacement files for STARTUP.A51 and PUTCHAR.C.
BL51 MYMODUL1.OBJ, MYMODUL2.OBJ, STARTUP.OBJ, PUTCHAR.OBJ
114
STARTUP.A51
The STARTUP.A51 file contains the startup code for a C51 target program. This
source file is located in the LIB directory. Include a copy of this file in each
8051 project that needs custom startup code.
This code is executed immediately upon reset of the target system and optionally
performs the following operations, in order:
!
The STARTUP.A51 file provides you with assembly constants that you may
change to control the actions taken at startup. These are defined in the following
table.
Constant Name
Description
IDATALEN
XDATASTART
XDATALEN
PDATASTART
PDATALEN
IBPSTACK
Constant Name
Description
IBPSTACKTOP
Specifies the top start address of the small model reentrant stack
area. The default is 0xFF in idata memory.
115
C51 does not check to see if the stack area available satisfies the
requirements of the applications. It is your responsibility to perform
such a test.
XBPSTACK
XBPSTACKTOP
Specifies the top start address of the large model reentrant stack
area. The default is 0xFFFF in xdata memory.
C51 does not check to see if the available stack area satisfies the
requirements of the applications. It is your responsibility to perform
such a test.
PBPSTACK
PBPSTACKTOP
Specifies the top start address of the compact model reentrant stack
area. The default is 0xFF in pdata memory.
C51 does not check to see if the available stack area satisfies the
requirements of the applications. It is your responsibility to perform
such a test.
PPAGEENABLE
PPAGE
Specifies the value to write to Port 2 of the 8051 for pdata memory
access. This value represents the xdata memory page to use for
pdata. This is the upper 8 bits of the absolute address range to use
for pdata.
For example, if the pdata area begins at address 1000h (page 10h)
in the xdata memory, PPAGEENABLE should be set to 1, and
PPAGE should be set to 10h. The BL51 Linker/Locator must
contain a value between 1000h and 10FFh in the PDATA control
directive. For example:
116
SEGMENT
SEGMENT
RSEG
DS
CODE
IDATA
?STACK
1
?C_STARTUP:
117
?C_STARTUP
CSEG
LJMP
AT 0
STARTUP1
RSEG
?C_C51STARTUP
STARTUP1:
IF IDATALEN <> 0
MOV
CLR
IDATALOOP:
MOV
DJNZ
ENDIF
IF XDATALEN <> 0
MOV
MOV
IF (LOW (XDATALEN))
MOV
ELSE
MOV
ENDIF
CLR
XDATALOOP:
MOVX
INC
DJNZ
DJNZ
ENDIF
R0,#IDATALEN - 1
A
@R0,A
R0,IDATALOOP
DPTR,#XDATASTART
R7,#LOW (XDATALEN)
<> 0
R6,#(HIGH XDATALEN) +1
R6,#HIGH (XDATALEN)
A
@DPTR,A
DPTR
R7,XDATALOOP
R6,XDATALOOP
IF PPAGEENABLE <> 0
MOV
ENDIF
P2,#PPAGE
IF PDATALEN <> 0
MOV
MOV
CLR
PDATALOOP:
MOVX
INC
DJNZ
ENDIF
R0,#PDATASTART
R7,LOW (PDATALEN)
A
@R0,A
R0
R7,PDATALOOP
IF IBPSTACK <> 0
EXTRN DATA (?C_IBP)
MOV
?C_IBP,#LOW IBPSTACKTOP
ENDIF
IF XBPSTACK <> 0
EXTRN DATA (?C_XBP)
MOV
MOV
?C_XBP,#HIGH XBPSTACKTOP
?C_XBP+1,#LOW XBPSTACKTOP
ENDIF
IF PBPSTACK <> 0
EXTRN DATA (?C_PBP)
MOV
ENDIF
?C_PBP,#LOW PBPSTACKTOP
118
SP,#?STACK-1
?C_START
END
START751.A51
The START751.A51 file contains the startup code for a C51 target program that is
to run on the Signetics 8xC751 CPU. This source file is located in the LIB
directory. To use this file, follow the instructions on how to use STARTUP.A51 in
the previous section. The only difference between the two files is that
START751.A51 is specifically used for the 8xC751 which cannot access more
than 2 KBytes of code space and can access no external data memory. For these
reasons, there are no assembler constants that can affect xdata and pdata
memory.
The following is a listing of START751.A51.
119
;
; Stack Space for reentrant functions in the SMALL model.
IBPSTACK
EQU
0
; set to 1 if small reentrant is used.
IBPSTACKTOP EQU
0FFH+1
; set top of stack to highest location+1.
;
;------------------------------------------------------------------------NAME
?C_C51STARTUP
?STACK
?C_STARTUP
SEGMENT
SEGMENT
CODE
IDATA
RSEG
DS
?STACK
1
?C_STARTUP:
CSEG
AJMP
RSEG
AT
0
STARTUP1
?C_C51STARTUP
STARTUP1:
IF IDATALEN <> 0
MOV
CLR
IDATALOOP:
MOV
DJNZ
ENDIF
R0,#IDATALEN - 1
A
@R0,A
R0,IDATALOOP
IF IBPSTACK <> 0
EXTRN DATA (?C_IBP)
MOV
?C_IBP,#LOW IBPSTACKTOP
MOV
AJMP
SP,#?STACK-1
?C_START
ENDIF
END
120
INIT.A51
The INIT.A51 file contains the initialization routine for variables that were
explicitly initialized. If your system is equipped with a watchdog timer, you can
integrate a watchdog refresh into the initialization code using the watchdog
macro. This macro need be defined only if the initialization takes longer than
the watchdog cycle time. If you are using an 80515, the macro could be defined
as follows:
WATCHDOG
MACRO
SETB
SETB
ENDM
WDT
SWDT
121
INIT751.A51
The INIT751.A51 file contains the initialization routine for variables that were
explicitly initialized. Use this initialization routine for the Signetics 8xC751.
The following is a listing of the INIT751.A51 file.
;------------------------------------------------------------------------; This file is part of the C51 Compiler package
;
;------------------------------------------------------------------------; INIT751.A51: This code is executed, if the application program
;
contains initialized variables at file level.
;
; To translate this file use A51 with the following invocation:
;
;
A51 INIT751.A51
;
; To link the modified INIT.OBJ file to your application use the
; following BL51 invocation:
;
;
BL51 <your object file list>, INIT751.OBJ <controls>
;
;-------------------------------------------------------------------------
?C_C51STARTUP
?C_INITSEG
NAME
?C_INIT
SEGMENT
SEGMENT
CODE
CODE
INITEND:
RSEG
AJMP
?C_C51STARTUP
MAIN
CLR
MOVC
INC
MOV
CLR
MOVC
INC
MOV
INC
DJNZ
SJMP
CLR
MOVC
INC
MOV
ANL
ADD
A
A,@A+DPTR
DPTR
R0,A
A,#007H
A,#Table-LoadTab
IorPData:
IorPLoop:
Common:
Bits:
122
LoadTab:
SetIt:
BitReady:
XCH
CLR
RLC
SWAP
ANL
ORL
XCH
MOVC
JC
CPL
ANL
SJMP
ORL
MOV
DJNZ
SJMP
A,R0
C
A
A
A,#00FH
A,#20H
A,R0
A,@A+PC
SetIt
A
A,@R0
BitReady
A,@R0
@R0,A
R7,Bits
Loop
; Bit Address
; convert to Byte Address
Table:
DB
DB
DB
DB
DB
DB
DB
DB
?C_START:
LOOP:
MOV
CLR
MOV
MOVC
JZ
INC
MOV
ANL
JNB
ANL
MOV
CLR
MOVC
INC
JZ
INC
XCH
ANL
ADD
JZ
JC
SJMP
DPTR,#?C_INITSEGSTART
A
R6,#1
A,@A+DPTR
INITEND
DPTR
R7,A
A,#3FH
ACC.5,NOBIG
A,#01FH
R6,A
A
A,@A+DPTR
DPTR
NOBIG
R6
A,R7
A,#0C0H
; Typ is in Bit 6 and Bit 7
A,ACC
IorPDATA
Bits
$
RSEG
DB
?C_INITSEG
0
6
NOBIG:
END
00000001B
00000010B
00000100B
00001000B
00010000B
00100000B
01000000B
10000000B
123
PUTCHAR.C
This file contains the putchar function which is the low-level character output
routine for the stream I/O routines. All stream routines that output character data
do so through this routine. You may adapt this routine to your individual
hardware (for example, LCD or LED displays).
The default PUTCHAR.C file delivered with the C51 compiler outputs characters
via the serial interface. An XON/XOFF protocol is used for flow control.
Linefeed characters (\n) are automatically converted into carriage
return/linefeed sequences (\r\n).
GETKEY.C
This file contains the _getkey function which is the low-level character input
routine for the stream I/O routines. All stream routines that input character data
do so through this routine. You may adapt this routine to your individual
hardware (for example, for matrix keyboards). The default GETKEY.C file
delivered with the C51 compiler reads a character via the serial interface. No
data conversions are performed.
CALLOC.C
This file contains the source code for the calloc function. This routine allocates
memory for an array from the memory pool.
FREE.C
This file contains the source code for the free function. This routine returns a
previously allocated memory block to the memory pool.
INIT_MEM.C
This file contains the source code for the init_mempool function. This routine
allows you to specify the location and size of a memory pool from which
memory may be allocated using the malloc, calloc, and realloc functions.
124
MALLOC.C
This file contains the source code for the malloc function. This routine allocates
memory from the memory pool.
REALLOC.C
This file contains the source code for the realloc function. This routine resizes a
previously allocated memory block.
125
Optimizer
The C51 compiler is an optimizing compiler. This means that the compiler takes
certain steps to ensure that the code that is generated and output to the object file
is the most efficient (smaller and/or faster) code possible. The compiler analyzes
the generated code to produce more efficient instruction sequences. This ensures
that your C51 program runs as quickly as possible.
The C51 compiler provides six different levels of optimizing. Each increasing
level includes the optimizations of the levels below it.
Level
0
Description
Constant Folding: The compiler performs calculations that reduce expressions to
numeric constants, where possible. This includes calculations of run-time addresses.
Simple Access Optimizing: The compiler optimizes access of internal data and bit
addresses in the 8051 system.
Jump Optimizing: The compiler always extends jumps to the final target. Jumps to
jumps are deleted.
Dead Code Elimination: Unused code fragments and artifacts are eliminated.
Jump Negation: Conditional jumps are closely examined to see if they can be
streamlined or eliminated by the inversion of the test logic.
Data Overlaying: Data and bit segments suitable for static overlay are identified and
internally marked. The BL51 Linker/Locator has the capability, through global data
flow analysis, of selecting segments which can then be overlaid.
126
Level
6
Description
Loop Rotation: Program loops are rotated if the resulting program code is faster and
more efficient.
General Optimizations
Optimization
Description
Constant Folding
Jump Optimizing
Register Variables
8051-Specific Optimizations
Optimization
Description
Peephole Optimization
Data Overlaying
Case/Switch Optimizing
Description
OPTIMIZE(SIZE)
NOAREGS
Optimization
Description
NOREGPARMS
127
128
Segment Prefix
Data Type
Description
?PR?
code
?CO?
code
?XD?
xdata
?DT?
data
?ID?
idata
?BI?
bit
?BA?
bdata
?PD?
pdata
Data Objects
Data objects are the variables and constants you declare in your C programs.
C51 generates a separate segment for each memory type for which a variable is
declared. The following table lists the segment names generated for different
variable data objects.
Segment Name
Description
?CO?module_name
?XD?module_name
?DT?module_name
?ID?module_name
Segment Name
129
Description
?BI?module_name
bit objects
?BA?module_name
?PD?module_name
Program Objects
Program objects include the code generated for C program functions by the C51
compiler. Each function in a source module is assigned a separate code segment
using the ?PR?function_name?module_name naming convention. For example,
the function error_check in the file SAMPLE.C would result in a segment name
of ?PR?ERROR_CHECK?SAMPLE.
Segments are also created for local variables that are declared within the body of
a function. These segment names follow the above conventions and have a
different prefix depending upon the memory area in which the local variables are
stored.
Function arguments were historically passed using fixed memory locations. This
is still true for routines written in PL/M-51. However, C51 can pass up to 3
function arguments in registers. Other arguments are passed using the traditional
fixed memory areas. Memory space is reserved for all function arguments
regardless of whether or not some of these arguments may be passed in registers.
The parameter areas must be publicly known to any calling module. So, they are
publicly defined using the following segment names:
?function_name?BYTE
?function_name?BIT
For example, if func1 is a function that accepts both bit arguments as well as
arguments of other data types, the bit arguments are passed starting at
?FUNC1?BIT, and all other parameters are passed starting at ?FUNC1?BYTE.
Refer to Interfacing C Programs to Assembler on page 131 for examples of the
function argument segments.
Functions that have parameters, local variables, or bit variables contain all
additional segments for these variables. These segments can be overlaid by the
BL51 Linker/Locator.
130
Segment Type
Program code
code
?PR?function_name?module_name
Local variables
data
?DT?function_name?module_name
bit
?BI?function_name?module_name
Segment Name
Segment Type
Segment Name
Program code
code
?PR?function_name?module_name
Local variables
pdata
?PD?function_name?module_name
bit
?BI?function_name?module_name
Segment Name
Program code
code
?PR?function_name?module_name
Local variables
xdata
?XD?function_name?module_name
bit
?BI?function_name?module_name
Segment Type
The names for functions with register parameters and reentrant attributes are
modified slightly to avoid run-time errors. The following table lists deviations
from the standard segment names.
Declaration
Symbol
Description
FUNC
_FUNC1
_?FUNC2
131
Function Parameters
By default, C functions pass up to three parameters in registers. The remaining
parameters are passed in fixed memory locations. You may use the directive
NOREGPARMS to disable parameter passing in registers. Parameters are
passed in fixed memory locations if parameter passing in registers is disabled or
if there are too many parameters to fit in registers. Functions that pass
parameters in registers are flagged by C51 with an underscore character (_)
prefixed to the function name at code generation time. Functions that pass
parameters only in fixed memory locations are not prefixed with an underscore.
Refer to Using the SRC Directive on page 134 for an example.
132
long, float
generic ptr
R7
R6 & R7
(MSB in R6,
LSB in R7)
R4R7
R1R3
(Mem type in R3,
MSB in R2,
LSB in R1)
R5
R4 & R5
(MSB in R4,
LSB in R5)
R4R7
R1R3
(Mem type in R3,
MSB in R2,
LSB in R1)
R3
R2 & R3
(MSB in R2,
LSB in R3)
R1R3
(Mem type in R3,
MSB in R2,
LSB in R1)
The following examples clarify how registers are selected for parameter passing.
Declaration
Description
func1 (
int a)
func2
int
int
int
(
b,
c,
*d)
func3 (
long e,
long f)
The first argument, e, is passed in registers R4, R5, R6, and R7. The
second argument, f, cannot be located in registers since those available for
a second parameter with a type of long are already used by the first argument.
This parameter is passed using fixed memory locations.
func4 (
float g,
char h)
The first argument, g, passed in registers R4, R5, R6, and R7. The second
parameter, h, cannot be passed in registers and is passed in fixed memory
locations.
133
Register
Description
bit
Carry Flag
R7
R6 & R7
R4-R7
float
R4-R7
generic pointer
R1-R3
134
generates the following assembly output file when compiled using the SRC
directive.
; ASM1.SRC generated from: ASM1.C
NAME
ASM1
?PR?_asmfunc1?ASM1
SEGMENT CODE
PUBLIC _asmfunc1
; #pragma SRC
; #pragma SMALL
;
; unsigned int asmfunc1 (
RSEG
USING
?PR?_asmfunc1?ASM1
0
_asmfunc1:
;---- Variable 'arg?00' assigned to
; SOURCE LINE
; SOURCE LINE
; return (1 + arg);
; SOURCE LINE
MOV
A,R7
ADD
A,#01H
MOV
R7,A
CLR
A
ADDC A,R6
MOV
R6,A
; }
; SOURCE LINE
?C0001:
RET
; END OF _asmfunc1
# 8
END
In this example, note that the function name, asmfunc1, is prefixed with an
underscore character signifying that arguments are passed in registers. The arg
parameter is passed using R6 and R7.
135
The following example shows the assembly source generated for the same
function; however, register parameter passing has been disabled using the
NOREGPARMS directive.
; ASM2.SRC generated from: ASM2.C
NAME
ASM2
?PR?asmfunc1?ASM2
SEGMENT CODE
?DT?asmfunc1?ASM2
SEGMENT DATA
PUBLIC ?asmfunc1?BYTE
PUBLIC asmfunc1
RSEG ?DT?asmfunc1?ASM2
?asmfunc1?BYTE:
arg?00:
DS 2
; #pragma SRC
; #pragma SMALL
; #pragma NOREGPARMS
;
; unsigned int asmfunc1 (
RSEG
USING
?PR?asmfunc1?ASM2
0
asmfunc1:
; SOURCE LINE # 5
; SOURCE LINE # 7
; return (1 + arg);
MOV
ADD
MOV
CLR
ADDC
MOV
; SOURCE LINE # 8
A,arg?00+01H
A,#01H
R7,A
A
A,arg?00
R6,A
; }
; SOURCE LINE # 9
?C0001:
RET
; END OF asmfunc1
END
Note in this example that the function name, asmfunc1, is not prefixed with an
underscore character and that the arg parameter is passed in the
?asmfunc1?BYTE segment.
136
Register Usage
Assembler functions can change all register contents in the current selected
register bank as well as the contents of the registers ACC, B, DPTR, and PSW.
When invoking a C function from assembly, assume that these registers may be
destroyed by the C function that is called.
Overlaying Segments
If the overlay process is executed during program linking and locating, it is
important that each assembler subroutine have a unique program segment. This
is necessary so that during the overlay process, the references between the
functions are calculated using the references of the individual segments. The
data areas of the assembler subprograms may be included in the overlay analysis
when the following points are observed:
!
All segment names must be created using the C51 segment naming
conventions.
Each assembler function with local variables must be assigned its own data
segment. This data segment may be accessed by other functions only for
passing parameters. Parameters must be passed in order.
Example Routines
The following program examples show you how to pass parameters to and from
assembly routines. The following C functions are used in all of these examples:
int function (
int v_a,
char v_b,
bit v_c,
long v_d,
bit v_e);
/*
/*
/*
/*
/*
passed
passed
passed
passed
passed
in
in
in
in
in
R6 & R7 */
R5 */
fixed memory location */
fixed memory location */
fixed memory location */
137
(_function)
(?_function?BYTE)
(?_function?BIT)
R6,#HIGH intval
R7,#LOW intval
R7,#charconst
?_function?BIT+0
?_function?BYTE+3,longval+0
?_function?BYTE+4,longval+1
?_function?BYTE+5,longval+2
?_function?BYTE+6,longval+3
C,bitvalue
?_function?BIT+1,C
_function
intresult+0,R6
intresult+1,R7
;
;
;
;
;
;
;
;
int
int
char
bit
long
long
long
long
; bit
a
a
b
c
d
d
d
d
e
; store int
; retval
138
PUBLIC
SEGMENT
RSEG
?PD?FUNCTION?MODULE
?_function?BYTE:
v_a:
DS
2
v_b:
DS
1
v_d:
DS
4
.
.
.
RSEG
?BI?FUNCTION?MODULE
?_function?BIT:
v_c:
DBIT 1
v_e:
DBIT 1
.
.
.
RSEG
_function:
?PR?FUNCTION?MODULE
MOV v_a,R6
MOV v_a+1,R7
MOV v_b,R5
;
;
;
;
MOV
MOV
RET
; Return value
; int constant
; Return
Program segment
A function prolog and epilog is
not necessary. All variables can
immediately be accessed.
.
.
.
R6,#HIGH retval
R7,#LOW retval
139
(_function)
(?_function?BYTE)
(?_function?BIT)
R6,#HIGH intval
R7,#LOW intval
R5,#charconst
?_function?BIT+0
R0,#?_function?BYTE+3
A,longval+0
@R0,A
R0
A,longval+1
@R0,A
R0
A,longval+2
@R0,A
R0
A,longval+3
@R0,A
C,bitvalue
?_function?BIT+1,C
_function
intresult+0,R6
intresult+1,R7
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
int a
int a
char b
bit c
Addr of 'v_d' in the passing area
long d
Store parameter byte
Inc parameter passing address
long d
Store parameter byte
Inc parameter passing address
long d
Store parameter byte
Inc parameter passing address
long d
Store parameter byte
; bit
; Store int
; Retval
140
SEGMENT
; Name of
CODE
; Seg for
XDATA OVERLAYABLE
; Seg for
BIT
OVERLAYABLE
; Seg for
'function'
PUBLIC
RSEG
?PD?FUNCTION?MODULE
?_function?BYTE:
v_a:
DS
2
v_b:
DS
1
v_d:
DS
4
.
.
.
RSEG
?BI?FUNCTION?MODULE
?_function?BIT:
v_c:
DBIT 1
v_e:
DBIT 1
.
.
.
RSEG
_function:
?PR?FUNCTION?MODULE
MOV
R0,#?_function?BYTE+0
MOV
A,R6
MOVX @R0,A
INC
R0
MOV
A,R7
MOVX @R0,A
INC
R0
MOV
A,R5
MOVX @R0,A
;
;
;
;
;
;
Program segment
Special function prolog
and epilog is not
necessary. All
vars can immediately
be accessed
MOV
MOV
RET
; Return value
; int constant
; Return
.
.
.
R6,#HIGH retval
R7,#LOW retval
141
(_function)
(?_function?BYTE)
(?_function?BIT)
R6,#HIGH intval
R7,#LOW intval
R5,#charconst
?_function?BIT+0
R0,#?_function?BYTE+3
A,longval+0
@DPTR,A
DPTR
A,longval+1
@DPTR,A
DPTR
A,longval+2
@DPTR,A
DPTR
A,longval+3
@DPTR,A
C,bitvalue
?_function?BIT+1,C
_function
intresult+0,R6
intresult+1,R7
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
int a
int a
char b
bit c
Address of 'v_d' in the passing area
long d
Store parameter byte
Increment parameter passing address
long d
Store parameter byte
Increment parameter passing address
long d
Store parameter byte
Increment parameter passing address
long d
Store parameter byte
; bit
; Store int
; Retval
142
CODE
XDATA
?BI?FUNCTION?MODULE
BIT
PUBLIC
SEGMENT
RSEG
?XD?FUNCTION?MODULE
; Segment for local variables
?_function?BYTE:
; Start of the parameter passing seg
v_a:
DS
2
;
int variable: v_a
v_b:
DS
1
;
char variable: v_b
v_d:
DS
4
;
long variable: v_l
.
.; Additional local variables from 'function'
.
RSEG
?BI?FUNCTION?MODULE
?_function?BIT:
v_c:
DBIT
1
v_e:
DBIT
1
.
.
.
RSEG
_function:
?PR?FUNCTION?MODULE
MOV
DPTR,#?_function?BYTE+0
MOV
A,R6
MOVX @DPTR,A
INC
R0
MOV
A,R7
MOVX @DPTR,A
INC
R0
MOV
A,R5
MOVX @DPTR,A
;
;
;
;
;
;
Program segment
Special function prolog
and epilog is not
necessary. All vars
can immediately be
accessed.
MOV
MOV
RET
; Return value
; int constant
; Return
.
.
.
R6,#HIGH retval
R7,#LOW retval
143
Parameters and return values of PL/M-51 functions may be any of the following
types: bit, char, unsigned char, int, and unsigned int. Other types, including
long, float, and all types of pointers, can be declared in C functions with the
alien type specifier. However, use these types with care because PL/M-51 does
not directly support 32-bit binary integers or floating-point numbers.
PL/M-51 does not support variable-length argument lists. Therefore, functions
declared using the alien type specifier must have a fixed number of arguments.
The ellipsis notation used for variable-length argument lists is not allowed for
alien functions and causes C51 to generate an error message. For example:
extern alien unsigned int
144
Bits
Bytes
Value Range
bit
0 to 1
signed char
-128 to +127
unsigned char
0 to 255
enum
16
-32768 to +32767
signed short
16
-32768 to +32767
unsigned short
16
0 to 65535
signed int
16
-32768 to +32767
unsigned int
16
0 to 65535
signed long
32
-2147483648 to 2147483647
unsigned long
32
0 to 4294967295
float
32
1.175494E-38 to 3.402823E+38
0x00 to 0xFF
code*, xdata *
16
0x0000 to 0xFFFF
generic pointer
24
Other data types, like structures and unions, may contain scalars from this table.
All elements of these data types are allocated sequentially and are byte-aligned
due to the 8-bit architecture of the 8051 family.
Bit Variables
Scalars of type bit are stored using a single bit. Pointers to and arrays of bit are
not allowed. Bit objects are always located in the bit-addressable internal
memory space of the 8051 CPU. The BL51 Linker/Locator overlays bit objects
if possible.
145
+0
+1
Contents
0x12
0x34
+0
+1
+2
+3
Contents
0x12
0x34
0x56
0x78
146
Generic Pointers
Generic pointers have no declared explicit memory type. They may point to any
memory area on the 8051. These pointers are stored using 3 bytes (24 bits). The
first byte contains a value that indicates the memory area or memory type. The
remaining two bytes contain the address offset with the high-order byte first.
The following memory format is used:
Address
+0
+1
+2
Contents
Memory Type
The memory type byte may have one of the following values:
Memory Type
xdata
pdata
code
0x00
0x01
0xFE
0xFF
Value
Use of any other memory type values may lead to unpredictable program
behavior.
The following example shows the memory storage of a generic pointer that
references address 0x1234 in the xdata memory area.
Address
+0
+1
+2
Contents
0x01
0x12
0x34
147
Floating-point Numbers
Scalars of type float are stored using 4 bytes (32 bits). The format used
corresponds to that of the IEEE-754 standard.
There are two components of a floating-point number: the mantissa and the
exponent. The mantissa stores the actual digits of the number. The exponent
stores the power to which the mantissa must be raised. The exponent is an 8-bit
value in the 0 to 255 range and is stored relative to 127. The actual value of the
exponent is calculated by subtracting 127 from the stored value (0 to 255). The
value of the exponent can be anywhere from +128 to -127. The mantissa is a
24-bit value whose most significant bit (MSB) is always 1 and is, therefore, not
stored. There is also a sign bit which indicates if the floating-point number is
positive or negative.
Floating-point numbers are stored in 8051 memory using the following format:
Address
+0
+1
+2
+3
Contents
SEEE EEEE
EMMM MMMM
MMMM MMMM
MMMM MMMM
where:
S
Using the above format, the floating-point number -12.5 would be stored as a
hexadecimal value of 0xC1480000. In memory, this appears as follows:
Address
+0
+1
+2
+3
Contents
0xC1
0x48
0x00
0x00
148
+0
+1
+2
+3
Format
SEEEEEEE
EMMMMMMM
MMMMMMMM
MMMMMMMM
Binary
11000001
01001000
00000000
00000000
C1
48
00
00
Hex
10010000000000000000000
There is an understood decimal point at the left of the mantissa that is always
preceded by a 1. This digit is not stored in the hexadecimal representation of
the floating-point number. Adding 1 and the decimal point to the beginning of
the mantissa gives the following:
1.10010000000000000000000
Now, adjust the mantissa for the exponent. A negative exponent moves the
decimal point to the left. A positive exponent moves the decimal point to the
right. Because the exponent is 3, the mantissa is adjusted as follows:
1100.10000000000000000000
The result is now a binary floating-point number. Binary digits left of the
decimal point represent the power of two corresponding to the position: 1100
represents (1 23) + (1 22) + (0 21) + (0 20) which equals 12.
Binary digits that are right of the decimal point also represent the power of two
corresponding to their position. However, the powers are negative: .100
represents (1 2-1) + (0 2-2) + (0 2-3) + which equals .5.
Adding these values together gives 12.5 which must be negated since the sign
bit is set. So, the floating-point hexadecimal value 0xC1480000 is -12.5.
149
Floating-point Errors
The 8051 does not contain an interrupt vector to trap floating-point errors;
therefore, your software must appropriately respond to these error conditions. In
addition to the normal floating-point values, a floating-point number may contain
a binary error value. These values are defined as a part of the IEEE standard and
are used whenever an error occurs during normal processing of floating-point
operations. Your code should check for possible arithmetic errors at the end of
each floating-point operation.
Name
Value
Meaning
NaN
0xFFFFFFF
Not a number
+INF
0x7F80000
-INF
0xFF80000
NOTE
The C51 library function _chkfloat_ lets you quickly check floating-point status.
You can use the following union to store floating-point values.
union f {
float
f;
unsigned long ul;
};
/* Floating-point value */
/* Unsigned long value */
This union contains a float and an unsigned long in order to perform floatingpoint math operations and to respond to the IEEE error states. For example:
#define NaN
#define plusINF
#define minusINF
0xFFFFFFFF
0x7F800000
0xFF800000
union f {
float
unsigned long
};
f;
ul;
/* Floating-point value */
/* Unsigned long value */
x.f = a * b;
if (x.ul == NaN || x.ul == plusINF
/* handle the error */
}
else {
/* result is correct */
}
}
|| x.ul == minusINF)
150
XBYTE
CWORD
DWORD
PWORD
XWORD
Refer to Absolute Memory Access Macros on page 178 for definitions of these
macros.
151
The C51 compiler generates an object file for ALMCTRL.C and includes a
segment for variables in the xdata memory area. Because it is the only variable
declared in this module, alarm_control is the only variable in that segment.
The name of the segment is ?XD?ALMCTRL.
The BL51 Linker/Locator allows you to specify the base address of any segment
by using the location control directives. Because the alarm_control variable
was declared to reside in xdata, the XDATA BL51 directive must be used as
follows:
BL51 almctrl.obj XDATA(?XD?ALMCTRL(2000h))
152
void ) {
= (void *) 0;
= 0x1234;
= 'a';
153
Debugging
C51 uses the Intel Object Format (OMF-51) for object files and generates
complete symbol information. All Intel compatible emulators may be used for
program debugging. The DEBUG control directive embeds debugging
information in the object file. In addition, the OBJECTEXTEND control
directive embeds additional variable type information in the object file which
allows type-specific display of variables and structures when using certain
emulators.
155
Fatal Errors
Fatal errors cause immediate termination of the compilation. These errors
normally occur as the result of invalid options specified on the command line.
Fatal errors are also generated when the compiler cannot access a specified
source include file.
Fatal error messages conform to one of the following formats:
C51 FATAL-ERROR ACTION:
LINE:
ERROR:
C51 TERMINATED.
<current action>
<line in which the error is detected>
<corresponding error message>
<current action>
<file in which the error is detected>
<corresponding error message>
The following are descriptions of the possible text for the Action and Error
fields in the above messages.
156
Actions
ALLOCATING MEMORY
The compiler could not allocate enough memory to compile the specified
source file.
CREATING LIST-FILE / OBJECT-FILE / WORKFILE
The compiler could not create the list file, object file, or work file. This error
may occur if the disk is full or write-protected, or if the file already exists and
is read only.
GENERATING INTERMEDIATE CODE
The source file contains a function that is too large to be translated into
pseudo-code by the compiler. Try breaking the function into smaller
functions and re-compiling.
OPENING INPUT-FILE
The compiler failed to find or open the selected source or include file.
PARSING INVOKE-/#PRAGMA-LINE
The source file contains too many external references. Reduce the number of
external variables and functions accessed by the source file.
WRITING TO FILE
An error was encountered while writing to the list file, object file, or work
file.
157
Errors
'(' AFTER CONTROL EXPECTED
This message indicates that the right parenthesis of the enclosed argument is
missing.
BAD DIGIT IN NUMBER
An error occurred while writing to the list, preprint, work, or object file
because of insufficient disk space.
IDENTIFIER EXPECTED
The compiler could not allocate enough memory to compile the specified
source file. If you receive this message consistently, you should break the
source file into two or more smaller files and re-compile. Alternatively, you
can add more memory to your PC because the C51 compiler uses a DOS
extender to utilize all extended memory available.
MORE THAN 100 ERRORS IN SOURCE-FILE
During the compilation more than 100 errors were detected. This causes the
termination of the compiler.
MORE THAN 256 SEGMENTS/EXTERNALS
More than 256 total references were encountered in a source file. A single
source file cannot contain more than 256 functions or external references.
This is a historical restriction mandated by the Intel Object Module Format
158
The parse stack has overflowed. This can occur if the source program
contains extremely complex expressions or if blocks are nested more than 31
levels deep.
PREPROCESSOR: LINE TOO LONG (32K)
During macro expansion the stack consumption of the preprocessor grew too
large to continue. This message usually indicates a recursive macro
definition, but can also indicate a macro with too many levels of nesting.
RESPECIFIED OR CONFLICTING CONTROL
The source and include files must exist on either a hard disk or diskette. The
console CON:, :CI:, or similar devices are not allowed as input files.
UNKNOWN CONTROL
159
where:
number
line
file
error message
The following table lists syntax and semantic errors by error number. The error
message displayed is listed along with a brief description and possible cause and
correction.
Number
100
101
Unclosed string
A string is not terminated with a quote (").
102
103
125
160
Number
126
Type-stack underflow
The type declaration stack has underflowed. This error is usually a side-effect of
error 125.
127
129
130
131
Duplicate function-parameter
A formal parameter name exists more than once within a function. The formal
parameter names must be unique in function declarations.
132
134
135
136
void on variable
The type void is allowed only as a non-existent return value or an empty argument
list for functions (void func (void)), or in combination with a pointer (void *).
138
140
141
142
Number
161
143
144
145
146
Undeclared sfr
Invalid sfr
The declaration of an absolute bit (base^pos) contains an invalid
base-specification. The base must be the name of a previously declared sfr. Any
other names are invalid.
147
149
150
151
152
153
154
Ptr to field
Pointers to bit-fields are not valid types.
155
156
157
158
159
160
void invalid
The void type is legal only in combination with pointers or as the non-existent
return value of a function.
161
162
Number
162
Duplicate function-parameter
The name of a defined object inside a function duplicates the name of a
parameter.
163
164
Ptr to nul
This error is usually the result of a previous error for a pointer declaration.
165
Ptr to bit
The type combination pointer to bit is not a legal type.
166
Array of functions
Arrays cannot contain functions; however, they may contain pointers to functions.
167
Array of fields
Bit-fields may not be arranged as arrays.
168
Array of bit
An array may not have type bit as its basic type. This limitation is imposed by the
architecture of the 8051.
169
170
171
172
173
Missing return-expression
A function which returns a value of any type but int, must contain a return
statement including an expression. Because of compatibility to older programs, no
check is done on functions which return an int value.
174
Return-expression on void-function
A void function cannot return a value and thus may not contain a return
statement.
175
176
177
Different struct/union
Different types of structures are used in an assignment or as an argument to a
function.
178
179
Number
163
180
181
Incompatible operand
At least one operand type is not valid with the given operator (for example,
~float_type).
183
Unmodifiable lvalue
The object to be changed resides in code memory or has const attribute and
therefore cannot be modified.
184
185
186
Invalid dereference
This error message may be caused by an internal compiler problem. Please
contact technical support if this error is repeated.
187
Not an lvalue
The needed argument must be the address of an object that can be modified.
188
189
190
193
193
193
193
Illegal op-type(s)
Illegal add/sub on ptr
Illegal operation on bit(s)
Bad operand type
This error results when an expression uses illegal operand-types with the given
operator. Examples of invalid expressions are bit * bit, ptr + ptr, or ptr * anything.
The error message includes the operator which caused the error.
The following operations may be executed with bit-type operands:
!
Assignment (=)
Negation (~)
bit operands may be used in expressions with other data types. In this case a
type cast is automatically performed.
194
195
* illegal indirection
The * operator may not be applied on non-pointer arguments.
164
Number
196
198
199
200
201
202
Undefined identifier
The given identifier is undefined.
203
204
Undefined member
The given member name in a struct or union reference is undefined.
205
207
208
209
210
211
212
213
214
215
Number
165
216
217
Non-integral index
The dimension expression of an array must be of the type char, unsigned char,
int, or unsigned int. All other types are illegal.
218
219
220
221
222
223
Div by zero
Mod by zero
The compiler detected a division or a modulo by zero.
225
226
227
228
229
230
231
Redefinition
The specified name is already defined and cannot be redefined.
232
Duplicate label
The specified label is already defined.
233
Undefined label
This message indicates a label that was accessed but was not defined.
Sometimes this message appears several lines after the actual label reference.
This is caused by the method used to search for undefined labels.
234
235
166
Number
236
237
238
239
Duplicate member
Duplicate parameter
An attempt was made to define an already defined struct member or function
parameter.
240
241
128 bytes
256 bytes
65535 bytes
242
243
244
245
246
Floating-point error
This error occurs when a floating-point argument lies outside of the valid range for
32-bit floating values. The numeric range of the 32-bit IEEE values is:
1.175494E-38 to 3.402823E+38.
247
Non-address/constant initializer
A valid initializer expression must evaluate to a constant value or the name of an
object plus or minus a constant.
248
249
250
251
252
Number
253
167
255
256
257
258
259
260
Pointer truncation
A spaced pointer has been assigned some constant value which exceeds the
range covered by the pointers memory space. For example:
char idata *p1 = 0x1234;
/* result is 0x34 */
168
Number
261
262
263
264
265
267
268
271
Number
169
272
273
274
278
279
Multiple initialization
An attempt has been made to initialize some object more than once.
300
Unterminated comment
This message occurs when a comment does not have a closing delimiter (*/).
301
Identifier expected
The syntax of a preprocessor directive expects an identifier.
302
Misused # operator
This message occurs if the stringize operator # is not followed by an identifier.
303
304
305
306
307
308
309
310
170
Number
311
312
313
_ _DATE_ _
_ _STDC_ _
_ _FILE_ _
_ _TIME_ _
_ _MODEL_ _
314
315
316
Unterminated conditionals
The number of endifs does not match the number of if or ifdefs after the end of the
input file.
318
319
320
User_error_text
This error number is reserved for errors introduced with the #error directive of the
preprocessor. The #error directive causes the user error text to come up with error
320 which counts like some other error and prevents the compiler from generating
code.
321
Missing <character>
In the filename argument of an include directive, the closing character is missing.
For example: #include <stdio.h
325
326
327
171
Warnings
Warnings produce information about potential problems which may occur during
the execution of the resulting program. Warnings do not hinder compilation of
the source file.
Warnings produce a message in the list file. These warning messages are in the
following format:
*** WARNING number IN LINE line OF file: warning message
where:
number
line
file
warning message
The following table lists warnings by number. The warning message displayed
is listed along with a brief description and possible cause and correction.
Number
173
Missing return-expression
A function which returns a value of any type but int, must contain a return
statement including an expression. Because of compatibility to older programs, no
check is done on functions which return an int value.
182
185
196
198
172
Number
206
209
219
245
258
259
260
Pointer truncation
This error or warning occurs when converting a pointer to a pointer with a smaller
offset area. The conversion takes place, but the offset of the larger pointer is
truncated to fit into the smaller pointer.
261
265
Number
271
173
275
276
/* dead expression */
/* result is not used */
277
/* mspace xdata */
/* mspace collision */
280
Unreferenced symbol/label
This message identifies a symbol or label which has been defined but not used.
307
317
_ _DATE_ _
_ _STDC_ _
_ _FILE_ _
_ _TIME_ _
_ _MODEL_ _
322
Unknown identifier
The identifier in an #if directive line is undefined (evaluates to FALSE).
323
foo
174
Number
324
175
Intrinsic Routines
The C51 compiler supports a number of intrinsic library functions. Non-intrinsic
functions generate ACALL or LCALL instructions to perform the library
routine. Intrinsic functions generate in-line code to perform the library routine.
The generated in-line code is much faster and more efficient than a called routine
would be. The following functions are available in intrinsic form:
_crol_
_cror_
_irol_
_iror_
_lrol_
_lror_
_nop_
_testbit_
176
Library Files
The C51 library includes six different compile-time libraries which are
optimized for various functional requirements. These libraries support most of
the ANSI C function calls.
Library File
Description
C51S.LIB
C51FPS.LIB
C51C.LIB
C51FPC.LIB
C51L.LIB
C51FPL.LIB
80C751.LIB
Several library modules are provided in source code form. These routines are
used to perform low-level hardware-related I/O for the stream I/O functions.
You can find the source for these routines in the LIB directory. You may
modify these source files and substitute them for the library routines. By using
these routines, you can quickly adapt the library to perform (using any hardware
I/O device available in your target) stream I/O. Refer to Stream Input and
Output on page 187 for more information.
177
Standard Types
The C51 standard library contains definitions for a number of standard types
which may be used by the library routines. These standard types are declared in
include files which you may access from your C programs.
jmp_buf
The jmp_buf type is defined in SETJMP.H and specifies the buffer used by the
setjmp and longjmp routines to save and restore the program environment. The
jmp_buf type is defined as follows:
#define _JBLEN
7
typedef char jmp_buf[_JBLEN];
va_list
The va_list array type is defined in STDARG.H. This type holds data required by
the va_arg and va_end routines. The va_list type is defined as follows:
typedef char *va_list;
178
CBYTE
The CBYTE macro allows you to access individual bytes in the program
memory of the 8051 and is defined as follows:
#define CBYTE ((unsigned char volatile code *)0)
CWORD
The CWORD macro allows you to access individual words in the program
memory of the 8051 and is defined as follows:
#define CWORD ((unsigned int volatile code *) 0)
NOTE
The index used with this macro does not represent the memory address of the
integer value. To obtain the memory address, you must multiply the index by the
sizeof an integer (2 bytes).
179
DBYTE
The DBYTE macro allows you to access individual bytes in the internal data
memory of the 8051 and is defined as follows:
#define DBYTE ((unsigned char volatile idata *) 0)
to read or write the contents of the byte in internal data memory at address
0002h.
DWORD
The DWORD macro allows you to access individual words in the internal data
memory of the 8051 and is defined as follows:
#define DWORD ((unsigned int volatile idata *) 0)
to read or write the contents of the word in internal data memory at address
0004h (2 sizeof (unsigned int) = 4).
NOTE
The index used with this macro does not represent the memory address of the
integer value. To obtain the memory address, you must multiply the index by the
sizeof an integer (2 bytes).
180
PBYTE
The PBYTE macro allows you to access individual bytes in one page of the
external data memory of the 8051 and is defined as follows:
#define PBYTE ((unsigned char volatile pdata *) 0)
to read or write the contents of the byte in pdata memory at address 0002h.
PWORD
The PWORD macro allows you to access individual words in one page of the
external data memory of the 8051 and is defined as follows:
#define PWORD ((unsigned int volatile pdata*) 0)
to read or write the contents of the word in pdata memory at address 0004h
(2 sizeof (unsigned int) = 4).
NOTE
The index used with this macro does not represent the memory address of the
integer value. To obtain the memory address, you must multiply the index by the
sizeof an integer (2 bytes).
181
XBYTE
The XBYTE macro allows you to access individual bytes in the external data
memory of the 8051 and is defined as follows:
#define XBYTE ((unsigned char volatile xdata*) 0)
to read or write the contents of the byte in external data memory at address
0002h.
XWORD
The XWORD macro allows you to access individual words in the external data
memory of the 8051 and is defined as follows:
#define XWORD ((unsigned int volatile xdata*) 0)
to read or write the contents of the word in external data memory at address
0004h (2 sizeof (unsigned int) = 4).
NOTE
The index used with this macro does not represent the memory address of the
integer value. To obtain the memory address, you must multiply the index by the
sizeof an integer (2 bytes).
182
Routines by Category
This sections gives a brief overview of the major categories of routines available
in the C51 standard library. Refer to Reference on page 195 for a complete
description of routine syntax and usage.
NOTE
Many of the routines in the C51 standard library are reentrant, intrinsic, or
both. These specifications are listed under attributes in the following tables.
Unless otherwise noted, routines are non-reentrant and non-intrinsic.
Buffer Manipulation
Routine
Attributes
Description
Copies data bytes from one buffer to another until a
specified character or specified number of characters has
been copied.
memccpy
memchr
reentrant
memcmp
reentrant
memcpy
reentrant
memmove
reentrant
memset
reentrant
183
Attributes
Description
isalnum
reentrant
isalpha
reentrant
iscntrl
reentrant
isdigit
reentrant
isgraph
reentrant
islower
reentrant
isprint
reentrant
ispunct
reentrant
isspace
reentrant
isupper
reentrant
isxdigit
reentrant
toascii
reentrant
toint
reentrant
tolower
reentrant
_tolower
reentrant
toupper
reentrant
_toupper
reentrant
The character conversion and classification routines allow you to test individual
characters for a variety of attributes and convert characters to different formats.
The _tolower, _toupper, and toascii routines are implemented as macros. All
other routines are implemented as functions. All macro definitions and function
prototypes are found in the CTYPE.H include file.
184
Data Conversion
Routine
Attributes
Description
abs
reentrant
atof / atof517
atoi
atol
cabs
reentrant
labs
reentrant
Math
Routine
Attributes
acos / acos517
asin / asin517
atan / atan517
atan2
ceil
cos / cos517
cosh
exp / exp517
fabs
Description
reentrant
floor
log / log517
log10 / log10517
modf
pow
rand
reentrant
sin / sin517
sinh
srand
Routine
Attributes
185
Description
sqrt / sqrt517
tan / tan517
tanh
_chkfloat_
intrinsic,
reentrant
_crol_
intrinsic,
reentrant
_cror_
intrinsic,
reentrant
_irol_
intrinsic,
reentrant
_iror_
intrinsic,
reentrant
_lrol_
intrinsic,
reentrant
_lror_
intrinsic,
reentrant
186
Memory Allocation
Routine
Attributes
Description
calloc
free
init_mempool
malloc
realloc
The memory allocation functions provide you with a means to specify, allocate,
and free blocks of memory from a memory pool. All memory allocation
functions are implemented as functions and are prototyped in the STDLIB.H
include file.
Before using any of these functions to allocate memory, you must first specify,
using the init_mempool routine, the location and size of a memory pool from
which subsequent memory requests are satisfied.
The calloc and malloc routines allocate blocks of memory from the pool. The
calloc routine allocates an array with a specified number of elements of a given
size and initializes the array to 0. The malloc routine allocates a specified
number of bytes.
The realloc routine changes the size of an allocated block, while the free routine
returns a previously allocated memory block to the memory pool.
187
Attributes
Description
getchar
reentrant
_getkey
gets
printf / printf517
putchar
puts
reentrant
scanf / scanf517
sprintf / sprintf517
sscanf / sscanf517
ungetchar
vprintf
vsprintf
The stream input and output routines allow you to read and write data to and
from the 8051 serial interface or a user-defined I/O interface. The default
_getkey and putchar functions found in the C51 library read and write
characters using the 8051 serial interface. You can find the source for these
functions in the LIB directory. You may modify these source files and
substitute them for the library routines. When this is done, other stream
functions then perform input and output using the new _getkey and putchar
routines.
If you want to use the existing _getkey and putchar functions, you must first
initialize the 8051 serial port. If the serial port is not properly initialized, the
default stream functions do not function. Initializing the serial port requires
manipulating special function registers SFRs of the 8051. The include file
REG51.H contains definitions for the required SFRs.
188
The following example code must be executed immediately after reset, before
any stream functions are invoked.
.
.
.
#include <reg51.h>
.
.
.
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFD;
TR1 = 1;
/* Start timer 1 */
TI = 1;
.
.
.
The stream routines treat input and output as streams of individual characters.
There are routines that process characters as well as functions that process
strings. Choose the routines that best suit your requirements.
All of these routines are implemented as functions. Most are prototyped in the
STDIO.H include file. The printf517, scanf517, sprintf517, and sscanf517
functions are prototyped in the 80C517.H include file.
189
String Manipulation
Routine
Attributes
Description
Concatenates two strings.
strcat
strchr
reentrant
strcmp
reentrant
strcpy
reentrant
strcspn
strlen
reentrant
strncat
strncmp
strncpy
strpbrk
strpos
reentrant
strrchr
reentrant
strrpbrk
strrpos
reentrant
strspn
The string routines are implemented as functions and are prototyped in the
STRING.H include file. They perform the following operations:
!
Copying strings
190
Attributes
Description
va_arg
reentrant
va_end
reentrant
va_start
reentrant
The variable-length argument list routines are implemented as macros and are
defined in the STDARG.H include file. These routines provide you with a
portable method of accessing arguments in a function that takes a variable
number of arguments. These macros conform to the ANSI C Standard for
variable-length argument lists.
Miscellaneous
Routine
Attributes
Description
setjmp
reentrant
longjmp
reentrant
_nop_
intrinsic,
reentrant
_testbit_
intrinsic,
reentrant
Routines found in the miscellaneous category do not fit easily into any other
library routine category. The setjmp and longjmp routines are implemented as
functions and are prototyped in the STDJMP.H include file.
The _nop_ and _testbit_ routines are used to direct the compiler to generate a
NOP instruction and a JBC instruction respectively. These routines are
prototyped in the INTRINS.H include file.
191
Include Files
The include files that are provided with the C51 standard library are found in the
INC subdirectory. These files contain constant and macro definitions, type
definitions, and function prototypes. The following sections describe the use
and contents of each include file. Macros and functions included in the file are
listed as well.
REG51.H
REG515.H
REG515A.H
REG515C.H
REG517.H
REG517A.H
REG51F.H
REG51G.H
REG51GB.H
REG52.H
REG552.H
REG592.H
REG781.H
80C517.H
The 80C517.H include file contains routines that use the enhanced operational
features of the 80C517 CPU and its derivatives. These routines are:
acos517
asin517
atan517
atof517
cos517
exp517
log10517
log517
printf517
scanf517
sin517
sprintf517
sqrt517
sscanf517
tan517
ABSACC.H
The ABSACC.H include file contains definitions for macros that allow you to
directly access the different memory areas of the 8051.
CBYTE
CWORD
DBYTE
DWORD
PBYTE
PWORD
XBYTE
XWORD
192
ASSERT.H
The ASSERT.H include file defines the assert macro you can use to create test
conditions in your programs.
CTYPE.H
The CTYPE.H include file contains definitions and prototypes for routines
which classify ASCII characters and routines which perform character
conversions. The following is a list of these routines:
isalnum
isalpha
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
isxdigit
toascii
toint
tolower
_tolower
toupper
_toupper
INTRINS.H
The INTRINS.H include file contains prototypes for routines that instruct the
compiler to generate in-line intrinsic code.
_chkfloat_
_crol_
_cror_
_irol_
_iror_
_lrol_
_lror_
_nop_
_testbit_
MATH.H
The MATH.H include file contains prototypes and definitions for all routines
that perform floating-point math calculations. Other math functions are also
included in this file. All of the math function routines are listed below:
abs
acos
asin
atan
atan2
cabs
ceil
cos
cosh
exp
fabs
floor
fprestore
fpsave
labs
log
log10
modf
pow
sin
sinh
sqrt
tan
tanh
193
SETJMP.H
The SETJMP.H include file defines the jmp_buf type and prototypes the setjmp
and longjmp routines which use it.
STDARG.H
The STDARG.H include file defines macros that allow you to access arguments in
functions with variable-length argument lists. The macros include:
va_arg
va_end
va_start
STDDEF.H
The STDDEF.H include file defines the offsetof macro you can use to determine
the offset of members of a structure.
STDIO.H
The STDIO.H include file contains prototypes and definitions for stream I/O
routines. They are:
getchar
_getkey
gets
printf
putchar
puts
scanf
sprintf
sscanf
ungetchar
vprintf
vsprintf
The STDIO.H include file also defines the EOF manifest constant.
194
STDLIB.H
The STDLIB.H include file contains prototypes and definitions for the type
conversion and memory allocation routines listed below:
atof
atoi
atol
calloc
free
init_mempool
malloc
rand
realloc
srand
The STDLIB.H include file also defines the NULL manifest constant.
STRING.H
The STRING.H include file contains prototypes for the following string and
buffer manipulation routines:
memccpy
memchr
memcmp
memcpy
memmove
memset
strcat
strchr
strcmp
strcpy
strcspn
strlen
strncat
strncmp
strncpy
strpbrk
strpos
strrchr
strrpbrk
strrpos
strspn
The STRING.H include file also defines the NULL manifest constant.
195
Reference
The following pages constitute the C51 standard library reference. The routines
included in the standard library are described here in alphabetical order and each
is divided into several sections:
Summary:
Description:
Return Value:
See Also:
Example:
196
abs
Summary:
#include <math.h>
int abs (
int val);
of */
Description:
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_abs (void)
int x;
int y;
/* for printf */
{
x = -42;
y = abs (x);
printf ("ABS(%d) = %d\n", x, y);
}
197
acos / acos517
Summary:
Description:
#include <math.h>
float acos (
float x);
cosine of */
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_acos (void)
float x;
float y;
/* for printf */
{
198
asin / asin517
Summary:
Description:
#include <math.h>
float asin (
float x);
of */
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_asin (void)
float x;
float y;
/* for printf */
{
199
assert
Summary:
#include <assert.h>
void assert (
expression);
Description:
Return Value:
None.
Example:
#include <assert.h>
#include <stdio.h>
void check_parms (
char *string)
{
assert (string != NULL);
/* check for NULL ptr */
printf ("String %s is OK\n", string);
}
200
atan / atan517
Summary:
Description:
#include <math.h>
float atan (
float x);
tangent of */
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_atan (void)
float x;
float y;
/* for printf */
{
201
atan2
Summary:
#include <math.h>
float atan2 (
/* denominator for arc tangent */
float y,
/* numerator for arc tangent */
float x);
Description:
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_atan2 ()
float x;
float y;
float z;
/* for printf */
{
x = -1.0;
for (y = -10.0; y < 10.0; y += 0.1)
z = atan2 (y,x);
202
atof / atof517
Summary:
Description:
#include <stdlib.h>
float atof (
void *string);
/* string to convert */
where:
digits
Return Value:
See Also:
atoi, atol
Example:
#include <stdlib.h>
#include <stdio.h>
void tst_atof (void) {
float f;
char s [] = "1.23";
f = atof (s);
printf ("ATOF(%s) = %f\n", s, f);
}
/* for printf */
203
atoi
Summary:
Description:
#include <stdlib.h>
int atoi (
void *string);
/* string to convert */
| -}" digits
where:
digits
Return Value:
See Also:
atof, atol
Example:
#include <stdlib.h>
#include <stdio.h>
/* for printf */
204
atol
Summary:
Description:
#include <stdlib.h>
long atol (
void *string);
/* string to convert */
| -}" digits
where:
digits
Return Value:
See Also:
atof, atoi
Example:
#include <stdlib.h>
#include <stdio.h>
void tst_atol (void) {
long l;
char s [] = "8003488051";
l = atol (s);
printf ("ATOL(%s) = %ld\n", s, l);
}
/* for printf */
205
cabs
Summary:
#include <math.h>
char cabs (
/* character to take absolute value of */
char val);
Description:
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_cabs (void)
char x;
char y;
/* for printf */
{
x = -23;
y = cabs (x);
printf ("CABS(%bd) = %bd\n", x, y);
}
206
calloc
Summary:
Description:
#include <stdlib.h>
void *calloc (
unsigned int num,
unsigned int len);
/* number of items */
/* length of each item */
Return Value:
See Also:
Example:
#include <stdlib.h>
#include <stdio.h>
void tst_calloc (void)
int xdata *p;
/* for printf */
{
/* ptr to array of 100 ints */
207
ceil
Summary:
#include <math.h>
float ceil (
/* number to calculate ceiling for */
float val);
Description:
Return Value:
See Also:
floor
Example:
#include <math.h>
#include <stdio.h>
void tst_ceil (void)
float x;
float y;
/* for printf */
{
x = 45.998;
y = ceil (x);
printf ("CEIL(%f) = %f\n", x, y);
/* output is "CEIL(45.998) = 46" */
}
208
_chkfloat_
Summary:
#include <intrins.h>
unsigned char _chkfloat_ (
/* number for error checking */
float val);
Description:
Return Value:
Example:
Meaning
Floating-point value 0
#include <intrins.h>
#include <stdio.h>
/* for printf */
break;
break;
break;
break;
break;
209
cos / cos517
Summary:
Description:
#include <math.h>
float cos (
float x);
for */
Return Value:
See Also:
sin, tan
Example:
#include <math.h>
#include <stdio.h>
void tst_cos (void)
float x;
float y;
/* for printf */
{
210
cosh
Summary:
#include <math.h>
float cosh (
float x);
function */
Description:
Return Value:
See Also:
sinh, tanh
Example:
#include <math.h>
#include <stdio.h>
void tst_cosh (void)
float x;
float y;
/* for printf */
{
211
_crol_
Summary:
#include <intrins.h>
unsigned char _crol_ (
unsigned char c,
unsigned char b);
Description:
The _crol_ routine rotates the bit pattern for the character c
left b bits. This routine is implemented as an intrinsic
function. The code required is included in-line rather than
being called.
Return Value:
See Also:
Example:
#include <intrins.h>
void tst_crol (void)
char a;
char b;
a = 0xA5;
b = _crol_(a,3);
/* b now is 0x2D */
212
_cror_
Summary:
#include <intrins.h>
unsigned char _cror_ (
unsigned char c,
unsigned char b);
Description:
The _cror_ routine rotates the bit pattern for the character c
right b bits. This routine is implemented as an intrinsic
function. The code required is included in-line rather than
being called.
Return Value:
See Also:
Example:
#include <intrins.h>
void tst_cror (void)
char a;
char b;
a = 0xA5;
b = _crol_(a,1);
}
/* b now is 0xD2 */
213
exp / exp517
Summary:
Description:
#include <math.h>
float exp (
float x);
*/
Return Value:
See Also:
log, log10
Example:
#include <math.h>
#include <stdio.h>
void tst_exp (void)
float x;
float y;
/* for printf */
{
x = 4.605170186;
y = exp (x);
/* y = 100 */
214
fabs
Summary:
#include <math.h>
float fabs (
/* number to calc absolute value for */
float val);
Description:
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_fabs (void)
float x;
float y;
/* for printf */
{
x = 10.2;
y = fabs (x);
printf ("FABS(%f) = %f\n", x, y);
x = -3.6;
y = fabs (x);
printf ("FABS(%f) = %f\n", x, y);
}
215
floor
Summary:
#include <math.h>
float floor (
/* value for floor function */
float val);
Description:
Return Value:
See Also:
ceil
Example:
#include <math.h>
#include <stdio.h>
void tst_floor (void)
float x;
float y;
/* for printf */
{
x = 45.998;
y = floor (x);
printf ("FLOOR(%f) = %f\n", x, y);
/* prints 45 */
216
free
Summary:
Description:
#include <stdlib.h>
void free (
void xdata *p);
/* block to free */
Return Value:
None.
See Also:
Example:
#include <stdlib.h>
#include <stdio.h>
void tst_free (void)
void *mbuf;
/* for printf */
{
217
getchar
Summary:
#include <stdio.h>
char getchar (void);
Description:
Return Value:
See Also:
Example:
#include <stdio.h>
void tst_getchar (void)
char c;
218
_getkey
Summary:
#include <stdio.h>
char _getkey (void);
Description:
Return Value:
See Also:
Example:
#include <stdio.h>
void tst_getkey (void)
char c;
219
gets
Summary:
#include <stdio.h>
char *gets (
char *string, /* string to read */
/* maximum characters to read
int len);
*/
Description:
Return Value:
See Also:
Example:
#include <stdio.h>
void tst_gets (void) {
xdata char buf [100];
do {
gets (buf, sizeof (buf));
printf ("Input string \"%s\"", buf);
} while (buf [0] != '\0');
}
220
init_mempool
Summary:
Description:
#include <stdlib.h>
void init_mempool (
void xdata *p,
unsigned int size);
Return Value:
None.
See Also:
Example:
#include <stdlib.h>
void tst_init_mempool (void)
xdata void *p;
int i;
221
_irol_
Summary:
#include <intrins.h>
unsigned int _irol_ (
unsigned int i,
unsigned char b);
Description:
The _irol_ routine rotates the bit pattern for the integer i
left b bits. This routine is implemented as an intrinsic
function. The code required is included in-line rather than
being called.
Return Value:
See Also:
Example:
#include <intrins.h>
void tst_irol (void)
int a;
int b;
a = 0xA5A5;
b = _irol_(a,3);
/* b now is 0x2D2D */
222
_iror_
Summary:
#include <intrins.h>
unsigned int _iror_ (
unsigned int i,
unsigned char b);
Description:
The _iror_ routine rotates the bit pattern for the integer i
right b bits. This routine is implemented as an intrinsic
function. The code required is included in-line rather than
being called.
Return Value:
See Also:
Example:
#include <intrins.h>
void tst_iror (void)
int a;
int b;
a = 0xA5A5;
b = _irol_(a,1);
}
/* b now is 0xD2D2 */
223
isalnum
Summary:
#include <ctype.h>
bit isalnum (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_isalnum (void)
unsigned char i;
char *p;
/* for printf */
{
224
isalpha
Summary:
#include <ctype.h>
bit isalpha (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_isalpha (void)
unsigned char i;
char *p;
/* for printf */
{
225
iscntrl
Summary:
#include <ctype.h>
bit iscntrl (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_iscntrl (void)
unsigned char i;
char *p;
/* for printf */
{
226
isdigit
Summary:
#include <ctype.h>
bit isdigit (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_isdigit (void)
unsigned char i;
char *p;
/* for printf */
{
227
isgraph
Summary:
#include <ctype.h>
bit isgraph (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_isgraph (void)
unsigned char i;
char *p;
/* for printf */
{
228
islower
Summary:
#include <ctype.h>
bit islower (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_islower (void)
unsigned char i;
char *p;
/* for printf */
{
229
isprint
Summary:
#include <ctype.h>
bit isprint (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_isprint (void)
unsigned char i;
char *p;
/* for printf */
{
230
ispunct
Summary:
Description:
#include <ctype.h>
bit ispunct (
char c);
/* character to test */
"
*
<
^
#
+
=
_
$
,
>
`
%
?
{
&
.
@
|
'
/
[
}
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_ispunct (void)
unsigned char i;
char *p;
/* for printf */
{
(
:
\
~
231
isspace
Summary:
#include <ctype.h>
bit isspace (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_isspace (void)
unsigned char i;
char *p;
/* for printf */
{
232
isupper
Summary:
#include <ctype.h>
bit isupper (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_isupper (void)
unsigned char i;
char *p;
/* for printf */
{
233
isxdigit
Summary:
#include <ctype.h>
bit isxdigit (
char c);
/* character to test */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_isxdigit (void)
unsigned char i;
char *p;
/* for printf */
{
234
labs
Summary:
#include <math.h>
long labs (
/* value to calc. abs. value for */
long val);
Description:
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_labs (void)
long x;
long y;
/* for printf */
{
x = -12345L;
y = labs (x);
printf ("LABS(%ld) = %ld\n", x, y);
}
235
log / log517
Summary:
#include <math.h>
float log (
/* value to take natural logarithm of */
float val);
Description:
Return Value:
See Also:
exp, log10
Example:
#include <math.h>
#include <stdio.h>
void tst_log (void)
float x;
float y;
/* for printf */
{
x = 2.71838;
x *= x;
y = log (x);
/* y = 2 */
236
log10 / log10517
Summary:
#include <math.h>
float log10 (
/* value to take common logarithm of */
float val);
Description:
Return Value:
See Also:
exp, log
Example:
#include <math.h>
#include <stdio.h>
void tst_log10 (void)
float x;
float y;
/* for printf */
{
x = 1000;
y = log10 (x);
printf ("LOG10(%f) = %f\n", x, y);
}
/* y = 3 */
237
longjmp
Summary:
#include <setjmp.h>
void longjmp (
jmp_buf env, /* environment to restore */
/* return value */
int retval);
Description:
Return Value:
None.
See Also:
setjmp
238
Example:
#include <setjmp.h>
#include <stdio.h>
jmp_buf env;
bit error_flag;
/* for printf */
/* force an error */
239
_lrol_
Summary:
#include <intrins.h>
unsigned long _lrol_ (
unsigned long l,
unsigned char b);
Description:
The _lrol_ routine rotates the bit pattern for the long integer
l left b bits. This routine is implemented as an intrinsic
function. The code required is included in-line rather than
being called.
Return Value:
See Also:
Example:
#include <intrins.h>
void tst_lrol (void)
long a;
long b;
a = 0xA5A5A5A5;
b = _lrol_(a,3);
/* b now is 0x2D2D2D2D */
240
_lror_
Summary:
#include <intrins.h>
unsigned long _lror_ (
unsigned long l,
unsigned char b);
Description:
The _lror_ routine rotates the bit pattern for the long integer
l right b bits. This routine is implemented as an intrinsic
function. The code required is included in-line rather than
being called.
Return Value:
See Also:
Example:
#include <intrins.h>
void tst_lror (void)
long a;
long b;
a = 0xA5A5A5A5;
b = _lrol_(a,1);
}
/* b now is 0xD2D2D2D2 */
241
malloc
Summary:
Description:
#include <stdlib.h>
void *malloc (
unsigned int size);
Return Value:
See Also:
Example:
#include <stdlib.h>
#include <stdio.h>
/* for printf */
if (p == NULL)
printf ("Not enough memory space\n");
else
printf ("Memory allocated\n");
}
242
memccpy
Summary:
#include <string.h>
void *memccpy (
/* destination buffer */
void *dest,
/* source buffer */
void *src,
/* character which ends copy */
char c,
/* maximum bytes to copy */
int len);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
243
memchr
Summary:
#include <string.h>
void *memchr (
/* buffer to search */
void *buf,
/* byte to find */
char c,
/* maximum buffer length */
int len);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
244
memcmp
Summary:
#include <string.h>
char memcmp (
/* first buffer */
void *buf1,
/* second buffer */
void *buf2,
/* maximum bytes to compare
int len);
*/
Description:
Meaning
<0
=0
>0
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
else
printf ("hexchars == hexchars2\n");
}
245
memcpy
Summary:
#include <string.h>
void *memcpy (
/* destination buffer */
void *dest,
/* source buffer */
void *src,
/* maximum bytes to copy */
int len);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
246
memmove
Summary:
#include <string.h>
void *memmove (
/* destination buffer */
void *dest,
/* source buffer */
void *src,
/* maximum bytes to move */
int len);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
"
"
";
= %s\n", buf);
247
memset
Summary:
#include <string.h>
void *memset (
/* buffer to initialize */
void *buf,
/* byte value to set */
char c,
/* buffer length */
int len);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
void tst_memset (void)
char buf [10];
/* for printf */
{
248
modf
Summary:
#include <math.h>
float modf (
/* value to calculate modulo for */
float val,
/* integer portion of modulo */
float *ip);
Description:
Return Value:
Example:
#include <math.h>
#include <stdio.h>
/* for printf */
249
_nop_
Summary:
#include <intrins.h>
void _nop_ (void);
Description:
Return Value:
None.
Example:
#include <intrins.h>
#include <stdio.h>
void tst_nop (void)
/* for printf */
{
P1 = 0xFF;
_nop_ ();
_nop_ ();
_nop_ ();
P1 = 0x00;
}
250
offsetof
Summary:
#include <stddef.h>
int offsetof (
structure,
member);
/* structure to use */
/* member to get offset for */
Description:
Return Value:
Example:
#include <stddef.h>
struct index_st
{
unsigned char type;
unsigned long num;
unsigned ing len;
};
typedef struct index_st index_t;
void main (void)
{
int x, y;
x = offsetof (struct index_st, len);
y = offsetof (index_t, num);
}
/* x = 5 */
/* x = 1 */
251
pow
Summary:
#include <math.h>
float pow (
/* value to use for base */
float x,
/* value to use for exponent */
float y);
Description:
Return Value:
See Also:
sqrt
Example:
#include <math.h>
#include <stdio.h>
void tst_pow (void)
float base;
float power;
float y;
/* for printf */
{
base = 2.0;
power = 8.0;
y = pow (base, power);
/* y = 256 */
252
printf / printf517
Summary:
Description:
#include <stdio.h>
int printf (
const char *fmtstr
!, arguments");
/* format string */
/* additional arguments */
253
Argument Type
Output Format
Signed decimal number
int
unsigned int
unsigned int
unsigned int
unsigned int
float
float
float
float
float
char
Single character
generic *
generic *
254
Meaning
blank ( )
#
255
e, E
g, G
c, p
256
NOTE
This function is implementation-specific and is based on the
operation of the putchar function. This function, as
provided in the standard library, writes characters using the
serial port of the 8051. Custom functions may use other I/O
devices.
You must ensure that the argument type matches that of the
format specification. You can use type casts to ensure that
the proper type is passed to printf.
The total number of bytes that may be passed to printf is
limited due to the memory restrictions imposed by the 8051.
A maximum of 15 bytes may be passed in small model or
compact model. A maximum of 40 bytes may be passed in
large model.
Return Value:
See Also:
Example:
257
#include <stdio.h>
void tst_printf (void) {
char a;
int b;
long c;
unsigned char x;
unsigned int y;
unsigned long z;
float f,g;
char buf [] = "Test String";
char *p = buf;
a
b
c
x
y
z
f
g
=
=
=
=
=
=
=
=
1;
12365;
0x7FFFFFFF;
'A';
54321;
0x4A6F6E00;
10.0;
22.95;
printf
printf
printf
printf
printf
printf
258
putchar
Summary:
Description:
#include <stdio.h>
char putchar (
char c);
/* character to output */
Return Value:
See Also:
Example:
#include <stdio.h>
void tst_putchar (void)
unsigned char i;
259
puts
Summary:
Description:
#include <stdio.h>
int puts (
const char *string);
/* string to output */
Return Value:
See Also:
Example:
#include <stdio.h>
void tst_puts (void)
260
rand
Summary:
#include <stdlib.h>
int rand (void);
Description:
Return Value:
See Also:
srand
Example:
#include <stdlib.h>
#include <stdio.h>
void tst_rand (void)
int i;
int r;
/* for printf */
{
261
realloc
Summary:
Description:
#include <stdlib.h>
void *realloc (
void xdata *p,
unsigned int size);
Return Value:
See Also:
Example:
#include <stdlib.h>
#include <stdio.h>
void tst_realloc (void)
void xdata *p;
void xdata *new_p;
/* for printf */
{
p = malloc (100);
if (p != NULL) {
new_p = realloc (p, 200);
if (new_p != NULL) p = new_p;
else printf ("Reallocation failed\n");
}
}
262
scanf
Summary:
Description:
#include <stdio.h>
int scanf (
const char *fmtstr
!, argument");
/* format string */
/* additional arguments */
263
Argument Type
Input Format
int *
int *
unsigned int *
264
Character
Argument Type
Input Format
unsigned int *
unsigned int *
float *
Floating-point number
float *
Floating-point number
float *
Floating-point number
char *
A single character
char *
265
Return Value:
See Also:
Example:
#include <stdio.h>
void tst_scanf (void)
char a;
int b;
long c;
unsigned char x;
unsigned int y;
unsigned long z;
float f,g;
char d, buf [10];
int argsread;
printf ("Enter a signed byte, int, and long\n");
argsread = scanf ("%bd %d %ld", &a, &b, &c);
printf ("%d arguments read\n", argsread);
printf ("Enter an unsigned byte, int, and long\n");
argsread = scanf ("%bu %u %lu", &x, &y, &z);
printf ("%d arguments read\n", argsread);
printf ("Enter a character and a string\n");
argsread = scanf ("%c %9s", &d, buf);
printf ("%d arguments read\n", argsread);
printf ("Enter two floating-point numbers\n");
argsread = scanf ("%f %f", &f, &g);
printf ("%d arguments read\n", argsread);
}
266
setjmp
Summary:
Description:
#include <setjmp.h>
int setjmp (
jmp_buf env);
/* current environment */
Return Value:
See Also:
longjmp
Example:
See longjmp
267
sin / sin517
Summary:
Description:
#include <math.h>
float sin (
float x);
sine for */
/* value to calculate
Return Value:
See Also:
cos, tan
Example:
#include <math.h>
#include <stdio.h>
void tst_sin (void)
float x;
float y;
/* for printf */
{
268
sinh
Summary:
#include <math.h>
float sinh (
/* value to calc hyperbolic sine for */
float val);
Description:
Return Value:
See Also:
cosh, tanh
Example:
#include <math.h>
#include <stdio.h>
void tst_sinh (void)
float x;
float y;
/* for printf */
{
269
sprintf / sprintf517
Summary:
Description:
#include <stdio.h>
int sprintf (
char *buffer,
const char *fmtstr
!, argument");
/* storage buffer */
/* format string */
/* additional arguments */
Return Value:
See Also:
270
Example:
#include <stdio.h>
void tst_sprintf (void)
char buf [100];
int n;
int a,b;
float pi;
a = 123;
b = 456;
pi = 3.14159;
n = sprintf (buf,
"%f\n", 1.1);
n += sprintf (buf+n, "%d\n", a);
n += sprintf (buf+n, "%d %s %g", b, "---", pi);
printf (buf);
}
271
sqrt / sqrt517
Summary:
Description:
#include <math.h>
float sqrt (
float x);
of */
Return Value:
See Also:
Example:
#include <math.h>
#include <stdio.h>
void tst_sqrt (void)
float x;
float y;
/* for printf */
{
x = 25.0;
y = sqrt (x);
/* y = 5 */
272
srand
Summary:
#include <stdlib.h>
void srand (
/* random number generator seed */
int seed);
Description:
The srand function sets the starting value seed used by the
pseudo-random number generator in the rand function. The
random number generator produces the same sequence of
pseudo-random numbers for any given value of seed.
Return Value:
None.
See Also:
rand
Example:
#include <stdlib.h>
#include <stdio.h>
void tst_srand (void)
int i;
int r;
/* for printf */
{
srand (56);
for (i = 0; i < 10; i++) {
printf ("I = %d, RAND = %d\n", i, rand ());
}
}
273
sscanf / sscanf517
Summary:
Description:
#include <stdio.h>
int sscanf (
char *buffer,
const char *fmtstr
!, argument");
The sscanf function reads data from the string buffer. Data
input are stored in the locations specified by argument
according to the format string fmtstr. Each argument must
be a pointer to a variable that corresponds to the type
defined in fmtstr which controls the interpretation of the
input data. The fmtstr argument is composed of one or
more whitespace characters, non-whitespace characters, and
format specifications, as defined in the scanf function
description. Refer to scanf on page 262 for a complete
description of the formation string and additional arguments.
The sscanf517 function is identical to sscanf, but uses the
arithmetic unit of the Siemens 80C517 to provide faster
execution. When using this function include the header file
80C517.H. Do not use this routine with a CPU that does not
support this feature.
NOTE
The total number of bytes that may be passed to sscanf is
limited due to the memory restrictions imposed by the 8051.
A maximum of 15 bytes may be passed in small model or
compact model. A maximum of 40 bytes may be passed in
large model.
Return Value:
See Also:
274
Example:
#include <stdio.h>
void tst_sscanf (void)
char a;
int b;
long c;
unsigned char x;
unsigned int y;
unsigned long z;
float f,g;
char d, buf [10];
int argsread;
printf ("Reading a signed byte, int,and long\n");
argsread = sscanf ("1 -234 567890",
"%bd %d %ld", &a, &b, &c);
printf ("%d arguments read\n", argsread);
printf ("Reading an unsigned byte, int, and long\n");
argsread = sscanf ("2 44 98765432",
"%bu %u %lu", &x, &y, &z);
printf ("%d arguments read\n", argsread);
printf ("Reading a character and a string\n");
argsread = sscanf ("a abcdefg", "%c %9s", &d, buf);
printf ("%d arguments read\n", argsread);
printf ("Reading two floating-point numbers\n");
argsread = sscanf ("12.5 25.0", "%f %f", &f, &g);
printf ("%d arguments read\n", argsread);
}
275
strcat
Summary:
#include <string.h>
char *strcat (
/* destination string */
char *dest,
/* source string */
char *src);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
276
strchr
Summary:
#include <string.h>
char *strchr (
const char *string,
char c);
/* string to search */
/* character to find */
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
void tst_strchr (void) {
char *s;
char buf [] = "This is a test";
s = strchr (buf, 't');
if (s != NULL)
printf ("found a 't' at %s\n", s);
}
/* for printf */
277
strcmp
Summary:
#include <string.h>
char strcmp (
char *string1,
char *string2);
/* first string */
/* second string */
Description:
Return Value:
Meaning
<0
=0
>0
See Also:
memcmp, strncmp
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
278
strcpy
Summary:
#include <string.h>
char *strcpy (
/* destination string */
char *dest,
/* source string */
char *src);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
void tst_strcpy (void) {
char buf [21];
char s [] = "Test String";
strcpy (buf, s);
strcat (buf, " #2");
printf ("new string is %s\n", buf);
}
/* for printf */
279
strcspn
Summary:
#include <string.h>
int strcspn (
/* source string */
char *src,
/* characters to find */
char *set);
Description:
The strcspn function searches the src string for any of the
characters in the set string.
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
280
strlen
Summary:
#include <string.h>
int strlen (
/* source string */
char *src);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
281
strncat
Summary:
#include <string.h>
char *strncat (
/* destination string */
char *dest,
/* source string */
char *src,
/* max. chars to concatenate */
int len);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
void tst_strncat (void)
char buf [21];
/* for printf */
{
282
strncmp
Summary:
#include <string.h>
char strncmp (
char *string1,
char *string2,
int len);
compare */
/* first string */
/* second string */
/* max characters to
Description:
Return Value:
Meaning
<0
=0
>0
See Also:
memcmp, strcmp
Example:
#include <string.h>
#include <stdio.h>
void tst_strncmp (void) {
char str1 [] = "Wrodanahan
char str2 [] = "Wrodanaugh
/* for printf */
T.J.";
J.W.";
char i;
i = strncmp (str1, str2, 15);
if (i < 0)
else if (i > 0)
else
}
283
strncpy
Summary:
#include <string.h>
char *strncpy (
char *dest,
char *src,
int len);
copy */
/* destination string */
/* source string */
/* max characters to
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
void tst_strncpy ( char *s)
char buf [21];
/* for printf */
{
284
strpbrk
Summary:
#include <string.h>
char *strpbrk (
char *string, /* string to search */
/* characters to find */
char *set);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
285
strpos
Summary:
#include <string.h>
int strpos (
const char *string,
char c);
/* string to search */
/* character to find */
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
286
strrchr
Summary:
#include <string.h>
char *strrchr (
const char *string,
char c);
/* string to search */
/* character to find */
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
287
strrpbrk
Summary:
#include <string.h>
char *strrpbrk (
char *string, /* string to search */
/* characters to find */
char *set);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
/* for printf */
288
strrpos
Summary:
#include <string.h>
int strrpos (
const char *string,
char c);
/* string to search */
/* character to find */
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
void tst_strrpos (
/* for printf */
char *s)
int i;
i = strpos (s, ' ');
if (i == -1)
printf ("No spaces found in %s\n", s);
else
printf ("Last space in %s is at offset %d\n",
s, i);
}
289
strspn
Summary:
#include <string.h>
int strspn (
char *string, /* string to search */
/* characters to allow */
char *set);
Description:
Return Value:
See Also:
Example:
#include <string.h>
#include <stdio.h>
void tst_strspn ( char *digit_str)
char octd [] = "01234567";
int i;
/* for printf */
{
290
tan / tan517
Summary:
Description:
#include <math.h>
float tan (
float x);
*/
Return Value:
See Also:
cos, sin
Example:
#include <math.h>
#include <stdio.h>
void tst_tan (void)
float x, y, pi;
/* for printf */
{
pi = 3.14159;
for (x = -(pi/4); x < (pi/4); x += 0.1)
y = tan (x);
printf ("TAN(%f) = %f\n", x, y);
}
}
291
tanh
Summary:
#include <math.h>
float tanh (
float x);
tangent for */
Description:
Return Value:
See Also:
cosh, sinh
Example:
#include <math.h>
#include <stdio.h>
void tst_tanh (void)
float x;
float y;
float pi;
/* for printf */
{
pi = 3.14159;
for (x = -(pi/4); x < (pi/4); x += 0.1)
y = tanh (x);
printf ("TANH(%f) = %f\n", x, y);
}
292
_testbit_
Summary:
#include <intrins.h>
bit _testbit_ (
* bit to test and clear */
bit b);
Description:
Return Value:
Example:
#include <intrins.h>
#include <stdio.h>
void tst_testbit (void){
bit test_flag;
if (_testbit_ (test_flag))
printf ("Bit was set\n");
else
printf ("Bit was clear\n");
}
/* for printf */
293
toascii
Summary:
#include <ctype.h>
char toascii (
char c);
/* character to convert */
Description:
Return Value:
See Also:
toint
Example:
#include <ctype.h>
#include <stdio.h>
void tst_toascii ( char c)
char k;
/* for printf */
{
k = toascii (c);
printf ("%c is an ASCII character\n", k);
}
294
toint
Summary:
#include <ctype.h>
char toint (
char c);
/* digit to convert */
Description:
Return Value:
See Also:
toascii
Example:
#include <ctype.h>
#include <stdio.h>
void tst_toint (void)
unsigned long l;
char k;
/* for printf */
{
295
tolower
Summary:
#include <ctype.h>
char tolower (
char c);
/* character to convert */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_tolower (void)
unsigned char i;
/* for printf */
{
296
_tolower
Summary:
#include <ctype.h>
char _tolower (
char c);
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst__tolower (
if (isupper (k))
}
/* character to convert */
/* for printf */
char k)
k = _tolower (k);
297
toupper
Summary:
#include <ctype.h>
char toupper (
char c);
/* character to convert */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst_toupper (void)
unsigned char i;
/* for printf */
{
298
_toupper
Summary:
#include <ctype.h>
char _toupper (
char c);
/* character to convert */
Description:
Return Value:
See Also:
Example:
#include <ctype.h>
#include <stdio.h>
void tst__toupper ( char k) {
if (islower (k)) k = _toupper (k);
}
/* for printf */
299
ungetchar
Summary:
#include <stdio.h>
char ungetchar (
char c);
/* character to unget */
Description:
Return Value:
See Also:
Example:
#include <stdio.h>
void tst_ungetchar (void)
char k;
300
va_arg
Summary:
#include <stdarg.h>
type va_arg (
argptr,
/* optional argument list */
type);
/* type of next argument */
Description:
Return Value:
See Also:
va_end, va_start
Example:
301
#include <stdarg.h>
#include <stdio.h>
int varfunc (char *buf, int id, ...)
va_list tag;
/* for printf */
{
if (id == 0) {
int arg1;
char *arg2;
long arg3;
arg1 = va_arg (tag, int);
arg2 = va_arg (tag, char *);
arg3 = va_arg (tag, long);
}
else {
char *arg1;
char *arg2;
long arg3;
arg1 = va_arg (tag, char *);
arg2 = va_arg (tag, char *);
arg3 = va_arg (tag, long);
}
}
302
va_end
Summary:
#include <stdarg.h>
void va_end (
argptr);
/* optional argument list */
Description:
Return Value:
None.
See Also:
va_arg, va_start
Example:
See va_arg.
303
va_start
Summary:
#include <stdarg.h>
void va_start (
argptr,
/* optional argument list */
prevparm);
/* arg preceding optional args */
Description:
Return Value:
None.
See Also:
va_arg, va_end
Example:
See va_arg.
304
vprintf
Summary:
Description:
#include <stdio.h>
void vprintf (
const char * fmtstr,
char * argptr);
Return Value:
See Also:
Example:
305
#include <stdio.h>
#include <stdarg.h>
void error (char *fmt, ...)
va_list arg_ptr;
va_start (arg_ptr, fmt);
vprintf (fmt, arg_ptr);
va_end (arg_ptr);
/* format string */
}
void tst_vprintf (void) {
int i;
i = 1000;
/* call error with one parameter */
error ("Error: '%d' number too large\n", i);
/* call error with just a format string */
error ("Syntax Error\n");
}
306
vsprintf
Summary:
Description:
#include <stdio.h>
void vsprintf (
char *buffer,
const char * fmtstr,
char * argptr);
Return Value:
See Also:
Example:
307
#include <stdio.h>
#include <stdarg.h>
xdata char etxt[30];
/* text buffer */
/* format string */
}
void tst_vprintf (void)
int i;
i = 1000;
309
Compiler-related Differences
!
Wide Characters
Wide 16-bit characters are not supported by C51. ANSI provides wide
characters for future support of an international character set.
Library-related Differences
The ANSI C Standard Library includes a vast number of routines, most of which
are included in C51. Many, however, are not applicable to an embedded
application and are excluded from the C51 library.
The following ANSI Standard library routines are included in the C51 library:
abs
acos
asin
atan
atan2
atof
atoi
atol
calloc
ceil
cos
cosh
exp
fabs
floor
free
getchar
gets
isalnum
isalpha
iscntrl
isdigit
isgraph
islower
isprint
ispunct
isspace
isupper
isxdigit
labs
log
log10
longjmp
310
malloc
memchr
memcmp
memcpy
memmove
memset
modf
pow
printf
putchar
puts
rand
realloc
scanf
setjmp
sin
sinh
sprintf
sqrt
srand
sscanf
strcat
strchr
strcmp
strcpy
strcspn
strlen
strncat
strncmp
strncpy
strpbrk
strrchr
strspn
tan
tanh
tolower
toupper
va_arg
va_end
va_start
vprintf
vsprintf
The following ANSI Standard library routines are not included in the C51
library:
abort
asctime
atexit
bsearch
clearerr
clock
ctime
difftime
div
exit
fclose
feof
ferror
fflush
fgetc
fgetpos
fgets
fmod
fopen
fprintf
fputc
fputs
fread
freopen
frexp
fscanf
fseek
fsetpos
ftell
fwrite
getc
getenv
gmtime
ldexp
ldiv
localeconv
localtime
mblen
mbstowcs
mbtowc
mktime
perror
putc
qsort
raise
remove
rename
rewind
setbuf
setlocale
setvbuf
signal
strcoll
strerror
strftime
strstr
strtod
strtok
strtol
strtoul
strxfrm
system
time
tmpfile
tmpnam
ungetc
vfprintf
wcstombs
wctomb
311
The following routines are not found in the ANSI Standard Library but are
included in the C51 library.
acos517
asin517
atan517
atof517
cabs
_chkfloat_
cos517
_crol_
_cror_
exp517
_getkey
init_mempool
_irol_
_iror_
log10517
log517
_lrol_
_lror_
memccpy
_nop_
printf517
scanf517
sin517
sprintf517
sqrt517
sscanf517
strpos
strrpbrk
strrpos
tan517
_testbit_
toascii
toint
_tolower
_toupper
ungetchar
312
313
Version 4 Differences
!
FLOATFUZZY Directive
C51 now supports the FLOATFUZZY directive. This directive controls the
number of bits ignored during the execution of a floating-point compare.
Refer to FLOATFUZZY on page 23 for more information.
Memory Types
The memory types have been changed to achieve better performance in the
run-time library and to reflect the memory map of the MCS 251 architecture.
314
idata
data
bdata
xdata
pdata
code
C51 V5 Value
0x00
0x00
0x00
0x01
0xFE
0xFF
C51 V4 Value
0x01
0x04
0x04
0x02
0x03
0x05
WARNINGLEVEL Directive
C51 now supports the WARNINGLEVEL directive which lets you specify
the strength of the warning detection for the C51 compiler. The C51
compiler now also checks for unused local variables, labels, and expressions.
Refer to WARNINGLEVEL on page 55 for more information.
315
_at_Keyword
C51 supports variable location using the _at_ keyword. This new keyword
allows you to specify the address of a variable in a declaration. Refer to The
_at_ Keyword on page 152 for more information.
NOAMAKE Directive
C51 now supports the NOAMAKE directive. This directive causes C51 to
generate object modules without project information and register
optimization records. This is necessary only if you want to use object files
with older versions of C51 tools. Refer to NOAMAKE on page 35 for
more information.
Optimizer Level 6
C51 now supports optimizer level 6 which provides loop rotation. The
resulting code is more efficient and executes faster. Refer to OPTIMIZE
on page 39 for more information.
ORDER Directive
When you specify the ORDER directive, C51 locates variables in memory in
the order in which they are declared in your source file. Refer to ORDER
on page 42 for more information.
REGFILE Directive
C51 now supports the REGFILE directive which lets you specify the name
of the register definition file generated by the linker. This file contains
information that is used to optimize the use of registers between functions in
different modules. Refer to REGFILE on page 47 for more information.
316
Trigraphs
C51 now supports trigraph sequences.
317
318
Version 2 Differences
Intrinsic Functions
Intrinsic functions have been added to the library to support some of the
special instructions built in to the 8051.
319
320
321
Memory Model
The most significant impact on code size and execution speed is memory model.
Compiling in small model always generates the smallest, fastest code possible.
The SMALL control directive instructs the C51 compiler to use the small
memory model. In small model, all variables, unless declared otherwise, reside
in the internal memory of the 8051. Memory access to internal data memory is
fast (typically performed in 1 or 2 clock cycles), and the generated code is much
smaller than that generated with the compact or large models. For example, the
following loop:
for (i = 0; i < 100; i++)
do_nothing ();
}
is compiled both in small model and in large model to demonstrate the difference
in generated code. The following is the small model translation:
stmt level
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0000
0001
0003
0003
0005
0006
source
#pragma small
void do_nothing (void);
1
1
1
for (i = 0; i < 100; i++)
1
{
2
do_nothing ();
2
}
1
}
; FUNCTION func (BEGIN)
; SOURCE LINE # 10
E4
CLR
A
F500
R
MOV
i,A
?C0001:
E500
R
MOV
A,i
C3
CLR
C
9464
SUBB A,#064H
322
000D 0500
000F 80F2
0011
0011 22
JNC
?C0004
; SOURCE LINE # 12
LCALL do_nothing
; SOURCE LINE # 13
INC
i
SJMP ?C0001
; SOURCE LINE # 14
?C0004:
RET
; FUNCTION func (END)
E4
900000
F0
CLR
MOV
MOVX
; SOURCE LINE # 10
A
DPTR,#i
@DPTR,A
?C0001:
900000
E0
C3
9464
500B
000E 120000
0011
0014
0015
0016
0017
900000
E0
04
F0
80EC
0019
0019 22
MOV
MOVX
CLR
SUBB
JNC
DPTR,#i
A,@DPTR
C
A,#064H
?C0004
; SOURCE LINE # 12
LCALL do_nothing
; SOURCE LINE # 13
MOV
DPTR,#i
MOVX A,@DPTR
INC
A
MOVX @DPTR,A
SJMP ?C0001
; SOURCE LINE # 14
?C0004:
RET
; FUNCTION func (END)
323
Variable Location
Frequently accessed data objects should be located in the internal data memory
of the 8051. Accessing the internal data memory is much more efficient than
accessing the external data memory. The internal data memory is shared among
register banks, the bit data area, the stack, and other user defined variables with
the memory type data.
Because of the limited amount of internal data memory (128 to 256 bytes), all
your program variables may not fit into this memory area. In this case, you must
locate some variables in other memory areas. There are two ways to do this.
One way is to change the memory model and let the compiler do all the work.
This is the simplest method, but it is also the most costly in terms of the amount
of generated code and system performance. Refer to Memory Model on page
321 for more information.
Another way to locate variables in other memory areas is to manually select the
variables that can be moved into external data memory and declare them using
the xdata memory specifier. Usually, string buffers and other large arrays can
be declared with the xdata memory type without a significant degradation in
performance or increase in code size.
Variable Size
Members of the 8051 family are all 8-bit CPUs. Operations that use 8-bit types
(like char and unsigned char) are much more efficient than operations that use
int or long types. For this reason, always use the smallest data type possible.
The C51 compiler directly supports all byte operations. Byte types are not
promoted to integers unless required. See the INTPROMOTE directive for
more information.
An example can be illustrated by examining of multiplication operations. The
multiplication of two char objects is done inline with the 8051 instruction
MUL AB. To accomplish the same operation with int or long types would require
a call to a compiler library function.
324
Unsigned Types
The 8051 family of processors does not specifically support operations with
signed numbers. The compiler must generate additional code to deal with sign
extensions. Far less code is produced if unsigned objects are used wherever
possible.
Local Variables
When possible, use local variables for loops and other temporary calculations.
As part of the optimization process, the compiler attempts to maintain local
variables in registers. Register access is the fastest type of memory access. The
best effect is normally achieved with unsigned char and unsigned int variable
types.
Other Sources
The quality of the compiler generated code is more often than not directly
influenced by the algorithms implemented in the program. Sometimes, you can
improve the performance or reduce the code size simply by using a different
algorithm. For example, a heap sort algorithm always outperforms a bubble sort
algorithm.
For more information on how to write efficient programs, refer to the following
books:
The Elements of Programming Style, Second Edition
Kernighan & Plauger
McGraw-Hill
ISBN 0-07-034207-5
Writing Efficient Programs
Jon Louis Bentley
Prentice-Hall Software Series
ISBN 0-13-970244-X
Efficient C
Plum & Brodie
Plum Hall, Inc.
ISBN 0-911537-05-8
325
For the most part, there are no limits placed on the compiler with respect to
components of the C language; for example, you may specify an unlimited
number of symbols or number of case statements in a switch block. If there is
enough address space, several thousand symbols could be defined. However, at
this time, C51 is bound by a historical limit of 256 global symbols.
Names can be up to 255 characters long. However, only the first 32 are
significant. The C language provides for case sensitivity in regard to function
and variable names. However, for compatibility reasons, all names in the
object file appear in capital letters. It is therefore irrelevant if an external
object name within the source program is written in capital or small letters.
326
There may be a maximum of 256 external symbols. All module names which
have the memory class extern, are contained in this external symbol class.
The compiler produces external names for external functions, that are
dependent upon whether or not bit or data parameters are contained in the
function call. Thus, the reference name to the external data and bit segments
is produced in a manner analogous to the global functions.
327
+0
+1
Contents
0x34
0x12
+0
+1
+2
+3
Contents
0x44
0x52
0x41
0x57
A second method of accessing multi-byte data is called big endian and is often
referred to as Motorola order. In big endian, the most significant, or high-order
byte is stored first, and the least significant, or low-order byte is stored last. For
example, a 16-bit integer value of 0x1234 would be stored using the big endian
method in two consecutive bytes as follows:
Address
+0
+1
Contents
0x12
0x34
A 32-bit integer value of 0x004A4F4E would be stored using the big endian
method as follows:
Address
+0
+1
+2
+3
Contents
0x00
0x4A
0x4F
0x4E
328
The 8051 is an 8-bit machine and has no instructions for directly manipulating
data objects that are larger than 8 bits. Multi-byte data are stored according to
the following rules.
!
The 8051 LCALL instruction stores the address of the next instruction on the
stack. The address is pushed onto the stack low-order byte first. The address
is, therefore, stored in memory in little endian format.
All other 16-bit and 32-bit values are stored, contrary to other Intel
processors, in big endian format, with the high-order byte stored first. For
example, the LJMP and LCALL instructions expect 16-bit addresses that are
in big endian format.
Floating-point numbers are stored according to the IEEE-754 format and are
stored in big endian format with the high-order byte stored first.
329
330
does not print the string A 1 2 3. This is because the C51 compiler passes the
arguments 1, 2, and 3 all as 8-bit byte types. The format specifiers %d and
%u both expect 16-bit int types.
To avoid this type of problem, you must explicitly define the data type to pass to
the printf function. To do this, you must type cast the above values. For
example:
printf ("%c %d %u %bu", 'A',(int) 1, (unsigned int) 2, (char) 3);
If you are uncertain of the size of the argument that is passed, you may cast the
value to the desired size.
331
Uncalled Functions
It is common practice during the development process to write but not call
additional functions. While the compiler permits this without error, the
Linker/Locator does not treat this code casually, because of the support for data
overlaying, and emits a warning message.
Interrupt functions are never called, they are invoked by the hardware. An
uncalled routine is treated as a potential interrupt routine by the linker. This
means that the function is assigned non-overlayable data space for its local
variables. This quickly exhausts all available data memory (depending upon the
memory model used).
If you unexpectedly run out of memory, be sure to check for linker warnings
relating to uncalled or unused routines. You can use the linkers IXREF control
directive to include a cross reference list in the linker map (.M51) file.
332
In order to generate the appropriate instructions, the compiler must have the
absolute value of the reference to be generated. In the above example, this
cannot be done, as this address of xyz_flag cannot be known until after the
linking phase has been completed. Follow the rules below to avoid this problem.
1. A bdata variable (defined and used in the same way as an sfr) must be
defined in global space; not within the scope of a procedure.
2. A bdata bit variable (defined and used in the same way as an sbit) must also
be defined in global space, and cannot be located within the scope of a
procedure.
3. The definition of the bdata variable and the creation of its sbit access
component name must be accomplished where the compiler has a view of
both the variable and the component.
For example, declare the bdata variable and the bit component in the same
source module:
bdata char xyz_flag;
sbit xyz_bit1 = xyz_flag^1;
As with any other declared and named C variable that reserves space, simply
define your bdata variable and its component sbits in a module. Then, use the
extern bit specifier to reference it as the need arises.
333
Using Monitor-51
If you want to test a C program with Monitor-51 and if the Monitor-51 is
installed at code address 0, consider the following rules (the specification refers
to a target system where the available code memory for user programs starts at
address 8000H):
!
All C modules which contain interrupt functions must be translated with the
control directive INTVECTOR (0x8000).
334
Function Pointers
Function pointers are one of the most difficult aspects of C to understand and to
properly utilize. Most problems involving function pointers are caused by
improper declaration of the function pointer, improper assignment, and
improper dereferencing.
The following brief example demonstrates how to declare a function pointer (f),
how to assign function addresses to it, and how to call the functions through the
pointer. The printf routine is used for example purposes when running DS51 to
simulate program execution.
#pragma code symbols debug oe
#include <reg51.h>
#include <stdio.h>
void func1(int d) {
printf("In FUNC1(%d)\n", d);
}
/* function #1 */
void func2(int i) {
printf("In FUNC2(%d)\n", i);
}
/* function #2 */
void main(void) {
void (*f)(int i);
SCON = 0x50;
TMOD |= 0x20;
TH1
= 0xf3;
TR1
= 1;
TI
= 1;
while( 1 ) {
f = (void *)func1;
f(1);
f = (void *)func2;
f(2);
}
*/
*/
*/
*/
*/
/* f points to function #1 */
/* f points to function #2 */
NOTE
Because of the limited stack space of the 8051, the linker overlays function
variables and arguments in memory. When you use a function pointer, the linker
cannot correctly create a call tree for your program. For this reason, you may
have to correct the call tree for the data overlaying. Use the OVERLAY
directive with the linker to do this. Refer to the 8051 Utilities Users Guide for
more information.
Glossary
A51
The command used to assemble programs using the 8051 Macro Assembler.
aggregate types
Arrays, structures, and unions.
ANSI
American National Standards Institute. The organization responsible for
defining the C language standard.
argument
The value that is passed to a macro or function.
arithmetic types
Data types that are integral, floating-point, or enumerations.
array
A set of elements all of the same data type.
ASCII
American Standard Code for Information Interchange. This is a set of 256
codes used by computers to represent digits, characters, punctuation, and
other special symbols.
basename
The part of the file name that excludes the drive letter, directory name, and
file extension. For example, the basename for the file C:\C51\SAMPLE\SIO.C
is SIO.
batch file
A text file that contains MS-DOS commands and programs that can be
invoked from the command line.
BL51
The command used to link object files and libraries using the 8051
Code-Banking Linker/Locator.
block
A sequence of C statements, including definitions and declarations, enclosed
within braces ({ }).
335
336
Glossary
C51
The command used to compile programs using the 8051 Optimizing C Cross
Compiler.
constant expression
Any expression that evaluates to a constant non-variable value. Constants
may include character, integer, enumeration, and floating-point constant
values.
declaration
A C construct that associates the attributes of a variable, type, or function
with a name.
definition
A C construct that specifies the name, formal parameters, body, and return
type of a function or that initializes and allocates storage for a variable.
directive
An instruction to the C preprocessor or a control switch to the C51 compiler.
disk cache
A software program usually installed as a TSR or device driver that buffers
disk I/O operations in memory in an attempt to improve system performance
by satisfying disk reads from the memory buffer.
DS51
The command used to load and execute the 8051 Simulator/Debugger.
environment table
The memory area used by MS-DOS to store environment variables and their
values.
environment variable
A variable stored in the environment table. These variables provide MS-DOS
programs with information such as where to find include files and library
files.
escape sequence
A backslash (\) character followed by a single letter or a combination of
digits that specifies a particular character value in strings and character
constants.
expression
A combination of any number of operators and operands that produces a
constant value.
formal parameters
The variables that receive the value of arguments passed to a function.
function
A combination of declarations and statements that can be called by name that
perform an operation and/or return a value.
function body
A block that contains the declarations and statements that make up a function.
function call
An expression that invokes and possibly passes arguments to a function.
function declaration
A declaration that provides the name and return type of a function that is
explicitly defined elsewhere in the program.
function definition
A definition that provides the name, formal parameters, return type,
declarations, and statements that define what a function does.
function prototype
A function declaration that includes the list of formal parameters in
parentheses following the function name.
in-circuit emulator (ICE)
A hardware device that aids in debugging embedded software by providing
hardware-level single-stepping, tracing, and break-pointing. Some ICEs
provide a trace buffer that stores the most recent CPU events.
include file
A text file that is incorporated into a source file using the #include
preprocessor directive.
keyword
A reserved word with a predefined meaning for the compiler.
L51
The command used to link object files and libraries using the 8051
Linker/Locator.
LIB51
The command used to manipulate library files using the 8051 Library
Manager.
337
338
Glossary
library
A file that stores a number of possibly related object modules. The linker can
extract modules from the library to use in building a target object file.
LSB
Least significant bit or byte.
macro
An identifier that represents a series of keystrokes that is defined using the
#define preprocessor directive.
manifest constant
A macro that is defined to have a constant value.
MCS-51
The general name applied to the Intel family of 8051 compatible
microprocessors.
memory manager
Any of the programs that utilize the extended memory of the 80386 and
80486 CPUs to reduce system overhead and provide convenient means of
accessing the different types of memory on IBM AT/286/386 based machines
or 100% compatibles.
memory model
Any of the models that specifies which memory areas are used for function
arguments and local variables.
monitor51
An 8051 program that can be loaded into your target CPU to aid in debugging
and rapid product development through rapid software downloading.
MSB
Most significant bit or byte.
newline character
The character used to mark the end of a line in a text file or the escape
sequence (\n) used to represent the newline character.
null character
The ASCII character with the value 0 represented as the escape sequence
(\0).
null pointer
A pointer that references nothing and has an offset of 0000h. A null pointer
has the integer value 0.
object
An area of memory that can be examined. Usually used when referring to the
memory area associated with a variable or function.
object file
A file, created by the compiler, that contains the program segment
information and relocatable machine code.
OH51
The command used to convert absolute object files into Intel HEX file format
using the Object File Converter.
operand
A variable or constant that is used in an expression.
operator
A symbol (e.g., +, -, *, /) that specifies how to manipulate the operands of an
expression.
parameter
The value that is passed to a macro or function.
PL/M-51
A high-level programming language that provides a blocked structure, a
facility for data structures, type checking, and a standard language for use on
most Intel hardware architectures.
pointers
A variable that contains the address of another variable, function, or memory
area.
pragma
A statement that passes an instruction to the compiler at compile time.
preprocessor
The compilers first pass text processor that manipulates the contents of a C
file. The preprocessor defines and expands macros, reads include files, and
passes control directives to the compiler.
RAM disk
A memory area used by a device drive or TSR that emulates a disk drive, but
provides much faster access.
relocatable
Able to be moved or relocated. Not containing absolute or fixed addresses.
339
340
Glossary
RTX51 Full
An 8051 Real-TIME Executive that provides a multitasking operating system
kernel and library of routines for its use.
RTX51 Tiny
A limited version of RTX51.
scalar types
In C, integer, enumerated, floating-point, and pointer types.
scope
The sections or a program where an item (function or variable) can be
referenced by name. The scope of an item may be limited to file, function, or
block.
source file
A text file containing C program code.
stack
An area of memory, indirectly accessed by a stack pointer, that shrinks and
expands dynamically as items are pushed onto the stack and popped off of the
stack. Items in the stack are removed on a LIFO (last-in first-out) basis.
static
A storage class that, when used with a variable declaration in a function,
causes variables to retain their value after exiting the block or function in
which they are declared.
stream functions
Routines in the library that read and write characters using the input and
output streams.
string
An array of characters that is terminated with a null character (\0).
string literal
A string of characters enclosed within double quotes ( ).
structure
A set of elements of possibly different types grouped together under one
name.
structure member
One element of a structure.
token
A fundamental symbol that represents a name or entity in a programming
language.
TS51
The command used to load and execute the 8051 Target Debugger.
twos complement
A binary notation that is used to represent both positive and negative
numbers. Negative values are created by complementing all bits of a positive
value and adding 1.
type
A description of the range of values associated with a variable. For example,
an int type can have any value within its specified range (-32768 to 32767).
type cast
An operation in which an operand of one type is converted to another type by
specifying the desired type enclosed within parentheses immediately
preceding the operand.
whitespace character
Characters that are used as delimiters in C programs such as space, tab, and
newline.
wild card
One of the MS-DOS characters (? or *) that can be used in place of characters
in a filename.
341
342
Glossary
343
Index
#........................................................102
##......................................................103
#define..............................................101
#elif...................................................101
#else..................................................101
#endif................................................101
#error ................................................101
#if .....................................................101
#ifdef ................................................101
#ifndef ..............................................101
#include ............................................101
#line..................................................101
#pragma ............................................101
#undef...............................................101
+INF
described.....................................148
.I files....................................................5
.LST files ..............................................5
.OBJ files..............................................5
.SRC files..............................................5
_ _C51_ _ ...........................104,168,171
_ _DATE_ _ .......................104,168,171
_ _FILE_ _ .........................104,168,171
_ _LINE_ _.........................104,168,171
_ _MODEL_ _....................104,168,171
_ _STDC_ _........................104,168,171
_ _TIME_ _ ........................104,168,171
_at_ .......................................71,151,311
_chkfloat_..................................183,206
_crol_..................................173,183,209
_cror_ .................................173,183,210
_getkey ......................................185,216
_irol_ ..................................173,183,219
_iror_ ..................................173,183,220
_lrol_ ..................................173,183,237
_lror_ ..................................173,183,238
_nop_..................................173,188,247
_testbit_ ..............................173,188,289
_tolower.....................................181,293
_toupper.....................................181,295
16-bit Binary Integer Operations ......108
32-bit Binary Integer Operations ......108
A
A51
Interfacing .................................. 130
A51, defined..................................... 331
abs............................................. 182,194
ABSACC.H...................................... 189
Absolute Memory Access
Macros ............................................. 176
CBYTE ...................................... 176
CWORD..................................... 176
DBYTE ...................................... 177
DWORD..................................... 177
PBYTE....................................... 178
PWORD ..................................... 178
XBYTE ...................................... 179
XWORD..................................... 179
Absolute Memory Locations............ 149
Absolute register addressing .............. 10
344
Index
Absolute value
abs .............................................. 194
cabs ............................................ 203
fabs............................................. 212
labs ............................................. 232
Abstract Pointers................................ 81
Access Optimizing ........................... 126
Accessing Absolute Memory
Locations ......................................... 149
acos ........................................... 182,195
function timing ........................... 109
acos517 ............................................ 195
function timing ........................... 109
Additional items, notational
conventions .......................................... v
Address of interrupts.......................... 92
Advanced Programming
Techniques....................................... 113
aggregate types, defined................... 331
alien ................................................... 99
AMD
80C321....................................... 106
80C521....................................... 106
80C541....................................... 106
AMD 80C521 ............................... 34,35
ANSI
Differences ................................. 305
Include Files............................... 189
Library........................................ 173
Standard C Constant................... 104
ANSI, defined .................................. 331
Arc
cosine ......................................... 195
sine ............................................. 196
tangent................................. 198,199
AREGS .............................................. 10
Argument lists, variable-length ... 31,188
argument, defined ............................ 331
arithmetic types, defined .................. 331
array, defined ................................... 331
ASCII, defined ................................. 331
asin............................................ 182,196
function timing ........................... 109
asin517............................................. 196
function timing ........................... 109
ASM................................................... 12
Assembly code in-line........................ 12
B
basename, defined ............................331
batch file, defined.............................331
bdata...................................................59
bdata, tips for....................................328
big endian.........................................323
Binary Integer Operations ................108
bit
As first parameter in function
call ................................................87
Bit shifting functions
_crol_..........................................183
_cror_ .........................................183
_irol_ ..........................................183
_iror_ ..........................................183
_lrol_ ..........................................183
_lror_ ..........................................183
Bit Types ............................................65
Bit-addressable objects.......................66
BL51, defined...................................331
block, defined...................................331
bold capital text, use of ........................v
bold type, use of ...................................v
Books
About the C Language ....................2
braces, use of........................................v
Buffer Manipulation Routines..........180
memccpy..............................180,240
memchr ................................180,241
memcmp ..............................180,242
memcpy................................180,243
memmove.............................180,244
memset.................................180,245
C
C51
Control directives............................6
Errorlevel ........................................5
Extensions.....................................57
Output files .....................................5
Running...........................................4
C51 command.......................................3
C51, defined .....................................331
C51C.LIB .........................................174
C51FPC.LIB.....................................174
C51FPL.LIB .....................................174
C51FPS.LIB .....................................174
C51INC ................................................3
C51L.LIB .........................................174
C51LIB.................................................3
C51S.LIB..........................................174
cabs............................................182,203
calloc .........................................184,204
CALLOC.C ......................................123
Case/Switch Optimizing ...................126
Categories of C51 directives.................6
CBYTE......................................149,176
CD ......................................................14
ceil .............................................182,205
Character Classification
Routines............................................181
isalnum........................................181
isalpha.........................................181
iscntrl ..........................................181
isdigit ..........................................181
isgraph ........................................181
islower ........................................181
isprint ..........................................181
ispunct.........................................181
isspace.........................................181
isupper ........................................181
isxdigit ........................................181
Character Conversion and
Classification Routines .....................181
Character Conversion Routines ........181
_tolower ......................................181
_toupper ......................................181
345
D
Dallas 80C320/520 ............................ 34
Dallas Semiconductor
80C320....................................... 106
80C520....................................... 106
80C530....................................... 106
data..................................................... 59
Data Conversion Routines................ 182
abs .............................................. 182
346
Index
E
EJ ....................................................... 22
EJECT................................................ 22
elif.................................................... 101
ellipses, use of...................................... v
ellipses, vertical, use of........................ v
else................................................... 101
ENDASM .......................................... 12
endian ...............................................323
endif .................................................101
environment table, defined ...............332
environment variable, defined ..........332
EOF ..................................................191
error..................................................101
ERRORLEVEL....................................5
escape sequence, defined..................332
Execution timings.............................108
exp.............................................182,211
function timing ...........................109
exp517..............................................211
function timing ...........................109
exponent ...........................................146
expression, defined...........................332
Extensions for C51 .............................57
Extensions to C ..................................57
External Data Memory .......................60
F
fabs............................................182,212
Fatal Error Messages........................153
FF .......................................................23
Filename, notational conventions .........v
Files generated by C51 .........................5
FLOATFUZZY ..................................23
Floating-point
exponent .....................................146
mantissa ......................................146
storage format.............................146
Floating-point compare ......................23
Floating-Point Errors........................148
+INF ...........................................148
-INF ............................................148
Nan .............................................148
Floating-point numbers ....................146
Floating-point Operations.................109
floor...........................................182,213
Form feeds..........................................22
formal parameters, defined...............332
free ............................................184,214
FREE.C ............................................123
function body, defined......................333
function call, defined........................333
function declaration, defined ............333
Function Declarations ........................85
function definition, defined ..............333
G
General Optimizations......................126
getchar .......................................185,215
GETKEY.C ......................................123
gets ............................................185,217
Global Common Subexpression
Elimination .......................................126
Global register optimization ...............47
Glossary............................................331
H
High-Speed Arithmetic.....................108
I
IBPSTACK.......................................114
IBPSTACKTOP ...............................114
ICE, defined......................................333
idata ....................................................59
IDATALEN......................................114
IEEE-754 standard ...........................146
if .......................................................101
ifdef ..................................................101
ifndef ................................................101
in-circuit emulator, defined ..............333
include ..............................................101
Include file listing...............................30
include file, defined ..........................333
Include Files .....................................189
80C517.H....................................189
ABSACC.H ................................189
347
ASSERT.H................................. 190
CTYPE.H ................................... 190
INTRINS.H ................................ 190
MATH.H .................................... 190
REG152.H.................................. 189
REG252.H.................................. 189
REG451.H.................................. 189
REG452.H.................................. 189
REG51.H.................................... 189
REG515.H.................................. 189
REG517.H.................................. 189
REG51F.H.................................. 189
REG51G.H................................. 189
REG51GB.H .............................. 189
REG52.H.................................... 189
REG552.H.................................. 189
SETJMP.H ................................. 191
STDARG.H................................ 191
STDDEF.H................................. 191
STDIO.H.................................... 191
STDLIB.H.................................. 192
STRING.H ................................. 192
-INF
described .................................... 148
INIT.A51 ......................................... 120
INIT_MEM.C .................................. 123
init_mempool ............................ 184,218
INIT751.A51 ................................... 121
Initializing memory.......................... 114
Initializing the stream I/O
routines ............................................ 185
In-line assembly ................................. 12
Integer Operations............................ 108
Integer promotion............................... 25
Interfacing C Programs to A51 ........ 130
Interfacing C Programs to
PL/M-51........................................... 142
Internal Data Memory........................ 59
interrupt......................................... 90,93
Addresses ..................................... 92
Description ................................... 92
Function rules............................... 95
Functions ...................................... 92
Numbers ....................................... 92
Interrupt vector .................................. 27
Interrupt vector interval ..................... 24
Interrupt vector offset ........................ 27
348
Index
INTERVAL ....................................... 24
INTPROMOTE ................................. 25
INTRINS.H...................................... 190
Intrinsic Routines............................. 173
_crol_ ......................................... 173
_cror_ ......................................... 173
_irol_.......................................... 173
_iror_.......................................... 173
_lrol_.......................................... 173
_lror_.......................................... 173
_nop_ ......................................... 173
_testbit_...................................... 173
INTVECTOR..................................... 27
IP ....................................................... 25
isalnum...................................... 181,221
isalpha....................................... 181,222
iscntrl ........................................ 181,223
isdigit ........................................ 181,224
isgraph ...................................... 181,225
islower ...................................... 181,226
isprint ........................................ 181,227
ispunct....................................... 181,228
isspace....................................... 181,229
isupper ...................................... 181,230
isxdigit ...................................... 181,231
italicized text, use of ............................ v
IV....................................................... 27
J
jmp_buf............................................ 175
Jump Optimizing.............................. 126
K
Key names, notational
conventions .......................................... v
keyword, defined ............................. 333
Keywords........................................... 57
L
L51, defined..................................... 333
LA...................................................... 29
labs............................................ 182,232
Language elements, notational
conventions .......................................... v
Language Extensions ......................... 57
LARGE .........................................29,88
Large memory model .........................29
Large Model.......................................62
LC ......................................................30
LIB51, defined .................................333
Library Files .....................................174
80C751.LIB................................174
C51C.LIB ...................................174
C51FPC.LIB...............................174
C51FPL.LIB ...............................174
C51FPS.LIB ...............................174
C51L.LIB ...................................174
C51S.LIB....................................174
Library Reference.............................173
Library Routines
ANSI, excluded from C51 ..........306
ANSI, included in C51 ...............305
non-ANSI ...................................307
Library Routines by Category ..........180
library, defined .................................333
Limitations
C51 .............................................321
OMF-51 ......................................322
line....................................................101
Linker Location Controls .................150
LISTINCLUDE..................................30
Listing file generation ........................46
Listing file page length.......................43
Listing file page width........................44
Listing include files ............................30
little endian.......................................323
log .............................................182,233
function timing ...........................109
log10 .........................................182,234
function timing ...........................109
log10517 ..........................................234
function timing ...........................109
log517 ..............................................233
function timing ...........................109
longjmp .....................................188,235
LSB, defined ....................................334
M
macro, defined..................................334
malloc........................................184,239
MALLOC.C .....................................124
manifest constant, defined ................334
mantissa ............................................146
Manual organization ............................iv
Math Routines ..................................182
_chkfloat_ ...................................183
_crol_..........................................183
_cror_..........................................183
_irol_...........................................183
_iror_ ..........................................183
_lrol_...........................................183
_lror_ ..........................................183
acos .............................................182
asin..............................................182
atan .............................................182
atan2 ...........................................182
ceil ..............................................182
cos...............................................182
cosh.............................................182
exp ..............................................182
fabs .............................................182
floor ............................................182
log ...............................................182
log10 ...........................................182
modf............................................182
pow .............................................182
rand .............................................182
sin ...............................................182
sinh..............................................182
sqrt ..............................................182
srand ...........................................182
tan ...............................................183
tanh .............................................183
MATH.H ..........................................190
MAXARGS ........................................31
Maximum arguments in
variable-length argument lists.............31
MCS-51, defined ..............................334
memccpy....................................180,240
memchr ......................................180,241
memcmp ....................................180,242
memcpy .....................................180,243
memmove ..................................180,244
Memory Allocation Routines............184
calloc...........................................184
free..............................................184
init_mempool ..............................184
malloc .........................................184
realloc .........................................184
349
Memory areas..................................... 58
external data ................................. 60
internal data.................................. 59
program ........................................ 58
special function register................ 61
memory manager, defined................ 334
Memory Model .................................. 61
Compact ....................................... 62
Function ....................................... 88
Large ............................................ 62
Small ............................................ 61
memory model, defined ................... 334
Memory Type..................................... 62
bdata........................................ 59,63
code ......................................... 58,63
data............................................... 59
idata......................................... 59,63
pdata........................................ 60,63
xdata........................................ 60,63
Memory Typedata.............................. 63
memset ...................................... 180,245
Miscellaneous Routines ................... 188
_nop_.......................................... 188
_testbit_...................................... 188
longjmp ...................................... 188
setjmp ......................................... 188
MOD517 ..................................... 32,107
MODDP2.................................... 34,106
modf.......................................... 182,246
monitor51, defined........................... 334
MSB, defined ................................... 334
N
NaN...................... 207,249,264,265,287
described .................................... 148
newline character, defined ............... 334
NOAMAKE ....................................... 35
NOAREGS......................................... 10
NOAU................................................ 32
NOCO ................................................ 16
NOCOND .......................................... 16
NODP8 .............................................. 32
NOEXTEND...................................... 36
NOINTPROMOTE............................ 25
NOINTVECTOR ............................... 27
NOIP .................................................. 25
NOIV ................................................. 27
350
Index
NOMOD517 ...................................... 32
NOMODDP2 .............................. 34,106
NOOBJECT....................................... 37
NOOJ ................................................. 37
NOPR ................................................ 46
NOPRINT.......................................... 46
NOREGPARMS ................................ 49
NULL............................................... 192
null character, defined...................... 334
null pointer, defined......................... 334
O
OBJECT ............................................ 37
Object file generation......................... 37
object file, defined ........................... 335
object, defined ................................. 334
OBJECTEXTEND............................. 38
OE...................................................... 38
offsetof............................................. 248
OH51, defined ................................. 335
OHS51 ............................................. 311
OJ....................................................... 37
Omitted text, notational
conventions .......................................... v
operand, defined .............................. 335
Operation timings ............................ 108
operator, defined .............................. 335
OPTIMIZE ........................................ 39
Optimizer ......................................... 125
Optimizing programs ......................... 39
Optimum Code
Local Variables .......................... 320
Memory Model .......................... 317
Other Sources............................. 320
Variable Location....................... 319
Variable Size.............................. 319
Variable Types ........................... 320
Optional items, notational
conventions .......................................... v
Options for Code Generation ........... 126
OR...................................................... 42
ORDER.............................................. 42
Order of variables .............................. 42
OT...................................................... 39
Output files .......................................... 5
Overlaying Segments ....................... 135
P
Page length in listing file....................43
Page width in listing file.....................44
PAGELENGTH .................................43
PAGEWIDTH....................................44
Parameter Passing in Fixed
Memory Locations ...........................132
Parameter Passing in Registers.........131
Parameter Passing Via Registers ......126
parameter, defined............................335
Passing arguments in registers............49
Passing Parameters in Registers .........87
PATH ...................................................3
PBPSTACK .....................................115
PBPSTACKTOP..............................115
PBYTE......................................149,178
pdata...................................................60
PDATALEN.....................................114
PDATASTART................................114
Peephole Optimization .....................126
Philips
8xC750 .......................................111
8xC751 .......................................111
8xC752 .......................................111
PL.......................................................43
PL/M-51 .............................................99
Defined .......................................335
Interfacing ..................................142
Pointer Conversions ...........................78
Pointer memory types.........................73
Pointers ..............................................73
Generic .........................................73
Memory-specific...........................76
pointers, defined...............................335
pow............................................182,249
PP .......................................................45
PPAGE .............................................115
PPAGEENABLE .............................115
PR.......................................................46
pragma..............................................101
pragma, defined................................335
Predefined Macro Constants ............104
_ _C51_ _ ...................................104
_ _DATE_ _ ...............................104
_ _FILE_ _ .................................104
_ _LINE_ _.................................104
_ _MODEL_ _............................104
351
_ _STDC_ _ ................................104
_ _TIME_ _ ................................104
Preface................................................ iii
PREPRINT .........................................45
Preprocessor .....................................101
Preprocessor directives
define ..........................................101
elif...............................................101
else..............................................101
endif ............................................101
error ............................................101
if..................................................101
ifdef.............................................101
ifndef...........................................101
include ........................................101
line ..............................................101
pragma ........................................101
undef ...........................................101
Preprocessor output file
generation ...........................................45
preprocessor, defined........................335
PRINT ................................................46
Printed text, notational
conventions...........................................v
printf ..........................................185,250
printf, tips for....................................326
printf517 ...........................................250
Program Memory................................58
Program memory size .........................50
putchar .......................................185,255
PUTCHAR.C....................................123
puts ............................................185,256
PW......................................................44
PWORD ....................................149,178
R0-R7 .................................................10
RAM disk, defined ...........................335
rand............................................182,257
Range for data types ...........................64
RB ......................................................48
realloc ........................................184,258
REALLOC.C ....................................124
Real-Time Function Tasks................100
Recursive Code, tips for ...................325
Recursive Functions ...........................96
reentrant..............................................96
352
Index
sfr ....................................................... 68
sfr16 ................................................... 69
SIECO-51 ........................................ 312
Siemens
80C517....................................... 107
80C537....................................... 107
Siemens 80C517 ................................ 32
Signetics
8xC750....................................... 111
8xC751....................................... 111
8xC752....................................... 111
sin ............................................. 182,264
function timing ........................... 109
sin517 .............................................. 264
function timing ........................... 109
sinh............................................ 182,265
Size of data types ............................... 64
SM ..................................................... 52
SMALL......................................... 52,88
Small memory model ......................... 52
Small Model....................................... 61
source file, defined........................... 336
Special Function Register
Memory.............................................. 61
Special Function Registers................. 68
sprintf........................................ 185,266
sprintf517......................................... 266
sqrt ............................................ 182,268
function timing ........................... 109
sqrt517 ............................................. 268
function timing ........................... 109
srand ......................................... 182,269
SRC.................................................... 53
sscanf ........................................ 185,270
sscanf517 ......................................... 270
Stack .................................................. 86
stack, defined ................................... 336
Standard Types ................................ 175
jmp_buf ...................................... 175
va_list......................................... 175
START751.A51............................... 118
STARTUP.A51................................ 114
static, defined................................... 336
STDARG.H ..................................... 191
STDDEF.H ...................................... 191
STDIO.H ......................................... 191
STDLIB.H ....................................... 192
Storage format
bit................................................143
char .............................................144
code pointer ................................144
data pointer .................................144
enum ...........................................144
float.............................................146
generic pointer ............................145
idata pointer................................144
int................................................144
long.............................................144
pdata pointer ...............................144
short............................................144
xdata pointer ...............................144
strcat..........................................187,272
strchr .........................................187,273
strcmp........................................187,274
strcpy.........................................187,275
strcspn .......................................187,276
stream functions, defined..................336
Stream I/O Routines .........................185
_getkey .......................................185
getchar ........................................185
gets .............................................185
Initializing ..................................185
printf ...........................................185
putchar ........................................185
puts .............................................185
scanf ...........................................185
sprintf .........................................185
sscanf ..........................................185
ungetchar ....................................185
vprintf .........................................185
vsprintf........................................185
Stream Input and Output ..................185
string literal, defined ........................336
String Manipulation Routines...........187
strcat ...........................................187
strchr...........................................187
strcmp .........................................187
strcpy ..........................................187
strcspn.........................................187
strlen ...........................................187
strncat .........................................187
strncmp .......................................187
strncpy ........................................187
strpbrk.........................................187
strpos...........................................187
strrchr..........................................187
strrpbrk .......................................187
strrpos .........................................187
strspn...........................................187
string, defined ...................................336
STRING.H........................................192
Stringize Operator ............................102
strlen ..........................................187,277
strncat ........................................187,278
strncmp ......................................187,279
strncpy .......................................187,280
strpbrk........................................187,281
strpos .........................................187,282
strrchr ........................................187,283
strrpbrk ......................................187,284
strrpos ........................................187,285
strspn .........................................187,286
structure member, defined ................336
structure, defined ..............................336
Symbol table generation .....................54
SYMBOLS .........................................54
Syntax and Semantic Errors..............157
T
tan ..............................................183,287
function timing............................109
tan517 ...............................................287
function timing............................109
tanh ............................................183,288
Timing operation execution..............108
TMP......................................................3
toascii ........................................181,290
toint............................................181,291
token, defined ...................................336
Token-Pasting Operator....................103
tolower.......................................181,292
toupper.......................................181,294
TS51, defined ...................................336
twos complement, defined...............337
type cast, defined ..............................337
type, defined .....................................337
353
U
Uncalled Functions, tips for ............. 327
undef ................................................ 101
ungetchar................................... 185,296
using.............................................. 89,94
Using Monitor-51, tips for ............... 329
V
va_arg ....................................... 188,297
va_end....................................... 188,299
va_list............................................... 175
va_start...................................... 188,300
Variable-length argument list
routines ............................................ 188
va_arg......................................... 188
va_end ........................................ 188
va_start ....................................... 188
Variable-length argument lists ........... 31
Variables, notational
conventions .......................................... v
vertical bar, use of................................ v
vprintf ....................................... 185,301
vsprintf ...................................... 185,303
W
Warning detection.............................. 55
WARNINGLEVEL............................ 55
Warnings.......................................... 169
WATCHDOG .................................. 120
whitespace character, defined .......... 337
wild card, defined............................. 337
WL ..................................................... 55
X
XBPSTACK .................................... 115
XBPSTACKTOP ............................. 115
XBYTE..................................... 149,179
xdata................................................... 60
XDATALEN.................................... 114
XDATASTART............................... 114
XOFF ............................................... 123
XON................................................. 123
XWORD ................................... 149,179