Use and parameter transfer and data return of assembly in C51

Source: Internet
Author: User
1, in the C file to embed assembly code in the following way to add the assembly code:
#pragma ASM
; Assembler Code here
#pragma endasm
2, in the Project window contains assembly code of the C file on the right button, select "Options for ...", click on the right "Generate assembler SRC file"
and "Assemble SRC File", so that the check box from gray to black (valid) state;
3, according to the choice of compiling mode, the corresponding library files (such as Small mode, is keil\c51\lib\c51s. LIB) to join the project, the document must be used as the project's most
After the document;
4, compile, you can generate the target code.

Let's take an example:
#i nclude
void Main (void)
{
P2=1;
#pragma asm
MOV R7, #10
Del:mov R6, #20
DJNZ r6,$
DJNZ R7,del
#pragma endasm
p2=0;
}


C51 Call assembler function
1. function calls without parameter passing
First example: Example.c and Example.a51 are the two files in the project
example.c***********************************************
extern void delay100 ();

Main ()
{delay100;}
example.a51***********************************************
? PR? DELAY100 SEGMENT CODE; To define a segment in the program store
Public DELAY100; declaring functions
Rseg? PR?    DELAY100; The function can be placed anywhere by the connector

DELAY100:
MOV R7, #10
Del:
MOV R6, #20
DJNZ r6,$
DJNZ R7,del
Ret
End
In the example.c file, declare the external function first, and then call it directly in main.
In the Example.a51,
? PR?  DELAY100 SEGMENT CODE; function is to define a segment in the program store, DELAY100 is the segment name,? The PR? Indicates that the section is in the program store
Public DELAY100; function is to declare functions as public functions
Rseg? PR?    DELAY100; Indicates that the function can be placed anywhere by the connector, RSEG is the property of the segment name
The beginning of the segment name is PR and is intended to be compatible with the C51 internal naming transformation, as follows:
CODE-? PR?
Xdata-? Xd
Data-? Dt
Bit-? BI


Before writing this article, wrote a test procedure, but always pass, see the assembly code found in C file statements are not compiled, how can not find the reason, depressed ~~ 
        Finally on the Internet to search a test program, the copy of my program in the past, can be compiled successfully, strange, in my project is not, I noticed that my project after compiling a WARNING:
* * * WARNING L7:module NAME not UNIQUE
    module:  8.obj (8)
         and the same program code in another project, there is no warning, it must be the warning statement, which refers to name, it is related to the name, immediately A51 file to change the name (the original C file and A51 file name), compile, haha, Warning gone, look at the assembly code, everything according to the expected, alas, a name harm me not shallow ah, remember Oh, c files and A51 files can not use the same file name, but I do not know why this, there are experts know the words please tell, or do today's homework it.
         today say a function call with parameter transfer, there are two ways to pass parameters between C51 and assembly, one is passing parameters through registers, In the C51, different types of actual participants are deposited into the corresponding registers, only the corresponding registers are operated in the Assembly, that is, the purpose of passing the parameters is achieved.
    Registers of different types of data and their passing parameters are shown in the following table:
 

Parameter type Char Int Long/float Universal pointers
1th One R7 R6&r7 R4-r7 R1-r3
2nd One R5 R4&r5 R4-r7 R1-r3
3rd One R3 R2&r3 -- R1-r3


For example, void delay (unsigned char i, unsigned int j) When executing a statement delay (10,1000), 10 is deposited in the R7, 1000 high is stored in the R4, and the low is stored in the R5. In the assembly statement from these registers to take a few, and then operate on the line, it is very simple to say, hehe ~
Here's the simplest example, it doesn't make sense, it's a fool's program:

main.c*********************************************
extern void DELAY (unsigned char i,unsigned int j);

Main ()
{
DELAY (10,1000);
while (1);
}
DELAY. a51********************************************
? Pr?_delay? DELAY SEGMENT CODE
Public _delay
Rseg? Pr?_delay? DELAY
_delay:
DJNZ r4,$
DJNZ r5,$
DJNZ r7,$
Ret
End
Also say is that the function name to be underlined before, indicating that there are parameters passed function calls!


(2) The registers used by the function return value

return value type Register Description
Bit C Returned by a specific sign bit
char/unsigned CHAR/1 byte pointer R7
int/unsigned INT/2 byte pointer R6&r7 High in R6
long/unsigned LONG/3 byte pointer R4-r7 High in R4
Float R4-r7 32bit IEEE format, exponent and symbol bit in R7
Universal pointers R1-r3 Storage type in R3, high in R2


Instance:
main.c****************************************
unsigned int example (unsigned char i)
{
return (i*i);
}

Main ()
{example;
#pragma asm
djnz r7,$
djnz r6,$
#pragma endasm
while (1);
} The

function returns the value in R6,r7.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.