C51 Compiler - Advanced Programming Techniques (3) - Interface between C language and assembly

Publisher:科技创客Latest update time:2016-11-14 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
 C program and assembly interface

Cx51 programs can be easily interfaced with 8051 assemblers. The A51 assembler is an 8051 macro assembler that emits object modules in OMF-51 format. By observing some programming rules, you can call assembler programs from C programs and vice versa. Public variables declared in assembly modules can also be used in C programs.

There are several reasons to call an assembler from a C program. First, you can use an existing assembler, second, you want it to run faster, and third, you want to use it directly in assembly to manipulate SFRs or use I/O memory images.

For an assembler to be called from a C program, its parameter passing rules and value return rules must be consistent with C functions. In terms of application, it must look like a C function.

Function Parameters

In general, a C function can pass three parameters through registers. The other parameters are passed through fixed memory. Of course, NOREGPARAMS can be used to prohibit the use of registers to pass parameters. If it is specified that registers are not allowed to pass parameters or there are too many parameters, fixed memory areas are used to pass parameters. When generating code, the function that passes parameters in registers has "_" added to the front of the name, and the function that uses fixed memory areas to pass parameters does not change its name.

Parameter Passing in Registers

Passing parameters in registers

C functions can pass parameters in registers or in fixed storage areas. Up to three parameters can be passed in registers. The following table shows the rules for parameter passing:

Arg Number char, 1-byte ptr    int, 2-byte ptr     long, float     generic ptr

1 R7 R6 & R7 R4—R7 R1—R3

(MSB in R6,         (Mem type in R3,

LSB in R7)              MSB in R2

LSB in R1)

2          R5              R4 & R5     R4—R7         R1—R3

(MSB in R4,             (Mem type in R3

LSB in R5)                  MSB in R2

                                                    LSB in R1)

3          R3              R2 & R3                     R1—R3

(MSB in R2,                 (Mem type in R3

LSB in R3)              MSB in R2,

LSB in R1)

Example

Declaration       Description

func1 (int a) parameter a is passed through R6 and R7

func2 (int b,int c,int *d) parameter b is passed through R6 and R7, parameter c is passed through R4 and R5, and parameter d is passed through R1, R2 and R3

func3 (long e, long f) parameter e is passed through R4, R5, R6, R7, and parameter f is passed through a fixed storage area

func4 (float g, char h) parameter g is passed through R4, R5, R6, R7, and parameter h is passed through a fixed storage area

Parameter Passing in Fixed Memory Locations

Passing parameters via fixed storage area

Procedures that pass parameters through fixed storage areas ?function_name?BYTE and ?function_name?BIT name and carry parameter values ​​and pass them to function_name. Bit parameters are copied to the ?function_name?BIT segment before calling the function. All other parameter data is copied to the ?function_name?BYTE segment. All parameters are assigned storage space within these segments, even if they may use registers to pass parameters. Parameters are stored in the corresponding segments in the order they are declared.

Depending on the storage mode, the fixed storage area for parameter transfer may be the internal data memory or the external data memory. The small storage mode uses the internal data memory as the parameter transfer area, which is the most efficient. The Compact and large storage modes use the external data memory as the parameter transfer area.

Function Return Values

The return value of a function always uses CPU registers. The following table lists the registers used by possible return values.

Return Value Type Register Description

bit Carry Flag A single bit is returned in the carry position

char / unsigned char, R7 A single byte is returned in R7

1-byte pointer

int / unsigned int, R6 & R7 high bit in R6, low bit in R7

2-byte ptr

long / unsigned long R4-R7 The high bit is in R4, the low bit is in R7

float R4-R7 32-bit IEEE format

generic pointer R1-R3 memory type in R3, high in R2, low in R1

Using the SRC Directive

You can use the Cx51 compiler to generate an assembly code program, and use this program to determine the transfer rules that should be used. The instruction SRC can specify that Cx51 generate an assembly program instead of an object program. For example, the following C program:

#pragma SRC

#pragma SMALL

unsigned int asmfunc1 (

unsigned int arg)

{

return (1 + arg);

}

 

Generates the following assembler:

; ASM1.SRC generated from: ASM1.C

NAME ASM1

?PR?_asmfunc1?ASM1 SEGMENT CODE

PUBLIC _asmfunc1

; #pragma SRC

; #pragma SMALL

;

