In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "sample analysis of C++ embedded assembly", which is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn the "sample analysis of C++ embedded assembly" this article.
Assembly language
Assembly language is not only a powerful programming language, but also a language that makes use of all the hardware features of the computer and can directly control the hardware. In assembly language, mnemonics (Memoni) are used instead of opcodes, and address symbols (Symbol) or labels (Label) are used instead of address codes. In this way, the binary code of the machine language is replaced by symbols, which turns the machine language into assembly language.
Assembly language is easier to read, write, debug and modify than machine language. at the same time, it also has the advantages of fast execution speed and less memory space. However, when writing complex programs, the amount of code of assembly language is larger than that of high-level languages, and assembly language depends on specific models, so it can not be widely used, so it can not be directly transplanted between different processing models. Although its portability is not good, the efficiency is very high. The assembly language program written for the specific hardware of the computer can give full play to the function and specialty of the computer hardware, and the program is refined and of high quality. so assembly language is still a commonly used and powerful underlying development language.
The characteristics of assembly language
Assembly language instructions are expressed by some mnemonic symbols with corresponding meanings, so it is easier to master and use than machine language. But because of the direct use of CPU resources, it is relatively complex compared to high-level programming languages. Assembly language programs can be summed up with the following main features.
1. Related to hardware: Assembly language instruction refers to a symbolic representation of machine instructions, and different types of CPU have different machine instruction systems, so there is a close relationship between assembly language programs and machines. In other words, it is impossible for different types of CPU to share the same assembly code, which reduces the portability and versatility of assembly language, which is an inherent defect of assembly language.
two。 It maintains the advantages of machine language and has the characteristics of directness and simplicity: precisely because assembly language has the characteristic of "machine relevance", programmers can give full play to their ingenuity when writing programs in assembly language. make a reasonable arrangement of all kinds of resources inside the machine, so that they are always in the best state of use, and the final effect is that the execution code of the program is short and fast, so Assembly language is an efficient programming language. In addition, assembly language can effectively access and control all kinds of hardware devices of the computer, such as disk, memory, CPU, Imax O port and so on, so as to maximize the utilization of resources.
3. The programming is complex: Assembly language is a machine-oriented language, and its assembly instructions basically correspond to machine instructions one by one, so assembly instructions have the same single function and specific characteristics as machine instructions. If you want to finish something, you must arrange every step of CPU's work. In addition, when writing assembly language programs, we should also consider the limitations of specific models, the details and limitations of assembly instructions, and so on.
4. It is often used in conjunction with high-level language, and it is widely used: in some cases, such as directly operating CPU execution interrupts to achieve thread scheduling, saving CPU registers to store / restore thread state, etc., it is impossible to use high-level language alone, with the help of assembly language, but only using assembly language, large programs may need to pay several times more work than high-level language, and sometimes it is not necessary. Therefore, we can embed assembly statements in the high-level language, so that only part of the code that needs to be efficient is completed in assembly language, and the rest of the framework is built in high-level language, which not only ensures the efficiency but also reduces the complexity of the code. This kind of cooperation is often encountered in large-scale software development and is widely used.
Note:
All the code in this article was tested in my own VS2008, and due to differences in the environment, there is no guarantee that it will run on all compilers.
1. Introduction to embedded assembly
In C++, assembly language can be embedded with the _ _ asm keyword.
For example
Int main () {_ _ asm {/ / assembly! Mov eax,0} return 0;} 2. Assemble version Hello, World!
We know that in C++, you can use the printf function to output. (if you use cout, you need to use techniques such as operator overloading, which is not convenient here.)
Tip:
In assembly, the instruction to call a function is called CALL.
The parameters of the function are saved on the stack.
Then we can start writing. First of all, take a look at the normal version of C++:
# include#includeconst char * S1 = "Hello, World\ n", * S2 = "pause"; int main () {printf (S1); system (S2); return 0;}
For convenience, let's disassemble the normal version first, and the result is:
Printf (S1)
00BD13CE mov esi,esp
00BD13D0 mov eax,dword ptr [S1 (0BD7038h)]
00BD13D5 push eax
00BD13D6 call dword ptr [_ _ imp__printf (0BD82C4h)]
00BD13DC add esp,4
00BD13DF cmp esi,esp
00BD13E1 call @ ILT+315 (_ _ RTC_CheckEsp) (0BD1140h)
In the first sentence, mov esi,esp checks whether the stack is working properly later.
In the second sentence, the 0BD7038h in parentheses of mov eax,dword ptr [S1] is the address. Leave it alone, meaning put the address in the eax.
In the third sentence, push eax puts the address just put into the eax into the stack, which is actually putting the parameters on the stack.
The fourth sentence, call dword ptr [_ _ imp__printf]
_ _ imp__printf is the compiled result of the printf function. The beginning of the underscore indicates that it is a function.
When we write inline assembly, we can write printf directly.
The fifth sentence, add esp,4
In fact, it is manually leveling the stack. Before, we put a 4-byte S1 into the stack. Now we move the esp pointer, that is, the top pointer of the stack, down (the stack goes from high address to low address), leveling the stack.
The last two sentences, regardless of it, is to make sure that esi and esp are equal, because the stack has been flattened manually before, combined with the first sentence, it should be equal here, and it should be fine if you don't write it.
The final inline assembly should look like this:
# include#includeconst char * S1 = "Hello, World\ n", * S2 = "pause"; int main () {_ asm {mov eax,dword ptr [S1] push eax call dword ptr [printf] add esp,4 mov eax,dword ptr [S2] push eax call dword ptr [system] add esp,4} return 0;}
The result of the operation is normal.
3. Inline assembler Atom B
Aheb problem, which requires both scanf and printf
First of all, note that the parameters of the function are stored backwards in the stack. (note: this C standard is not specified, but the assembly language itself is very dependent on the environment, so ignore it for the time being.)
For example
Scanf ("% d% d", & aPhoneBy)
If translated into assembly, it should be like this (the following is pseudo code)
Push & bpush & apush "d d" call scanf
Then we can start writing.
In the part of scanf, note that the first two parameters are placed in the address, so you can't use the MOV instruction but the LEA instruction.
Lea eax, [a]
Means to put the address of an in the eax.
The other parts are not difficult. Notice how much add esp is added when the stack is finally leveled, plus the addition of the size of each parameter.
For example, scanf, each is a 4-byte address, a total of 12 bytes.
Complete code
# include#includeconst char * S1 = "% d% d", * S2 = "% d\ n", * S3 = "pause"; int ameme b Int main () {_ asm {lea eax, [b] push eax lea eax, [a] push eax mov eax,dword ptr [S1] push eax call dword ptr [scanf] add esp,12 mov eax, [a] add eax, [b] push eax mov eax Dword ptr [s2] push eax call dword ptr [printf] add esp,8 mov eax,dword ptr [s3] push eax call dword ptr [system] add esp,4} return 0 } the above is all the content of this article "sample Analysis of C++ embedded Assembly". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.