Saturday, December 10, 2011

keil C Programming Tutorial: C and Assembly together

►Interfacing C program to Assembler

You can easily interface your programs to routines written in 8051 Assembler. All you need to do is follow few programming rules, you can call assembly routines from C and vice-versa. Public variables declared in assembly modules are available to your C program.

There maybe several reasons to call an assembly routine like faster execution of program, accessing SFRs directly using assembly etc. In this part of
tutorial we will discuss how to write assembly progarms that can be directly interfaced with C programs.

For any assembly routine to be called from C program, you must know how to pass parameters or arguements to fucntion and get return values from a function.

►Segment naming

C51 compiler generates objects for every program like program code, program data and constant data. These objects are stored in segments which are units of code or data memory. Segment naming is standard for C51 compiler, so every assembly program need to follow this convention.

Segment names include module_name which is the name of the source file in which the object is declared. Each segment has a prefix that corresponds to memory type used for the segment. Prefix is enclosed in question marks (?). The following is the list of the standard segment name prefixes:
C51 module prefix

►Data Objects:


Data objects are the variables and constants you declare in your C programs. The C51 compiler generates a saperate segment for each memory type for which variable is declared. The following table lists the segment names generated for different variable data objects.

Data objects segment prefix

Program Objects:

Program onjects includes code generated for C programs functions by 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, for a function name send_char in file name uart.c will have a segment name of ?PR?SEND_CHAR?UART.

C51 compiler creates saperate segments for local variables that are declared within the body of a function. Segment naming conventions for different memory models are given in following tables:

Small model segment naming convention

Compact model segment naming convention

Large model segment naming convention
Function names are modified slightly depending on type of function (functions without arguments, functions with arguments and reentrant functions). Following tables explains the segment names:

function segment naming convention

In next section we will learn regarding defining functions which can be called from any C function and how a C function can be called from assembly.

No comments:

Post a Comment