"In-depth understanding of computer systems" notes (i) stack "illustrations"

Source: Internet
Author: User
Tags constant diff garbage collection

Welcome to the in-depth understanding of computer Systems Series Blog

"In-depth understanding of computer systems" notes (a) stack (this article)

In-depth understanding of computer systems Notes (ii) Principles of memory and caching

"In-depth understanding of computer systems" Note (iii) linking knowledge

Deep understanding of computer systems Notes (iv) virtual memory, malloc, garbage collection

"In-depth computer system" notes (v) concurrent, multi-process, and multi-threaded "Final"

--------------------------------------------------------------------------------------------------------------- -----
Reading

The book is a textbook of the Carnegie Mellon University (CMU), which is logically rigorous. Although it is a textbook, still some obscure, not too image. The second chapter is mainly about integers, floating-point numbers, is very obscure, all is a mathematical formula. The author's Thinking Mathematics thinking, is always N, M, K, ∑ and so on, let us math sucks classmate how is good. It would be nice if we could add math knowledge to the minds of ordinary people.

The book does systematically introduce the computer and is perfect. It can give you several important levels of models and processes:

1. Function call stack model--Chapter III (function does not always create a stack frame, this article will explain this phenomenon)

Structure of 2.a.out or EXE executable file--seventh chapter click Open Link

3. Program loader and Links--eighth chapter click Open Link

4.malloc and virtual memory principle--Nineth Chapter Click Open Link

5. Threading, model in memory--12th Chapter

It was ecstatic for a growing programmer. With this knowledge, the "C expert programming" book is needed. This book is "c expert programming" of the full coverage ah, haha.

The translator is very attentive, but the reader is not necessarily appreciative. For example, you can directly translate popular memory, hard drives, and SSDs, and there is absolutely no need to use memory, disks, and solid-state storage disks. Also for example: there is no need to translate the shell into a "shell" more awkward ah. These translators should study like "Houtie".

The content of this book is large and scattered, feeling no end. The foreigner how to learn this kind of course, cost mind ah. From the computer structure, binary representation, to the assembly language function of the call, then the structure of the CPU, and then the connector memory, there are processes, concurrency, more network programming, the basic University has learned so many things four years.

One of the interesting things about this book is that it's a funny attribute of memory that no matter how much memory is in the system, he's always a scarce resource. Disk space and garbage bins also have this attribute.

Work more than 2 years of time, often in the online search system knowledge, compile, link and virtual memory malloc and so on. Only by reading this book can we learn computer knowledge systematically. first, computer roaming

---"Using direct memory (DMA) technology, data can reach memory directly from disk without CPU.

---"according to the principle of machinery, larger memory smaller memory running slow, a register can only store hundreds of bytes, and memory can store more than GB. Faster processor speeds are easier than faster memory runs.

---Cache is critical, and a simple HelloWorld reveals an important issue. The system spends a lot of time moving information from one place to another. The HelloWorld is initially placed on the hard disk and then loaded into memory, which in turn enters the CPU. The following figure illustrates a memory hierarchy:

Ii. representation and processing of information

It's about computer principles, binary, complement, and floating-point numbers. Because the university curriculum has already studied, did not read carefully.

---floating point number, normalized, non-normalized, and infinity.

In general, we do not send a decimal number 1/3, 7/10 and so on these can not be full of numbers, then if you use binary notation decimal decimals, more expressions do not come out. Binary does not even represent the decimal 0.1 and 0.2 three, the machine-level representation of the program (in fact, assembly language)

---"is about " assembly language", the head is big. Personally, assembly language does not need to spend time to understand, even the book of assembly language also has text parsing. IA32 and x86-64 two kinds of assembly language.

---Assembly code does not differentiate between signed and unsigned or even pointer types.

---"The following illustration shows the meaning of the assembly code suffix:


Most gcc-generated assembly directives have a character suffix that represents the size of the operand. For example, there are three variants of the data transfer instruction: MOVB (transfer byte), MOVW (transfer word), and movl (double word transfer). Note that the assembly code uses the suffix ' l ' to represent 4-byte integers and 8-byte double-precision floating-point numbers, which does not create ambiguity because the floating-point number uses a completely different set of instructions and registers.

Operand indicator, three classes of operands: 1) The immediate number (immediate) is the constant value, the immediate number of the writing method is $. For example: $0x1f. 2) Register, 3) memory. There are many ways to address this because of the existence of three operands.

---"The structure of a register in a 32-bit CPU is as follows:


The above figure is an integer register of IA32. All 8 registers can be used as 32-bit and 16-bit, such as%eax and%ax. And the first four registers can access both of their low-byte. such as:%ah and%al.

The following figure is a register structure diagram of a 64-bit CPU:

The red box is compatible with 32 CPU results.

---Register usage:%eax,%edx, and%ECX are caller- save Registers,%EBX,%esi, and%edi are the callee's saved registers. Then, a function f () may be called by someone else, or other functions can be called, so when the F () runtime needs to save%EBX,%esi, and%edi to the stack and restore them before returning. (p151)---64-bit%rax register to hold the return value of the function (p198)

