Detailed Description
The following description of the embodiments of the present invention will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present invention, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
Referring to fig. 1, fig. 1 is a flowchart of an embodiment of a procedure call method according to the present invention, where the method includes:
step 11: the addresses of variables in the second program are fixed in the first program.
The first program and the second program are compiled independently, and the first program calls the second program; for example, there are three programs A, B and C in the compiler, programs A, B and C are compiled independently of each other, but program B or a middle molecular function of program B is called when executing program a and/or C.
The variables in the second program may be assigned fixed addresses and the addresses used by these variables are reserved in the first program to ensure that the data is not covered. For example, the addresses of variables A1 and A2 required to be used in the second program may be defined as Add1 and Add2, and the addresses Add1 and Add2 may be reserved in the first program, and the two addresses may be avoided when the addresses are allocated to the variables in the first program.
Step 12: the first program calls the second program to protect registers required for running the first program.
In a specific embodiment, when the first program calls the second program, the registers required by the first program running in the processor are protected, so that data in the registers are kept unchanged before and after the second program is called, and the situation of register address conflict is avoided.
In a specific embodiment, the processor is a chip that does not have a function of handling the problem that the variable addresses overlap each other when independently compiling, such as an 8051 single-chip microcomputer, and the registers required by the first program are kernel-related registers, and specifically may be at least one of 4 sets of working registers R0 to R7, an Accumulator (ACC), an auxiliary register (B), or a program status flag register (PSW, programStatus Word), etc.
The 4 groups of working registers R0-R7 correspond to 00H-1 FH units in the memory, and 0 groups: 00H-07H units, group 1: 08H-0 FH unit, group 2: 10H-17H units, 3 groups: the 18H-1 FH units, only one of the four groups is a current working register, 8 units in the current working register are used by a program, the current working register is determined by a third bit RS0 and a fourth bit RS1 in a program state flag register PSW, the third bit RS0 and the fourth bit RS1 in the program state flag register PSW have four states of 00, 01, 10 and 11, and 0 group, 1 group, 2 group and 3 group of the working registers are respectively designated, and the current working register group can be changed only by changing the values of the third bit RS0 and the fourth bit RS1 by instructions, so that 32 working registers can be selected in practice; the accumulator ACC is commonly used for storing the execution number and the operation result of arithmetic or logical operation; the auxiliary register B can be matched with the accumulator ACC in multiplication and trigger operation and can also be used as a general temporary register; the program status flag register PSW may be used to store status information related to instruction execution, and the status information of each bit in the program status flag register PSW is typically formed during instruction execution, or may be changed by a user using a transfer instruction as needed.
For example, the register required by the first program is an accumulator ACC, the data stored in the accumulator ACC is an 8-bit binary number 01000110 before the first program calls the second program, the second program is a summation program, the values of variables in the first program are respectively 2 and 3, for correct calculation, when the first program calls the second program, the accumulator ACC is protected, after the second program is called, the data stored in the accumulator ACC becomes 00000101, after the first program calls the second program, the return value is 5, and then the data in the accumulator ACC is reset to the original value 00100110; this achieves protection of the accumulator ACC during the invocation of the second program by the first program.
For relatively simple CPUs, the simple CPUs do not formulate rules/methods for solving the problem of variable address overlapping during independent compiling in design, and under the conditions of meeting independent compiling of multiple projects/functions and having function calling relations, as programmers cannot know the address allocation condition in advance in the process of programming, the problem of overlapping addresses possibly exists in variables in each project.
Compared with the prior art, the method and the device have the advantages that for the problem that variable addresses are mutually overlapped, the variable addresses in the called program can be fixed, so that the variable addresses in the called program can not be overlapped with the variable addresses in the called program, the problem that the variable addresses are overlapped when a plurality of programs are independently compiled and have function call relations can be avoided, the safety can be further improved by protecting the registers, the condition that the registers collide in the process of calling the programs is effectively avoided, the return value of the programs is incorrect, the variables which should not be modified are modified or the programs run away, and the normal running of the programs is ensured.
Referring to fig. 2, fig. 2 is a flowchart of another embodiment of a program calling method provided by the present invention, where the method includes:
step 21: the variables in the second program are set as global variables.
For convenience of implementation, variables used by the second program may be defined as global variables in the second program in advance, and addresses of the global variables may be fixed as reserved addresses. Taking 8051 single chip microcomputer as an example, the reserved address is an address in a memory, and the memory can be an internal random access memory (RAM, random Access Memory), wherein the internal random access memory comprises a general register area (RAMA), a non-general Register Area (RAMB) and an external random access memory area (RAMC); for example, for the 8051 single-chip microcomputer, variables A1, A2, A3 required to be used in the second program can be defined as global variables, and their addresses are set to 0x8051, 0x8052, 0x8053, respectively, and the addresses are reserved in the first program.
Step 22: the addresses of variables in the second program are reserved in the first program so that the addresses of variables in the first program do not overlap with the addresses of variables in the second program.
If the data in the register is to be avoided from being covered, on the one hand, the addresses of variables used by the second program can be reserved in the first program so as to avoid the addresses of the variables in the second program when the addresses of the variables in the first program are allocated, and the addresses of the variables are prevented from overlapping; specifically, the implementation of the method can be realized by reserved words in the 8051 singlechip.
The address of the variable in the second program reserved in the first program may be an address other than the address of the core-related register; the kernel-related register includes 4 sets of working registers R0-R7, an accumulator, an auxiliary register or a program status flag register, etc.
Since the kernel-related register also has a ram area, the kernel-related register in the ram area needs to be excluded when the address is reserved, so as to avoid that the kernel-related register corresponding to the first program is covered by the variable address in the second program. Addresses of 4 groups of working registers R0-R7 are fixed in the RAMA area; some core-related registers are addressed in special function registers such as accumulators, auxiliary registers or program status flag registers.
Step 23: and calling a second program to protect registers required by running the first program.
The data in the register required to be used for running the first program can be actively protected, and the protection is realized by a push mode.
Further, when the first program calls the second program, a push operation is executed, and data in the register is moved into a preset data protection area of the memory; when the first program finishes calling the second program, the pop operation is executed, and the data in the preset data protection area is moved back to the register.
Step 24: after the second program is called, the registers required to run the first program are restored.
If the data in the register is to be avoided being covered, on the other hand, the operation of popping and pushing the stack can be carried out before the first program is executed to call the second program, even if the second program can temporarily occupy the register when running, the popping operation can be carried out when the second program is completely executed, and the data originally stored in the register can be moved back to the register, so that the data in the register can be prevented from being tampered.
In a specific embodiment, an area may be reserved in the ram and the ram b as a preset data protection area to temporarily store the data in the register, before the first program needs to run the second program, the data in the register is sequentially moved to the preset data protection area, when the first program finishes calling the second program, and the second program is about to exit, the data is moved back from the preset data protection area to the register, so as to realize the protection of the register, and the data in the register is unchanged before and after the first program calls the second program.
In a specific implementation, two independently compiled projects P1 and P2 under an 8051 single chip microcomputer are taken as an example for illustration, a main function in the project P1 calls a sub-function S in the project P2, the project P1 has variables tmp 1-tmp 3, and the number of addresses occupied by the variables in the sub-function S is 3; other more projects or one project calls another project for more variables in multiple sub-functions or sub-functions, and so on.
According to the complexity of the code in the project P1, it is possible that the variables tmp 1-tmp 3 in the project P1 are distributed in the RAMA area, the RAMB area and the RAMC area, the addresses of the four groups of working registers R0-R7 are fixed in the RAMA area, and for the registers such as the accumulator ACC, the auxiliary Register B and the program status flag Register PSW in the special function Register (SFR, spacialFunction Register), the addresses are uniquely determined, and the contents in the addresses cannot be changed due to calling; the addresses of the variable and 4 sets of working registers R0 to R7 allocated in the RAM area cannot be overlapped or the contents cannot be covered due to the execution of the call operation, otherwise, the program operation is wrong, and unexpected results are generated.
If the scheme in this embodiment is not adopted, variable addresses in the engineering P2 may be randomly allocated to the ram area, the ram area or the ram area due to different complexity of codes, and the sub-function S in the engineering P2 may cover or tamper with the variable addresses or the addresses of the registers in the engineering P1, so that unexpected results occur after the main function in the engineering P1 calls the sub-function S in the engineering P2.
After the scheme is adopted, the variable addresses used by the subfunction S in the project P2 are fixed, 3 variable addresses of the subfunction S are reserved in the project P1, part of registers in the related registers of the kernel are needed to be avoided when the addresses are reserved, the variables in the project P1 can freely use the addresses of the related registers of the kernel, so that the variables in the subfunction S in the project P2 can be ensured not to influence the variables distributed in the RAMA area, the RAMB area or the RAMC area in the project P1, meanwhile, the data in the registers can be immediately stacked after the project P1 enters the subfunction S in the project P2 and immediately stacked before the subfunction S is exited, the values of the registers before the subfunction S is called by the project P1 can be ensured not to be covered or tampered, and the conflict can be avoided when the subfunction S is called.
Compared with the prior art, the method of the embodiment reserves the variable address of the second program in the first program in advance, protects the register in a stack entering and exiting mode, can avoid the conflict of the variable address, can avoid the conflict or the falsification of the register, is simple to realize, and can save the space of the RAM; and the variable address conflict and the register address conflict are fundamentally solved by adopting a software programming method, manual re-intervention is not needed, and the operation of a programmer is simplified.
Referring to fig. 3, fig. 3 is a schematic structural diagram of an embodiment of a chip provided by the present invention, where a control chip 30 includes a memory 31 and a processor 32 connected to each other, and the memory 31 is used for storing a computer program, and when the computer program is executed by the processor 32, the computer program is used for implementing a program calling method in the above embodiment, so that overlapping of variable addresses and register conflicts can be effectively avoided; the chip 30 may be a CPU, specifically, it may be an 8051 single-chip microcomputer.
Referring to fig. 4, fig. 4 is a schematic structural diagram of an embodiment of a computer storage medium provided by the present invention, where the computer storage medium 40 is used to store a computer program 41, and the computer program 41, when executed by a processor, is used to implement the program calling method in the above embodiment.
The computer storage medium 40 may be a server, a usb disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory, a magnetic disk, or an optical disk, etc. that may store the program code.
In the several embodiments provided in the present invention, it should be understood that the disclosed method and apparatus may be implemented in other manners. For example, the above-described device embodiments are merely illustrative, e.g., the division of modules or units is merely a logical functional division, and there may be additional divisions when actually implemented, e.g., multiple units or components may be combined or integrated into another system, or some features may be omitted or not performed.
The units described as separate units may or may not be physically separate, and units shown as units may or may not be physical units, may be located in one place, or may be distributed over a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiment.
In addition, each functional unit in each embodiment of the present invention may be integrated in one processing unit, each unit may exist alone physically, or two or more units may be integrated in one unit. The integrated units may be implemented in hardware or in software functional units.
The foregoing is only illustrative of the present invention and is not to be construed as limiting the scope of the invention, and all equivalent structures or equivalent flow modifications which may be made by the teachings of the present invention and the accompanying drawings or which may be directly or indirectly employed in other related art are within the scope of the invention.