In Xiao Ming's second C + +, we see that Xiaoming used the integer, floating-point type and other data types, then these data types in the computer is how to express. Their operation is how to achieve. In addition, the main function calls the area calculation function calculate what exactly happened. After the translation, a high-level language written statements into a lot of independent machine language can not be divided, that is, instructions. How many instructions are running on a computer? Let's look at the above questions in the following way. representation and operation of numbers
(1) Why is the binary system.
We know that computers use binary systems, that is, only 0 and 1. Why would you do that? Why not use the popular decimal. In my opinion there is only one reason, and that is to use the binary system simply. 0 and 1 are determined according to the voltage value. If you use the decimal, you need to use sophisticated hardware to determine exactly how many volts. However, if used in binary, "1" with a voltage, "0" with no voltage, so the measurement of whether the voltage is much simpler, and not easy to error.
(2) The representation of a number
Number can be divided into three kinds: unsigned number, signed number, floating point. Unsigned numbers only represent 0 and positive integers, and signed numbers can represent negative integers, 0, and positive integers. Floating point numbers are used to represent decimals that can be positively negative.
Binary and decimal is similar in fact, the difference is that binary is the binary, decimal is every ten. Decimal uses a bit-value notation:
123=1*10^2+2*10^1+3*10^0
In the bit value notation, each bit weight is different, the high position weight is small, so although 1:3 small, but the hundred is larger than the digit. Similarly, binary 1010:
1010=1*2^3+0*2^2+1*2^1+0*2^0
So how binary and decimal are converted.
binary conversion to decimal directly using the above formula can get decimal representation, such as 1010=8+2=10;
Decimal to binary system is a bit of a hassle. We can list an equation:
10=a3*2^3+a2*2^2+a1*2^1+a0*2^1
So to convert 10 to binary, the key is to determine the value of the a3-a0.
First, because 10 is an even number, so the A0 value is 1;
Then the equation is divided by 2 at the same time, getting:
5=a3*2^2+a2*2^1+a1*2^0
Because 5 is odd, the A1 value is 1, and then the equivalent is divided by 2:
2 = a3*2^1+a2*2^0
Because 2 is an even number, so the A2 value is 0;
The same A3 value is 1;
So the binary representation of ten is 1010.
The decimal binary method can be summed up as follows:
A. First construct a binary expansion of N;
B. Determine the lowest bit value based on the parity of N, and then divide the equation on both sides by 2;
c. Determine the value of the symbol bit according to the symbol of N.
In a computer, it is called the original code that is converted by this method.
There is also a representation called the complement:
If a number is a positive integer, its complement is the original code. If a number is negative, its complement is the original code to take counter plus one.
For example, 10 of the complement is 01010, and-10 of the complement is 10110 (10101+1);
As for why have the complement, is for the operation convenient, after the calculation of the number you know.
Let's talk about the decimal representation now.
The floating-point numbers below are all based on the IEEE 754 standard.
The representation of floating-point numbers is similar to that of the decimal science notation. The law of Science and Technology stipulates that there is only one valid digit before the decimal point, then the mantissa after the decimal point, multiplied by 10 n times. For example 6.023*10^23, where 023 is the mantissa, 23 is the order number. Similarly, floating point numbers use this idea:
where float is single-precision, double is double, so double can represent more values than float because the double has more digits. As you can see from the diagram, float takes up 4 bytes, while the double is eight bytes.
S is the sign bit, E represents the order, and M represents the mantissa.
For example-6.625 says:
The first is a negative number, so the value of the sign bit S is 1;
Second, 6.625 has a binary number of 110.101, which is not a normalized number because there are three valid digits to the left of the decimal point.
It is necessary to normalize to 1.10101, the index is 2, so the order is 2+127=129 (plus 127 is the stipulation);
So-6.625 IEEE floating-point numbers are represented as:
1 10000001 10101000000000000000000
(3) The operation of the number
Previously said with the complement to calculate relatively simple, it is time to reveal the answer.
Let's take a look at the arithmetic of integers first.
The operation of integers is performed with the complement.
The addition of the complement is also very simple, as with decimal addition, if and greater than 2 to carry:
The key to the complement is subtraction, complement subtraction is implemented by addition, as shown in the figure:
is not very magical, this is because the essence of the complement is modulo operation. For example, the clock, I want from seven o'clock to 4 o'clock can be turned counterclockwise three words, can also be transferred clockwise to 4 o'clock. Complement is also the idea. Because only by addition can realize the addition and subtraction operation of arithmetic, so the computer only need an adder Alu can realize the addition and subtraction of the number of operations.
Let's take a look at the multiplication operation:
The multiplication operation is similar to our decimal:
It's also a one-place multiplication and then add up.
The diagram above is the flow and structure of the 32-bit multiplier;
When the multiplication is started, the multiplier and the multiplier are placed in the corresponding position. The controller then determines whether the multiplier will be sent to the 64-bit adder based on the lowest bit of the multiplier, and the product is the register that holds the intermediate result. After the ALU has finished, let the multiplier move to the right and the multiplier to the left, and continue, knowing that the multiplier is 0 From this process, it can be seen that the multiplication operation time is much larger than the time of the addition operation.
Division operations:
Process: The computer is not as smart as humans, and you know whether the divisor is greater than dividend. The division operation of a computer is achieved by subtraction. Each time with dividend minus divisor, if the result is greater than 0, the quotient is 1, if less than 0, the quotient is 0. Finally, because the dividend may be less than 0, you need to add a divisor.
Floating-point number addition operation:
With the decimal science counting method is similar to the addition operation, floating point number addition needs to pass the comparison of order code, alignment, mantissa operation, normalization, take symbols and other steps, the final results, visible, floating-point operations is still a relatively complex process. function Call
Understand the representation and operation of the data, what happens when the main function calls other functions. How the computer completes the function call a series of work.
For ease of viewing, I posted the second piece of code here:
#include <iostream> using namespace std;
Vertex class contains horizontal and ordinate two variables class Point {public:float x,y;};
Float Calculate (point a,point b,point C) {float S;
S= ((a.x-b.x) * (A.Y-C.Y)-(a.x-c.x) * (A.Y-B.Y))/2; Return s>0?
S:-s;
int main () {point *points;
float sum;
int n,i;
cout<< "Please enter n (2<n) to represent the N-side shape:" <<endl;
cin>>n; if (n<3) {cout<< "n cannot be less than 3.
"<<endl;
return 1;
} points = new point[n];
cout<< "Please enter the n coordinates of the convex polygon clockwise:" <<endl;
User input for (i=0;i<n;i++) {cin>>points[i].x>>points[i].y; ///Compute area for (i=1,sum=0;i<n-1;++i) {///This statement implements SUM + = Calculate for the three steps of the algorithm (POINTS[0],POINTS[I],PO
Ints[i+1]);
///Output Area cout<< "This polygon area is" <<sum<<endl;
return 0; }
Before we talk about the function call, we'll talk about two memory allocation policies for C + +:
(1) Stack: Stack belongs to a kind of data structure called "Advanced and Out". This is similar to the daily life of the table tennis bucket, the beginning of the time can be loaded into the bucket table tennis, but to take out from the table tennis drum table tennis, only from the top start to take, but also to start in the order of table tennis in the opposite. Stacks are the data structures. The order of the pressed elements and the output elements is reversed. When you want to use stack space, only to be allocated in order, do not need to find the right space, so the allocation speed is faster. Stacks are often used to store data such as local variables and function parameters. In the program written by Xiao Ming, the Calculate function of S, A, B, c variables, is stored in the stack area.
(2) Heap: Heap is a continuous memory allocation policy, as with the operating system's contiguous memory allocation. The allocation of the heap is similar to the distribution of land, the beginning of a large land, and then a lot of farmers asked to farming, some to three acres, and some 10 acres. Therefore, the allocation of land needs to be done according to the size of the world to be used by farmers to allocate a suitable size to him, the recovery will be adjacent to the free land together as a piece of land. Therefore, the heap allocation needs to find the appropriate memory space and reclaim the memory space, so the allocation speed is slow, often used to hold the global variables and C + + The new statement generated by the memory space is from the heap area. In Xiaoming's program, the points array is stored in the heap area.
function call from the point of view of programming
In the above program, the main function calls the Calculate function, first passes the coordinates of the three points of the polygon to the Calculate function, and then executes the flow from the main function to the Calculate function, to perform the calculation of the area of the program, after the area is calculated, To return the area s to the main function, the main function adds s to sum as part of the polygon area.
look at the function call from the machine's point of view
Why I can better understand the function call process, I will call the calculate function of the statement and the Calculate function corresponding to the assembly code posted:
The main function calls the statement of the Calculate function:
Calculate function:
The above assembly code should belong to the IA32 instruction set architecture, can not read the first to learn.
However, you do not have to fully understand, only know the function call process.
Description of some registers:
As mentioned above, we use stacks to pass process parameters, store return information, save registers for future restores, and store locally. To determine the stack space used by a function call, you need to use two pointers to illustrate:
%EBP: Frame pointer (near stack bottom)
%ESP: Stack pointer (near top of stack)
%eax,%edx,%ECX: Registers that the caller main function needs to save
%EBX,%esi,%edi: Registers to be saved by the caller
function Call Procedure
(1) main using the stack transfer parameters: MOV instructions corresponding to the data removal instructions, it will be three vertex coordinates placed in the stack.
For the transfer of parameters, there are three methods, which are passed by value, passed by the pointer, and passed by reference. The parameters passed by the above function are passed by value.
(2) main uses the call statement to invoke the Calculate function: as shown in the screenshot above, the calling instruction (the 0x40155d corresponding instruction for the first screenshot) points the PC pointer to the Calculate function, and the execution stream shifts from the main function to the calculate, while the It presses the value of the current PC into the stack as the return address.
(3) Calculate saves the current register value and gets the parameters: If the calculate is running with%EBX,%esi,%edi registers, you need to save their values to the stack. At the same time, this step also involves getting the arguments passed from the main function from the stack.
(4) The operation of the calculate instruction: After obtaining the parameter, it can execute the calculate instruction.
(5) Calculate the value of the restore register and writes the return result to the register: if%EBX,%esi,%edi are used, then the values are recovered from the stack, and the return result S is written to return the register.
(6) Calculate calls the RET instruction return to main: Back up the stack and change the current PC value to the saved return address in the stack, return to the main function, the main function returns the result by value return register%EAX, continue to run the following instruction. Pipeline Technology
After analyzing the function call process, let's take a look at how the computer runs these instructions.
One of the most important reasons why computers are so fast includes pipelining. The assembly line technology that the computer uses actually has the similar place with the pipeline technology which uses in the factory or the enterprise.
In the factory, you can see a product production process is a line, this line includes the following processes: Product research, product design, product development, product testing, product sales. Each process requires a department to perform, so a company needs design department, Research and Development Department, Quality Department, sales department and other departments.
The main idea of the pipeline is: These departments are working at the same time, such as research and development department to complete a product, immediately start the next product development, rather than waiting for the product testing and sales. so every department is step-by-step, not slack, eventually the company has been profitable.
Similarly, a similar assembly line is included in the computer's CPU.
the steps required to process an instruction
Take MIPS as an example, we talk about how the computer assembly line is achieved.
(1) value (IF): Read instruction from instruction Register;
(2) decoding and reading the value of the Register (ID): Analysis of the instructions required to perform the operation, but also can read the value of registers;
(3) Perform an operation or compute the address (EX): For operations, at this stage is to perform addition and subtraction operations; for jump instructions, this phase will perform the calculation of the address to which you want to jump.
(4) Read from memory (MEM): The reading of an operand from memory, typically data removal instructions.
(5) write back the result of the calculation to register (WB).
Here, some students may have doubts, some instructions do not seem to need to go through so many steps ah. The reason to unify all the instructions so many steps is that the process is simple after unification, simplicity stems from unity, the premise of the pipeline is that each instruction is the implementation of the process is unified, so in fact some instructions do not need a certain period, will also be painted, indicating that this stage is free, but it is necessary.
Five steps corresponding to the functional components
(1) Instruction register and its control parts;
(2) Decoder;
(3) ALU;
(4) Visit system;
(5) write back the data path and its control parts.
After analyzing these, we can imitate the factory assembly line, so that all the instructions are executed concurrently.
That is, at the same time, every functional part is busy (in general).
Under the MIPS Simulator (WINMIPS64), we can analyze the pipelining process in detail. Take the HelloWorld procedure as an example:
The above diagram is the process that the pipeline needs to go through five major processes;
The above figure corresponds to the whole process of the assembly line, the left is the HelloWorld program corresponding to the assembly code, the right is the implementation process of the pipeline. It can be seen that the completion of this program runs a total of 11 clock cycles, and if it is not the assembly line, that is, an instruction to carry out a command, then need to 7*5=35 a clock cycle, not difficult to prove, The execution time on the pipelined machine equals the non pipelined machine execution time divided by the pipeline series (the number of steps in the pipeline). Summary
The third part of C + + in Xiaoming, we talked about how to convert from a high-level language to machine language, while in this blog, we focus on machine directives, analyzing programs from the perspective of computer systems, including how numbers are represented in binary, and what happens when functions are called, Also talked about how to use pipelining technology to speed up the operation of the program. I hope you have a preliminary understanding of the operation of the computer after you have read the blog. If necessary, please contact me, my email address is: 2317809590@qq.com.