A multi-level assembly code segment parsing method, system and device
By employing a multi-level assembly code fragment parsing method, and utilizing linear linked lists and partition lookup tables to achieve automatic replacement of assembly code, the problem of low parsing efficiency in existing technologies is solved, thereby improving the efficiency and accuracy of code modification.
Patent Information
- Application Number
- CN202111551667.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-17
- Publication Date
- 2025-11-07
- Estimated Expiration
- 2041-12-17
AI Technical Summary
Existing technologies struggle to efficiently parse and replace assembly code, especially failing to meet developers' needs for custom modifications and replacements within any specified range, and existing optimizers are inefficient.
A multi-level assembly code snippet parsing method is adopted. By obtaining the assembly file, converting it into an intermediate format representation and connecting it into a linear linked list, analyzing the code information at different levels, constructing a partition lookup table, obtaining replacement information and modifying the linear linked list, the automatic replacement of code snippets is achieved.
It enables automatic analysis and replacement of assembly code at multiple levels, reducing the workload and error probability of developers, and improving the efficiency and accuracy of code modification.
Smart Images

Figure CN114217850B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer information processing, and in particular to a multi-level assembly code segment parsing method, system and device. BACKGROUND
[0002] Because the assembly code loses the original abstract semantics of the high-level language in the process of compilation, it is difficult to understand the logical relationship of the code, the developer needs to handle the storage allocation and input and output of each instruction and each data, and also needs to clarify the working state at each step during program execution, so that the manual modification of the assembly code costs a lot for the developer, and the code amount of the assembly code is generally large, so that the manual modification by the developer is more prone to errors, and the inline assembly method is only suitable for replacing a small code segment, and the extensive use of inline assembly will affect the readability of the high-level language. The existing assembly code optimizer can only identify some defined code features and make modifications based thereon. These code features are only reflected in a small range, and cannot meet the requirements of the developer for customizing and replacing the code in any specified range. The efficiency of reading data from a file is also very low. SUMMARY
[0003] To solve the above technical problems, the purpose of the present application is to provide a multi-level assembly code segment parsing method, system and device, which can analyze different levels of information of the assembly code and replace the corresponding level of assembly code according to the requirements of the developer.
[0004] The first technical solution adopted by the present application is a multi-level assembly code segment parsing method, characterized by comprising the following steps:
[0005] An assembly file is obtained, and assembly code is read according to the assembly file;
[0006] The assembly code is converted into an intermediate format representation and connected in sequence to obtain a linear list;
[0007] The linear list is analyzed to obtain code information at different levels.
[0008] Further, the multi-level assembly code segment parsing method is characterized in that it further comprises a replacement step, specifically comprising:
[0009] A partition lookup table is constructed according to the code information at different levels;
[0010] Replacement information is obtained and combined with the partition lookup table to modify the linear list of the code to be replaced to obtain the intermediate format after replacement.
[0011] Further, the step of reading and generating the assembly code specifically comprises:
[0012] According to the source code, a corresponding assembly file is acquired;
[0013] The compiler is used for compiling the assembly file, and the "-S" option is added to the compiler in the command line, so that the assembly code is obtained.
[0014] Further, the step of converting the assembly code into an intermediate format representation and sequentially connecting to obtain a linear linked list specifically comprises:
[0015] The assembly code is converted into an intermediate format representation;
[0016] The intermediate format representation comprises operator, operand and category information of the assembly code;
[0017] According to the relative position of each assembly code in the original code, the corresponding intermediate format is connected to obtain a linear linked list.
[0018] Further, the step of acquiring replacement information and combining the partition lookup table to modify the linear linked list of the code to be replaced to obtain the replaced intermediate format specifically comprises:
[0019] The replacement information is acquired, and the code segment of the assembly code file of the code to be replaced is searched according to the replacement information and the partition lookup table, so that the range of the code segment is determined;
[0020] According to the range of the code segment, the pointer of the next statement pointed by the statement of the previous one of the entry instruction of the replacement code in the linked list is pointed to the entry instruction of the replaced code, and the next statement of the exit instruction of the code to be replaced is pointed to the next statement of the exit statement of the replacement code;
[0021] The replaced intermediate format is obtained.
[0022] Further, the step of acquiring replacement information and combining the partition lookup table to modify the linear linked list of the code to be replaced to obtain the replaced intermediate format further comprises:
[0023] It is checked whether there is a control dependency and a data dependency on external code in the code segment to be replaced;
[0024] It is checked whether there is a variable and a function defined in the external code in the replaced code.
[0025] The second technical scheme adopted by the present application is a multi-level assembly code segment analysis system, characterized by comprising:
[0026] A reading module is configured to acquire an assembly file and read assembly code according to the assembly file;
[0027] A linear linked list module is configured to convert the assembly code into an intermediate format representation and sequentially connect to obtain a linear linked list;
[0028] The information module analyzes the linear chain table to obtain code information at different levels.
[0029] The second technical solution adopted by the present application is: a multi-level assembly code segment parsing device, characterized in that it comprises:
[0030] at least one processor;
[0031] at least one memory for storing at least one program;
[0032] When the at least one program is executed by the at least one processor, the at least one processor implements the multi-level assembly code segment parsing method as described above.
[0033] The method, system and device of the present application have the following advantages: the multi-level assembly code segment parsing method, system and device proposed by the present application can analyze and extract assembly code at multiple levels, thereby allowing developers to modify and replace code at different ranges of assembly code without relying on specific code patterns. In addition, compared with manually modifying assembly code, the present application can automatically replace specified code segments, reducing the workload and error probability of developers. BRIEF DESCRIPTION OF DRAWINGS
[0034] Figure 1 is a flowchart of the code parsing of the specific embodiment of the present application;
[0035] Figure 2 is a flowchart of the code replacement of the specific embodiment of the present application;
[0036] Figure 3 is a structural block diagram of a multi-level assembly code segment parsing system according to a specific embodiment of the present application. DETAILED DESCRIPTION
[0037] The present application will be further described in detail below in combination with the drawings and specific embodiments. For the step numbers in the following embodiments, they are only set for the purpose of facilitating the description and explanation, and the order between the steps is not limited in any way. The execution order of each step in the embodiments can be adaptively adjusted according to the understanding of those skilled in the art.
[0038] Referring to Figure 1 and Figure 2 , the present application provides a multi-level assembly code segment parsing method, which comprises the following steps:
[0039] S1, obtaining an assembly file and reading assembly code according to the assembly file;
[0040] S1.1, obtaining a corresponding assembly file according to the source code;
[0041] S1.2, based on the compiler to compile the assembly file, and add "-S" option in the command line to the compiler, get the assembly code.
[0042] Specifically, first need to obtain the source code corresponding to the assembly file, in the process of compiling the executable file, by adding "-S" option in the command line to the compiler, so that the compiler output each source file assembly code; at the same time, the compiler needs to use the "--target" option to specify the target platform, the target platform is the environment of the executable file running, to ensure that the corresponding target platform of the assembly code, the generated assembly code will be saved in the form of text file;
[0043] S2, the assembly code is converted to intermediate format representation and connected in order, get linear linked list;
[0044] S2.1, the assembly code is converted to intermediate format representation;
[0045] S2.2, the intermediate format representation includes the operator, operand and category information of the assembly code;
[0046] S2.3, according to the relative position of each assembly code in the original code, the corresponding intermediate format is connected, and the linear linked list is obtained.
[0047] Specifically, each line of assembly code can be divided into two parts from the structure: operator and operand; Operator represents the function of the code or provides indication information, and the operator provides the parameters required for the execution function. The number of operands is variable according to the requirements of the instruction, which can be from 0 to 3. According to the different functions of the assembly code, the assembly code can be divided into three categories: label, pseudo instruction and instruction. Label is used for instruction jump and identification of code range; Pseudo instruction provides additional auxiliary information for the compilation process, such as defining the length and type of data. They do not directly participate in the execution of the code and do not produce real machine code; Instruction is the basic unit of CPU running, which controls the execution of CPU. The general intermediate representation generated by the code saves the original operator, operand and category information of the assembly code, so as to analyze and restore the subsequent code. The intermediate representation of each code is connected according to the relative position in the original code, forming a linear linked list, which represents all the codes of the assembly file. At the same time, the order of the assembly code is very important, unless the instruction jump caused by the control flow related instruction, the instruction is generally executed from top to bottom, so the order of the assembly code needs to be read from top to bottom according to the original order, and this order information is also reflected in the relative position relationship of different intermediate representations in the linked list.
[0048] S3, analyze the linear chain table to obtain code information at different levels;
[0049] Specifically, after reading all the assembly code, the chain table formed by the intermediate representation obtained previously is analyzed to obtain information at different levels in the code: the instruction level is the smallest level of information, which is saved in the intermediate representation of each instruction, such as the pointer value in the chain table, the sequence number of the instruction, the information of the previous and subsequent instructions, etc. A basic block is a collection of instructions, and it is guaranteed that there is no jump instruction jumping out of the middle of the basic block. Its information includes: label name, entry instruction, exit instruction, previous basic block, etc. The function level contains multiple basic blocks, and the basic blocks constitute a control flow graph. Its information includes: function name, function parameter, return value. Different instructions are classified into different sections according to their functions, forming a section level, such as a read-only data section and a code section, etc. Its information includes: the pointer of the location of the section, the number of instructions contained in the section, etc. The largest level is the compilation unit, which contains all the code information in an assembly file.
[0050] S4, construct a partition lookup table according to the code information at different levels;
[0051] Specifically, each level will correspond to a partition lookup table to determine the partition information in each level, so as to quickly search for the partition and find the partition location.
[0052] Through this identification part, the assembly code can be parsed and code information at different levels can be obtained, and the generated chain table structure retains all the original information in the original assembly code file.
[0053] S5, obtain replacement information and combine the partition lookup table to modify the linear chain table of the code to be replaced to obtain the intermediate format after replacement;
[0054] S5.1, obtain the replacement information and find the code segment of the assembly code file of the code to be replaced according to the replacement information and the partition lookup table, and determine the range of the code segment;
[0055] S5.2, according to the range of the code segment, point the next statement pointed by the statement of the previous one of the entry instruction of the replacement code in the chain table to the entry instruction of the replaced code, and point the next statement of the exit instruction of the code to be replaced to the next statement of the exit statement of the replacement code;
[0056] S5.3, obtain the intermediate format after replacement.
[0057] Specifically, the code replacement is performed according to the parsing results of the replaced assembly code file and the replacement assembly code file obtained above, and the specified hierarchical replacement information, such as function name, file name, etc. First, the instruction and related hierarchical information represented by two chains are obtained by using the parsing information of the two files, and then the corresponding code segments are found on both sides according to the specified replacement information, the range and dependency information of the code segments are determined. The pointer of the next statement pointed by the statement of the previous instruction of the replacement code entry instruction in the chain table is pointed to the entry instruction of the replaced code, and then the next statement of the exit instruction of the replaced code is pointed to the next statement of the exit statement of the replacement code, so that the basic replacement of the code is completed.
[0058] S5.4, checking whether there is a control dependency and a data dependency on external code in the code segment to be replaced;
[0059] S5.5, name conflict analysis.
[0060] Specifically, the control dependency refers to the existence of a branch jump statement in the replaced code that is illegal for the current code, and the data dependency refers to the existence of a data definition in the replacement code that is illegal for the current code. Since these dependencies exist in the replacement file, all the instructions related to the dependencies in the replacement file also need to be copied to the replaced file, which can be directly added to the end of the chain table in the corresponding hierarchical area, for example, if there is a data dependency caused by some undefined variable, the instruction related to the data definition in the data section of the replacement file needs to be added to the end of the data section of the replaced file.
[0061] In addition, name conflict analysis is also needed to prevent name conflicts between the original code and the replacement code. If a name conflict occurs, a new name needs to be generated for one of them. All defined variable names and function names in the replaced code are checked in sequence to see if they appear in the symbol information recorded in the partition lookup table. If found, it means that the same name conflict occurs. The modification method is to generate a random number composed of 6 digits as a suffix and append it to the original definition name as a new function or variable name, and then perform name conflict analysis again until the name conflict is eliminated.
[0062] S6, restoring the replaced intermediate format to assembly code and outputting to obtain the assembly code file after replacement.
[0063] Specifically, the intermediate format is restored to assembly code and outputted, the first statement in the intermediate representation chain table is taken as the entry statement, the instructions represented by each structure are printed as code strings into a text file, and all statements in the entire chain table are printed in sequence to obtain the assembly code file after replacement.
[0064] The part can automatically match and replace different levels of code segments in the assembly file with target assembly code.
[0065] Compared with the existing assembly level optimizer, the method can replace any specified code at multiple levels, and remove the restrictions on the number of instructions and specific code patterns
[0066] As shown in Figure 3 A multi-level assembly code segment parsing system includes:
[0067] A reading module is configured to obtain an assembly file and read assembly code according to the assembly file.
[0068] A connection module is configured to convert the assembly code into an intermediate format representation and connect the assembly code in sequence to obtain a linear chain table.
[0069] An analysis module is configured to analyze the linear chain table to obtain code information at different levels.
[0070] Further, the system further includes:
[0071] A construction module is configured to construct a partition lookup table according to the code information at different levels.
[0072] A replacement module is configured to obtain replacement information, combine the partition lookup table, modify the linear chain table of the code to be replaced, and obtain an intermediate format after replacement.
[0073] A restoration module is configured to restore the intermediate format after replacement into assembly code and output to obtain an assembly code file after replacement.
[0074] The contents in the above method embodiments are applicable to the system embodiments, the system embodiments specifically implement the same functions as the above method embodiments, and achieve the same beneficial effects as the above method embodiments.
[0075] A multi-level assembly code segment parsing device includes:
[0076] At least one processor;
[0077] At least one memory configured to store at least one program;
[0078] When the at least one program is executed by the at least one processor, the at least one processor implements a multi-level assembly code segment parsing method as described above.
[0079] The contents in the above method embodiments are applicable to the device embodiments, the device embodiments specifically implement the same functions as the above method embodiments, and achieve the same beneficial effects as the above method embodiments.
[0080] The above is a specific description of the preferred embodiments of the present application, but the present application is not limited to the described embodiments, and those skilled in the art can make various equivalent modifications or replacements without departing from the spirit of the present application, and these equivalent modifications or replacements are all included in the scope defined by the claims of the present application.
Claims
1. A multi-level assembly code snippet parsing method, characterized by, The method comprises the following steps: S1, obtaining an assembly file and reading assembly code according to the assembly file; S2, converting the assembly code into an intermediate format representation and connecting in sequence to obtain a linear chain table; S3, analyzing the linear chain table to obtain code information at different levels; specifically: After reading all the assembly code, the chain table formed by the intermediate representation obtained previously is analyzed to obtain information at different levels in the code: the instruction level is the smallest level of information, which is saved in the intermediate representation of each instruction; A basic block is a collection of instructions, and it is guaranteed that there is no jump instruction jumping out of the basic block in the middle, and its information includes: label name, entry instruction, exit instruction, and previous basic block; The function level contains multiple basic blocks, and the basic blocks constitute a control flow graph, and its information includes: function name, function parameter, and return value; Different instructions are classified into different sections according to their functions, forming a section level; its information includes: a pointer to the location of the section, and the number of instructions contained in the section; The largest level is a compilation unit, which contains all the code information in an assembly file; S4, constructing a partition lookup table according to the code information at different levels; S5, obtaining replacement information and combining the partition lookup table to modify the linear chain table of the code to be replaced to obtain the intermediate format after replacement; specifically including: Obtaining replacement information and finding the code segment of the assembly code file of the code to be replaced according to the replacement information and the partition lookup table to determine the range of the code segment; According to the range of the code segment, the pointer of the next statement pointed by the statement of the previous entry instruction of the code to be replaced points to the entry instruction of the replacement code, and the next statement of the exit instruction of the replacement code points to the next statement of the exit statement of the code to be replaced; Obtaining the intermediate format after replacement; S6, restoring the intermediate format after replacement to assembly code and outputting to obtain the assembly code file after replacement.
2. The method of claim 1, wherein the multi-level assembly code snippet is parsed by: The step of obtaining an assembly file and reading assembly code according to the assembly file specifically includes: Obtaining a corresponding assembly file according to source code; Compiling the assembly file based on a compiler, and adding a "-S" option to the compiler in the command line to obtain assembly code.
3. The method of claim 2, wherein the multi-level assembly code snippet is parsed by: The step of converting the assembly code into an intermediate format representation and connecting in sequence to obtain a linear chain table specifically includes: Converting the assembly code into an intermediate format representation; The intermediate format representation includes operator, operand and category information of the assembly code; According to the relative position of each assembly code in the original code, the corresponding intermediate format is connected to obtain a linear chain table.
4. The method of claim 1, wherein the multi-level assembly code snippet is parsed by: The step of obtaining replacement information and combining the partition lookup table to modify the linear chain table of the code to be replaced to obtain the intermediate format after replacement further includes: Checking whether there is control dependence and data dependence on external code in the code segment to be replaced; Checking whether there are variables and functions defined in the external code in the code after replacement.
5. A multi-level assembly code snippet parsing system, characterized in that, The system uses the method of any one of claims 1-4 for assembly code segment parsing, and the system comprises: A reading module for obtaining an assembly file and reading assembly code according to the assembly file; A connection module is configured to convert the assembly code into an intermediate format representation and connect them in sequence to obtain a linear chain; An analysis module is configured to analyze the linear chain to obtain code information at different levels.
6. A multi-level assembly code snippet parsing apparatus, characterized by, The method comprises the following steps: at least one processor; at least one memory for storing at least one program; When the at least one program is executed by the at least one processor, the at least one processor implements the method as claimed in any one of claims 1-4. A multi-level assembly code segment parsing method according to any one of claims 1-4.
Citation Information
Patent Citations
Peephole optimization method of C compiler
CN108427558A
Method and device for generating syntax tree of code file and electronic equipment
CN111381828A