; unsigned int asmfunc1 (

RSEG ?PR?_asmfunc1?ASM1

USING 0

_asmfunc1:

;---- Variable 'arg?00' assigned to Register 'R6/R7' ----

; SOURCE LINE # 4

; SOURCE LINE # 6

; return (1 + arg);

; SOURCE LINE # 7

MOV A,R7

ADD A,#01H

MOV R7,A

CLR A

ADDC A,R6

MOV R6,A

; }

; SOURCE LINE # 8

?C0001:

RIGHT

; END OF _asmfunc1

END

In this example, the function name asmfunc1 is prefixed with an underscore in the assembler to indicate that the parameters are passed through registers. The parameter arg is passed through registers R6 and R7.

The following program is an assembler compiled from the same source program, except that the NOREGPARMS directive is used:

; 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 ?PR?asmfunc1?ASM2

USING 0

asmfunc1:

; SOURCE LINE # 5

; SOURCE LINE # 7

; return (1 + arg);

; SOURCE LINE # 8

MOV A,arg?00+01H

ADD A,#01H

MOV R7,A

CLR A

ADDC A,arg?00

MOV R6,A

; }

; SOURCE LINE # 9

?C0001:

RIGHT

; END OF asmfunc1

END

 Note that in the example, there is no underscore prefix to the function name asmfunc1, and the parameters are passed in the ?asmfunc1?BYTE segment.

Keywords:C51 Reference address:C51 Compiler - Advanced Programming Techniques (3) - Interface between C language and assembly

Previous article:C51 Compiler - Advanced Programming Skills (4) - Register Application
Next article:C51 Compiler - Advanced Programming Skills (2) - Segment Naming Convention

Recommended ReadingLatest update time:2024-11-17 02:34

Centralized and accurate delay method in keil c51 programming
1Using timer/counter to achieve precise delay Single-chip microcomputer systems usually use 11.059 2 MHz, 12 MHz or 6 MHz crystal oscillators. The first type is easier to generate various standard baud rates, and the latter two types have a machine cycle of 1 μs and 2 μs respectively, which is convenient for accurat
[Microcontroller]
c51: Read the DS1820 serial number and display it
//DS1820 application, read the DS1820 serial number and display it. #include reg51.h #include intrins.h //Variable declaration #define uchar unsigned char #define uint unsigned int floating digit ="0123456789ABCDEF"; //Delay 1 millisecond program void delayms(); //Delay s milliseconds program  void delay
[Microcontroller]
c51: Read the DS1820 serial number and display it
LCD 12232F serial port C51 program (ST7920)
Because the 12232F display is commonly used, but what we usually see are some parallel port assembly programs, and there is no parallel port program corresponding to 12232F, and the serial port C51 program is very rare. In order to facilitate everyone's use, this site has debugged it out. Please give your advice!  /*
[Microcontroller]
Keil C51 uses ANSIC standard keywords (32 in total)
Serial number Keywords use illustrate 1 auto Memory Type Description Used to describe local variables. The default value is 2 break Program Statements Exit
[Microcontroller]
Position relationship of bit fields in bytes in C51
 typedef struct   {     uchar DC0_ALA:1; //Power supply 0 alarm     uchar DC1_ALA:1; //Power supply 1 alarm     uchar AC_ALA:1; //Power failure alarm     uchar UN_H_ALA:1; //Same frequency channel machine unlock alarm     uchar UN_L_ALA:1; //Inter-frequency channel machine unlock alarm     uchar FAR_ALA:1; //Remote co
[Microcontroller]
Comparison of interrupt systems among C51, STM32 and S3C2440
/*  Name: Comparison of interrupt systems of C51, STM32 and S3C2440  Description: As for the interrupt systems of these three chips, as far as I know now, I would say: almost the same. Here, almost the same means that the interrupts are essentially the same. First, the interrupt source applies (such as triggering an e
[Microcontroller]
How to call each other between C51 file and assembly language file?
How to call functions in C51 file and assembly language file? Answer: The method of calling a function in an assembly language file is the same as calling a function in assembly language, such as: LCALL DISPLAY To call a function in assembly language in a C language file, you must declare it first and then call
[Microcontroller]
C51 precise delay
Using C language to program the microcontroller undoubtedly saves a lot of time. More efficient code can be developed in a very short time. It is also much more convenient for program maintenance and expansion than assembly language.              But C language also has its shortcomings, that is, the precise control o
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号