
- •Chapter 1. Introduction
- •Books About the C Language
- •Chapter 2. Compiling with C51
- •Environment Settings
- •Running C51
- •DOS ERRORLEVEL
- •C51 Output Files
- •Control Directives
- •Directive Categories
- •Reference
- •Chapter 3. Language Extensions
- •Keywords
- •8051 Memory Areas
- •Program Memory
- •Internal Data Memory
- •External Data Memory
- •Special Function Register Memory
- •Memory Models
- •Small Model
- •Compact Model
- •Large Model
- •Memory Types
- •Explicitly Declared Memory Types
- •Implicit Memory Types
- •Data Types
- •Bit Types
- •Special Function Registers
- •sbit
- •Absolute Variable Location
- •Pointers
- •Generic Pointers
- •Pointer Conversions
- •Abstract Pointers
- •Function Declarations
- •Function Parameters and the Stack
- •Passing Parameters in Registers
- •Function Return Values
- •Specifying the Memory Model for a Function
- •Specifying the Register Bank for a Function
- •Register Bank Access
- •Interrupt Functions
- •Reentrant Functions
- •Chapter 4. Preprocessor
- •Directives
- •Stringize Operator
- •Predefined Macro Constants
- •Chapter 5. 8051 Derivatives
- •Dallas 80C320, 80C520, and 80C530
- •Siemens 80C517 and 80C537
- •Data Pointers
- •Library Routines
- •Philips/Signetics 8xC750, 8xC751, and 8xC752
- •Customization Files
- •STARTUP.A51
- •START751.A51
- •INIT.A51
- •INIT751.A51
- •PUTCHAR.C
- •GETKEY.C
- •CALLOC.C
- •FREE.C
- •INIT_MEM.C
- •MALLOC.C
- •REALLOC.C
- •Optimizer
- •General Optimizations
- •8051 Specific Optimizations
- •Options for Code Generation
- •Segment Naming Conventions
- •Data Objects
- •Program Objects
- •Interfacing C Programs to Assembler
- •Function Parameters
- •Parameter Passing in Registers
- •Parameter Passing in Fixed Memory Locations
- •Function Return Values
- •Using the SRC Directive
- •Register Usage
- •Overlaying Segments
- •Example Routines
- •Small Model Example
- •Compact Model Example
- •Large Model Example
- •Data Storage Formats
- •Bit Variables
- •Signed and Unsigned Long Integers
- •Generic Pointers
- •Accessing Absolute Memory Locations
- •Absolute Memory Access Macros
- •Linker Location Controls
- •The _at_ Keyword
- •Debugging
- •Chapter 7. Error Messages
- •Fatal Errors
- •Actions
- •Errors
- •Syntax and Semantic Errors
- •Warnings
- •Chapter 8. Library Reference
- •Intrinsic Routines
- •Library Files
- •Standard Types
- •va_list
- •Absolute Memory Access Macros
- •CBYTE
- •CWORD
- •DBYTE
- •DWORD
- •PBYTE
- •PWORD
- •XBYTE
- •XWORD
- •Routines by Category
- •Buffer Manipulation
- •Character Conversion and Classification
- •Data Conversion
- •Math
- •Memory Allocation
- •Stream Input and Output
- •String Manipulation
- •Miscellaneous
- •Include Files
- •8051 Special Function Register Include Files
- •ABSACC.H
- •ASSERT.H
- •CTYPE.H
- •INTRINS.H
- •MATH.H
- •SETJMP.H
- •STDARG.H
- •STDDEF.H
- •STDIO.H
- •STDLIB.H
- •STRING.H
- •Reference
- •Appendix A. Differences from ANSI C
- •Compiler-related Differences
- •Library-related Differences
- •Appendix B. Version Differences
- •Version 4 Differences
- •Version 3.4 Differences
- •Version 3.2 Differences
- •Version 3.0 Differences
- •Version 2 Differences
- •Appendix C. Writing Optimum Code
- •Memory Model
- •Variable Location
- •Variable Size
- •Unsigned Types
- •Local Variables
- •Other Sources
- •Appendix D. Compiler Limits
- •Appendix E. Byte Ordering
- •Recursive Code Reference Error
- •Problems Using the printf Routines
- •Uncalled Functions
- •Trouble with the bdata Memory Type
- •Using Monitor-51
- •Function Pointers
- •Glossary
- •Index