In X86-64 assembly language,%rax is used to hold the return value of the function, and%rax can be reused before the result is returned.

The---Stack plays a critical role in handling function calls. Below the diagram of the stack, stack top down, because IA32 stack unexpectedly is to the low address extension growth, straight let me collapse. (p115)


The upper part of the picture shows the actual effect, moving the value of%eax to%edx, and the lower half of the picture is the stack move step. The top of the stack changes the final key. From 0x108-0x104-0x108

---stack frame structure, the IA32 program uses a program stack to support function calls. The machine uses stacks to transfer function parameters, return values, save registers for later recovery, and local storage. The portion of the stack allocated for a single process becomes the stack frame. The structure of the stack frame is illustrated below.


---"Call command. The effect of the call instruction is to put the return value address into the stack and jump to the beginning of the called procedure. The return address is the address of the instruction that immediately follows the call in the program. When the called function returns, execution continues from here. The RET instruction pops the address from the stack and jumps to that position. For example, the following code:

int accum = 0;
int sum (int x,int y);
int main ()
{
    return sum (1.3);
}
int sum (int x,int y)
{
    int t = x + y;
    Accum + = t;
    return t;
}
After disassembly, the code of the call section of the excerpt is shown in the following figure:

The effect of the first call command is to push the 0x80483e1 into the stack while setting the value of the%EIP (program counter) to the first instruction of sum 0x8048394. The last line of ret instruction pops up 0x80483e1 to%eip and jumps to this address. As shown in the figure:

The effect of RET instruction is to let 0x080483e1 pop up, adjust the stack pointer, and 0x080483e1 assigned to%EIP, the program continues to execute.

---"Function call instance

int Swap_add (int* xp,int* YP);
int caller ()
{
    int arg1 = 534;
    int arg2 = 1057;
    int sum = Swap_add (&ARG1, &arg2);
    int diff = arg1-arg2;

    retur sum * diff;
}
int Swap_add (int* xp,int* yp)
{
    int x = * XP;
    int y = * YP;
    *xp = y;
    *YP = x;
    return x + y;
}

(The blue arrow is "point", the red Arrow is "offset", the green Arrow is the explanation)

Arg1 and arg2 must be stored in the stack because we have to generate addresses for them. The variables int x and int y in the swap_add can be stored in registers.

Allocate 24 bytes on the stack, 8 for local variables, 8 for parameters, and 8 unused because GCC knows that all stack space should be an integer multiple of 16. This ensures that the data is placed in strict alignment.

After the call to Swap_add, the stack's information is restored to its original state.

--- many functions do not require a stack frame after compilation. If all local variables can be stored in registers, and this function does not call other functions (leaf process), then the only reason to need the stack is to save the return value . In particular, DUI ' Yu Therefore, although there are register variables in C, if the variables of this function are few, it is not indicated in time that the variable is a register, it is also loaded into the register. (p196)

There are several reasons why the---function requires stack frames:

There are too many local variables that cannot be placed in registers.

Some local variables are arrays or structures.

The function uses & to calculate the address of a local variable.

function must pass some parameters on the stack to another function

Before modifying a call to save the register, the function needs to save the other state.

---"Stack break detection and stack protection (p181)

In the C language, there is no reliable way to prevent an out-of-bounds write operation on an array. Array out of bounds, is the stack overflow after the discovery of this error and then thrown.

Echo is a function that holds a local variable of char buf[8].

Thought: Storing a special canary (Canary) value between any local buffer and the stack state in the stack frame, also being the Sentinel value (guard values) is generated randomly each time the program is run. So if this sentinel value changes the description stack overflows.

---stack randomization (p180)

Computer

For example, if you run the following code multiple times, the address of the local variable is constant.

    int main ()
{
    int local;
    printf ("Local at%p\n", &local);
    return 0;

A real-life example, but this example says that each time the heap opens up space may be consistent.

Once in the Symbian project, found a not necessarily a bug, and later found to be a wild pointer. But the question is why it is not necessary. is because the Symbian operating system each time on the heap open space, in a short period of time is an address. For example: If the PTR pointer is now a wild pointer. However, the memory it points to is then re-malloc, which is the same as PTR pointing to the new object. However, this coincidence is not always a repetition.

---expands the IA32 to 64-bit. (p183)

X86-64 is presented by AMD and named. Now General shorthand X64

The general purpose register group is expanded from 8 to 16. And the name becomes%RAX,%RBX. Where%rax is used to store the return value.

Many program states are stored in registers, not on stacks. Parameters for shaping and pointer types are passed through registers. Therefore, some processes do not need to build stacks at all.

If possible, conditional-action conditional delivery directives are implemented, resulting in better performance than traditional branch code.

Floating-point operations are implemented using register-oriented instruction sets rather than IA32 supported stack-based methods.

The x86-64 does not have a frame register. ---"The value of the function pointer is the address of the first instruction in the machine code representation of the function. (p173)

Second reading note, click to view

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.