Basic knowledge of

How does the CPU work

The CPU instruction set Of the CPU
X86 / x64 instruction set The controller
ARM instruction set The operator
MIPS instruction set Memory (Temporary storage, not memory, memory is outside the CPU)
RISC instruction set

Detailed architecture

How does this code work

console.log('hello world');
Copy the code
  • Js interpretation engine -> lexical analysis -> Syntax analysis -> Syntax abstraction tree -> Machine instructions -> machine language

The development of computer languages

  • Source: binary data
  • First-generation language: machine instruction 01010… (Facing the machine)
  • Second generation language: Assembly instruction (machine oriented)
  • Third generation language: High-level LANGUAGE C (Process Oriented)
  • Fourth generation language: object-oriented language Java c++ javascript

Understanding assembly language

  • Closest to the underlying machine language
  • Manipulate the hardware directly without any abstraction
  • It consists of instructions and data, without any statements
  • Instruction received hardware platform constraints, low portability
  • Some knowledge of the underlying language will help you understand how computers work and how they manage memory
section .data ; Db 'hello World/n',10 helloLen: equ $-hello; 'Hello World! 'String length section.text global _start; Tell the operating system that the program instruction starts here. _start: mov ax,4; 4 System call number, put the number 4 in the 16-bit register mov ebx,1; 1: Standard output file descriptor, MOV ECx,hello; Mov edx,helloLen; Store string length int 80h; Soft interrupt, into the kernel mov AX,1; System call number move ebx,0; Return the value 0 for no error. exit(0) int 80hCopy the code

In plain English, you put ‘Hello world’ and the length of the string describing it into memory

# c int main() {printf('hello world'); # return 0; exit(0) }Copy the code

I xx you XX, this should be for the hospital programming ~

Understand THE C language

  • System-level language
  • It was first used to write the Unix kernel
  • It is still evolving today
  • Quickly understand THE C language
Int add(int x, int y) {return x + y; include <stdio.h> } int main(int argc, char const *argv[]) { printf("hello world\n"); printf("%d\n", add(100, 200)); Return 0; }Copy the code

C language compilation process

C language and JS similarities and differences

  • C is a compiled language
  • JS is an interpreted language
  • C is converted into an executable program with the help of a compiler
  • JS runs with the help of an interpretation engine

C language and C++ language differences

  • C++ is a new programming language, not an extension of C
  • C is procedural and C++ is object oriented
  • Both C and C++ have standard libraries.
  • At present, C is mainly applied in network related and embedded aspects
  • At present C++ is mainly used in complex engines and application software

Understand memory and Pointers

  • Memory: memory bar (computer hardware)
  • Memory address: number of a memory block
  • Pointer: is a variable that holds the memory address
    • Note: pointer is not an address, but a variable that holds the memory address

  • Reference (none) : used as a variable between the pointer and the memory address to prevent frequent manipulation of the pointer (changing the point of the pointer), only features of high-level languages, C language does not reference
  • Pointer to a variable (or data)

  • Pointer to pointer
    • Level 2 (multilevel) pointer, for example (why to use an example, such as my friend 😊), go to the garage to pick up the car -> find which garage -> which floor -> that area -> row and row
  • Pointer to a function
    • The program is essentially performing operations through functions, that is, saving the address saved by the function
Int main() {int a = 10; // declare an integer variable int b = a + 10; Int *p_int = &a; // int *p_int = &a; // int c = p_int + 10; *p_int int c = *p_int + 10; *p_int c = *p_int + 10; int d = 5; Fn (d) // d = 5} // void fn(d) {d = 10; }Copy the code

How does C implement references

  • C language is no reference, how to achieve the effect of similar reference, need to use secondary Pointers
int main(){
  int i = 5;
  fn(i);
  // i = 5
}
void fn(int **a){
  **a = 10;
}
Copy the code

An array of

Int arr1 [] = [1, 2, 3]; / / arr1 is equivalent to a pointer to an int arr2 [] [] = [[1, 2, 3], [4 and 6]]. // arr2 is used as a secondary pointerCopy the code

Understand C++ language

  • Supports data abstraction and encapsulation
  • Support for object-oriented programming
  • Support for generic programming
  • C++_ almost _ is a superset of C, which shares much in common with the basic syntax of C, such as declarations of variables and functions, native data types, and so on.
  • 10 minutes to learn C++ quickly
int main(int argc, char** argv){
  sizeof('c') = =1; // The size of a character literal is one byte
  sizeof('c') = =sizeof(10); // In C, character literals have the same size as int
  return 0; / / exit
}
Copy the code

Dynamic memory allocation

  • C does not have automatic garbage collection
    • Int * memeory_A = malloc(200); Return memory manually after use: free(memeory_A)
  • Java (JVM) and Node(V8 VIRTUAL machine) are based on dynamic memory allocation and automatic garbage collection
memory Corresponds to the memory segment describe features
The stack area Data segment Is a determined hamster (1-2m) with different platform sizes beyond StackOverflow Automatic allocation and release
The heap area Data segment Used for dynamic memory allocation Manually allocate and release
Global or static zone Data segment Explicitly initialized global variables, static variables (global and local), and constant data (such as string constants) in a program Initialize only once
Program code area Code segment Code area instructions are executed sequentially according to the programming flow. For sequential instructions, they are executed only once (process). If repeated, the jump instruction is required. If you recurse, you need a stack to do it The instructions in the code area include the opcode and the object to be manipulated (or the object address medical).

Subsequent articles

Advanced knowledge of EventLoop, Webkit, V8 principle analysis

Join us at ❤️

Bytedance Xinfoli team

Nice Leader: Senior technical expert, well-known gold digger columnist, founder of Flutter Chinese community, founder of Flutter Chinese community open source project, well-known developer of Github community, author of dio, FLY, dsBridge and other well-known open source projects

We look forward to your joining us to change our lives with technology!!

Recruitment link: job.toutiao.com/s/JHjRX8B