Keil Software — C51 Compiler User’s Guide |
141 |
|
|
Large Model Example
In the large model, parameters passed in fixed memory locations are stored in external data memory. The parameter passing segment for variables is located in the xdata area.
The following are two assembly code examples. The first shows you how the example function is invoked from assembly. The second example displays the assembly code for the example function.
Function invocation from assembly
EXTRN CODE |
(_function) |
; Ext declarations for function names |
|
||
EXTRN XDATA |
(?_function?BYTE) |
; Start of transfer for local vars |
|
||
EXTRN BIT |
(?_function?BIT) |
; Start of transfer for local bit vars |
|
||
. |
|
|
|
|
|
. |
|
|
|
|
|
. |
|
|
|
|
|
MOV |
R6,#HIGH intval |
; int |
a |
|
|
MOV |
R7,#LOW intval |
; int |
a |
|
|
MOV |
R5,#charconst |
; char b |
|
||
SETB |
?_function?BIT+0 |
; bit |
c |
|
|
MOV |
R0,#?_function?BYTE+3 |
; Address of 'v_d' in the passing area |
|
||
MOV |
A,longval+0 |
; long d |
|
||
MOVX |
@DPTR,A |
; Store parameter byte |
|
||
INC |
DPTR |
; Increment parameter passing address |
|
||
MOV |
A,longval+1 |
; long d |
|
||
MOVX |
@DPTR,A |
; Store parameter byte |
|
||
INC |
DPTR |
; Increment parameter passing address |
|
||
MOV |
A,longval+2 |
; long d |
|
||
MOVX |
@DPTR,A |
; Store parameter byte |
|
||
INC |
DPTR |
; Increment parameter passing address |
|
||
MOV |
A,longval+3 |
; long d |
6 |
||
MOVX |
@DPTR,A |
; Store parameter byte |
|||
|
|||||
MOV |
C,bitvalue |
|
|
|
|
MOV |
?_function?BIT+1,C |
; bit |
e |
|
|
LCALL |
_function |
|
|
|
|
MOV |
intresult+0,R6 |
; Store int |
|
||
MOV |
intresult+1,R7 |
; Retval |
|
||
. |
|
|
|
|
.
.

142 Chapter 6. Advanced Programming Techniques
Function implementation in assembly
|
|
NAME |
|
MODULE |
|
|
; Name of |
the program module |
|
|
|
?PR?FUNCTION?MODULE |
SEGMENT |
CODE |
; Seg for program code in 'functions' |
||||
|
|
?XD?FUNCTION?MODULE |
SEGMENT |
XDATA |
OVERLAYABLE |
||||
|
|
|
|
|
|
|
; Seg for local vars in 'function' |
||
|
|
?BI?FUNCTION?MODULE |
SEGMENT |
BIT |
OVERLAYABLE |
||||
|
|
|
|
|
|
|
; Seg for local bit vars in 'function' |
||
|
|
PUBLIC |
|
_function, ?_function?BYTE, ?_function?BIT |
|||||
|
|
|
|
|
|
|
; Public symbols for C function call |
||
|
|
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 |
; Segment for local bit variables |
||||
|
|
?_function?BIT: |
|
|
; Start of the parameter passing seg |
||||
|
|
v_c: |
DBIT |
1 |
|
|
; |
bit variable: v_c |
|
|
|
v_e: |
DBIT |
1 |
|
|
; |
bit variable: v_e |
|
|
|
. |
|
|
|
|
|
|
|
|
|
. |
|
|
|
|
; Additional local bit variables |
||
|
|
. |
|
|
|
|
|
|
|
|
|
RSEG |
|
?PR?FUNCTION?MODULE |
|
; Program segment |
|||
|
|
_function: |
MOV |
DPTR,#?_function?BYTE+0 ; Special function prolog |
|||||
|
|
|
|
MOV |
A,R6 |
|
|
; and epilog is not |
|
|
|
|
|
MOVX |
@DPTR,A |
|
|
; necessary. All vars |
|
|
|
|
|
INC |
R0 |
|
|
; can immediately be |
|
|
|
|
|
MOV |
A,R7 |
|
|
; accessed. |
|
|
|
|
|
MOVX |
@DPTR,A |
|
|
|
|
|
|
|
|
INC |
R0 |
|
|
|
|
6 |
|
. |
|
MOV |
A,R5 |
|
|
|
|
|
|
MOVX |
@DPTR,A |
|
|
|
|
||
|
|
|
|
|
|
|
|
||
|
|
. |
|
|
|
|
|
|
|
|
|
. |
|
|
|
|
|
|
|
|
|
|
|
MOV |
R6,#HIGH retval |
|
; Return value |
||
|
|
|
|
|
|||||
|
|
|
|
MOV |
R7,#LOW |
retval |
|
; int constant |
|
|
|
|
|
RET |
|
|
|
; Return |

Keil Software — C51 Compiler User’s Guide |
143 |
|
|
Interfacing C Programs to PL/M-51
You can easily interface C51 to routines written in PL/M-51. Intel’s PL/M-51 is a popular programming language that is similar to C in many ways. The PL/M-51 compiler generates object files in the OMF-51 format. You can access PL/M-51 functions from C by declaring them with the alien function type specifier. Public variables declared in the PL/M-51 module are available to your C programs.
C51 can optionally operate with PL/M-51 parameter passing conventions. The alien function type specifier is used to declare public or external functions that are compatible with PL/M-51 in any memory model. For example:
extern alien char plm_func (int, char);
alien unsigned int c_func (unsigned char x, unsigned char y) { return (x * y);
} |
|
|
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 |
6 |
|
alien functions and causes C51 to generate an error message. For example: |
||
|
||
extern alien unsigned int plm_i (char, int, ...); |
|
|
*** ERROR IN LINE 1 OF A.C: 'plm_i': Var_parms on alien function |
|