## 4 Approaches to Source Code Transformation into Vector-Based Representations
### Histograms of Assembly Language Instruction Opcodes  
According to [18; 28], histograms of assembly language instruction opcodes tend to outperform complex neural network-based approaches to program transformation into vector-based representations in the algorithm classification task. According to [44; 45], opcode frequency histograms also perform well in malware detection problems. In Python, the bytecode is an implementation detail of the CPython interpreter. The **dis** module [46] available in the standard library allows disassembly of the CPython bytecode. An example of a simple Python function computing factorial is shown in Figure 8a; the corresponding CPython assembly language instructions, their labels, line numbers, and arguments are shown in Figure 8b. The AST describing the program in Figure 8a is shown in Figure 4b. Figure 8: (a) A simple Python program; (b) the CPython bytecode for the simple Python program. Figure 6: Markov chains of different order constructed from a sample AST shown in Figure 4a,b: (a) first-order AST-based Markov chain; (b) second-order AST-based Markov chain. Figure 7: Program embedding into vector space based on \(n\)-th order Markov chains. The process of program embedding into vector space based on opcode histograms is described by Algorithm 3. The algorithm takes in an ordered set of programs and outputs an ordered set of vectors. ```
Input:\(S\triangleright\) a set of source codes to be converted into vector representations. 1. \(I=\varnothing\triangleright\) the ordered set of instruction type multisets. 2. \(O=\varnothing\triangleright\) the set of known instruction types. 3. **For each** source code \(s\in S\)**do:**
4. **Construct** an AST \(a\) for program \(s\) using the **ast.parse** function [41]. 5. **Build** a code object \(c\) from the AST \(a\) using the **compile** Python function. 6. **Disassemble** a multiset of instruction types \(i\) from \(c\) using **dis**[46]. 7. \(I\gets I\cup\{i\}\triangleright\) add the multiset of instruction types \(i\) to \(I\). 8. \(O\gets O\cup i\triangleright\) add discovered instruction types to the \(O\) set. 9. **End loop. **
10. \(V=\varnothing\triangleright\) the ordered set of vectors. 11. **For each** multiset of instruction types \(i\in I\)**do:**
12. \(\overset{\rightarrow}{v}=\varnothing\). 13. **For each** known instruction type \(o\in O\)**do:**
14. \(\omega=\frac{1}{|I|}[\{i_{k}:i_{k}\in i\wedge i_{k}=o\}]\triangleright\) instruction type occurrence frequency. 15. \(\overset{\rightarrow}{v}\leftarrow\overset{\rightarrow}{v}\cup\{\omega\}\)\(\triangleright\) add a new component to the program vector. 16. **End loop. **
17. **End loop. **
18. **Return** the \(V\) set of source code vector-based representations. ```  
**Algorithm 3**Source code embedding into vector space based on opcode histograms  
The histograms of relative occurrence frequencies of CPython assembly language instructions obtained for sample programs listed in the first row of Table 2 are shown in Figure 9; these histograms were constructed using Algorithm 3. The first approach highlighted in magenta in Figure 9 uses a loop for recurrent formula computation (see the first row of Table 2), and the second approach highlighted in cyan uses a function that calls itself recursively. Figure 9: Histograms of CPython assembly language instructions obtained using Algorithm 3 for programs listed in the first row of Table 2. The programs use different algorithms to implement a recurrent formula [19]; the dark blue color highlights overlapping histograms. According to Figure 9, the CPython assembly language instruction occurrence frequencies differ significantly depending on the algorithm implemented in source code. This is consistent with the results obtained in [18; 28]. We observed that histograms for programs solving different tasks differ even more, as this is important in the considered task detection problem. Figure 10 summarizes the process of opcode histogram-based embedding of programs into vector space.