Cxuan’s own Github is very hardcore, please you big guy star: github.com/crisxuan/be…

Assembly code is a low-level representation of a computer. It is a low-level language that can be understood literally, including processing data, managing memory, reading and writing data on storage devices, and using network communication. The compiler generates machine code through a series of transformations that follow the programming language, the instruction set of the target machine, and the operating system.

Instruction set

An instruction set is the instructions that direct the work of a computer, because a program is a set of instructions in a certain order of execution. Since the execution control of the computer is operated by the CPU, an instruction set is a set of instructions in the CPU that are used to calculate and control the computer. Each CPU in the output of the specified set of instructions to work with the hardware circuit.

There are many categories of instruction sets, but they are generally divided into two kinds, one is the concise instruction set, the other is the complex instruction set. The details are as follows

Reduced instruction set

Reduced instruction set computer, RISC, is a kind of CPU design mode, you can think of the CPU as a pipeline factory, the number of instructions and addressing methods have been reduced. Make it easier to implement, execute instructions more in parallel, and make the compiler more efficient.

Common reduced instruction set processors include ARM, AVR, MIPS, PARISC, RISC -v, and SPARC.

So you can understand

What the book is about.

Based on MIPS architecture, it introduces the hardware implementation and software design of five components of von Neumann system one by one. More importantly, it introduces a lot of parallel computing content, which is ignored or less content in most textbooks. According to this idea, the content related to parallel will be analyzed. OpenMP, CUDA, and Hadoop/Spark are integrated into the new book, which is the trend of the future

And this book

What is this book about?

This book is about the RISC-V instruction set, because the instruction set is different from the three versions, three versions?? Well, there’s this one down here

This book is about the ARM instruction set.

Therefore, it is usually a good choice to read this book while reading CASPP.

A reduced instruction set generally has the following characteristics

  • Uniform instruction code
  • A general purpose register that generally distinguishes between integer and floating point numbers
  • Simple addressing modes, complex addressing modes are replaced by simple instruction sequences
  • Support for types with few side gates, for example RISC supports byte string types.

Complex instruction set

Computing, CISC, is a microprocessor Instruction Set architecture, also translated as Complex Instruction Set.

Complex instruction sets include System/360, VAX, x86, etc.

The complex instruction set can be said to be a change over the reduced instruction set.

The characteristic of complex instruction set is that the number of instructions is large and complex, each instruction word length is not equal, the computer must be interpreted, and pay the cost of performance.

Generally speaking, there are several methods to improve CPU performance

  • Increases the register size
  • Improve internal parallelism
  • Increase the cache size
  • Increases the speed of the core time pulse
  • Add additional features such as IO and timers
  • Add vector processor
  • Hardware multithreading technology

It is more abstract, we will organize it into an article to introduce it in detail.

The C compiler takes other operations and converts them to output in assembly language, which is a machine-level representation of code. As we have described before, the execution of a C program is divided into the following steps

The following discussion will be based on assembly code.

Everyday we come into contact with high-level language, are the result of the layers of packaging, so we are usually less than assembly language contact, more not to use assembly language for programming, it’s like you don’t know the existence of the operating system, but in fact you each operation, even you double click an icon, and operating systems.

A high-level language has a high level of abstraction, but after layers of abstraction, it is certainly not as efficient or as reliable as assembly language.

But the greater advantage of high-level languages is that they can be compiled and run on different machines. Assembly languages have different representations for different sets of instructions. And advanced language learning to make it easier to understand, lower the computer threshold, make the internal volume more serious (of course, this is a joke, please don’t take the offense seriously).

Without further ado, understanding the underlying level requires understanding assembly language. Otherwise a low-level implementation of synchronized can be a headache. And, floating every day is not good, sooner or later to fall.

Understanding assembly code also helps us to optimize program code, analyze code for implicit inefficiencies, and this optimization method, once successful, will increase the magnitude of the optimization, rather than changing if… Else, using a new feature.

Machine-level code

Computer systems use many different forms of abstraction, and implementation details can be hidden by a simple abstract model. For machine-level programs, two things are important.

First, defining the format and behavior of machine-level programs is called instruction Set Architecture, or ISA. ISA defines the process state, the format of the instructions, and the effect of each instruction on the state. Most instruction set architectures, including ISA, describe processes that behave as if they are executed sequentially, with one instruction finishing and another starting. The description of the processor hardware is more complex, and it can execute many instructions simultaneously and in parallel, but it employs security measures to ensure that the overall behavior is in the order specified by ISA.

Second, the machine-level description of a memory address is a virtual address, which provides a memory model to represent a large array of bytes.

Compiler plays an important role in the whole process of compilation, converting C language into the basic instructions executed by the processor. Assembly code is very close to machine code, except that it is more readable than binary machine code, so understanding assembly is the first step to understanding how machines work.

Some process states are visible to the machine but not to C programmers, including

  • Program Counter, which stores the address of the next instruction and is used in the x86-64 architecture%ripTo represent.

When the program is executed, the initial value of PC is the address of the first instruction of the program. When the program is executed in sequence, the CPU first takes out an instruction from memory according to the instruction address indicated by the program counter, and then analyzes and executes the instruction. At the same time, the value of PC is increased by 1 and points to the next instruction to be executed.

Take the following example.

This is a numeric operation to add, the program is started, after compiling and parsing by the operating system to copy the program in the hard disk to the memory, the example of the program is 123 and 456 to perform the operation, and the result is output to the display. Since it is difficult to describe in machine language, this is the result of translation. In fact, each instruction and data may be distributed at different addresses, but for the sake of illustration, the memory and data that make up an instruction are placed at one memory address.

  • The integerRegister fileContains 16 named locations for storing 64-bit values. These registers can store address and integer data. Some registers are used to track program state, while others hold temporary data, such as parameters and local variables for procedures, and values to be returned by functions. thisfileIt has nothing to do with disk files. It is just a high-speed storage unit inside the CPU. There are special registers and general registers for storing operands.
  • Condition code registerUsed to hold state information about the most recently executed arithmetic or logical instruction. These are used to implement conditional changes in controls or data flows, such as those required to implement if and while statements. We have all learned high-level languages. The conditional control process in high-level languages can be divided into three main types:Sequential execution, conditional branching, circular judgmentThree. Sequential execution is the execution of instructions according to the content order of the address. Conditional branching is an instruction that conditionally executes at any address. A loop is the repeated execution of instructions at the same address.
    • In the sequential case, the program counter is + 1 for each instruction executed.
    • Condition and loop branching will set the value of the program counter to any address, so that the program can return to the previous address to repeat the same instruction, or jump to any instruction.

Here’s an example of how a conditional branch works (loops are also similar)

The beginning process of the program is the same as the sequential flow. The CPU starts to execute the command from 0100, and the command is executed sequentially in 0100 and 0101, and the value of PC is in order of +1. When the instruction is executed to address 0102, the value of register 0106 is judged to be greater than 0. Jump to address 0104, output the value to the display, and then end the program. The 0103 instruction is skipped, which is the same as if() in our program. If the condition is not met, the instruction is skipped. So the execution process of the PC is not directly +1, but the address of the next instruction.

  • A set ofVector registerUsed to store one or more integer or floating point values, the vector register operates on one-dimensional data.

Machine instructions perform very simple operations, such as adding two numbers stored in a register, moving data from memory to a register, or conditionally branching to a new instruction address. The compiler must generate sequences of such instructions to implement program constructs, such as arithmetic expression evaluation, loops, or procedure calls and returns

Know the assembly

I believe you should know that the emergence of assembly language background, that is binary data, too complex is too large, in order to solve this problem, the assembly language, assembly language and machine instruction lies in the difference between a representation, assembly using operands, machine instructions using binary, I mentioned many times before the machine code is compiled, You can’t say I’m wrong, but it’s not accurate.

But assembly is suitable for binary code where there is a conversion relationship.

Assembly code needs to be compiled by the assembler to produce binary code, which is the object code, and then run together by the linker.

Assembly languages fall into the following three categories

  • Assembly instruction: It is a kind of machine codemnemonicsIt has the corresponding machine code
  • Directive: no corresponding machine code, executed by the compiler, not by the computer
  • Other symbols, such as +, -, *, /, etc., are recognized by the compiler and have no corresponding machine code

The core of assembly language is assembly instruction, and our discussion of assembly is based on assembly instruction expansion.

Hardware and concepts related to assembly

CPU

CPU is the brain of the computer, it is also the core of the entire computer, it is also the implementation of assembly language hardware, the INTERNAL CPU contains registers, and registers are used to store instructions and data, the essence of assembly language is the CPU internal operands to perform a series of calculations.

memory

Without memory, a computer would be like a human being without memory, doing endless repetitive work. The instruction and data required by the CPU are provided by the memory. The CPU instruction is provided by the memory and output to the memory after a series of calculations.

disk

Disk is also a storage device, the biggest difference between it and memory is that the permanent storage, the program needs to be loaded in memory to run, and the program provided to memory is stored by disk.

The bus

Generally speaking, memory is divided into multiple storage units, which are used to store instructions and data, just like a house. The storage unit is the number of the house. The interaction between CPU and memory is carried out through the address bus, which is logically divided into three kinds

  • Address line
  • cable
  • The line of control

Reading and writing between THE CPU and memory mainly goes through the following steps

Read Procedure

  • The CPU sends the location of the instruction to be read through the address line
  • The CPU sends the read instruction through the control line
  • The memory puts the data on the data line and returns it to the CPU

Write Procedure

  • The CPU sends the location of the instruction to be written through the address line
  • The CPU sends a write instruction through the control line
  • The CPU writes data to memory over a data line

Let’s take a look at these three types of buses

The address bus

From our discussion above, we know that the CPU uses the address bus to specify the location of storage, and that the CPU can address as many storage units as the number of different information that can be transmitted on the address bus.

In the figure above, information is exchanged between CPU and memory through 10 address buses, each of which can transmit 0 or 1 data, so in the figure above, the number of data transmitted between CPU and memory is 2 to the tenth power.

So, if the CPU has N address buses, it can be said that the width of the address bus is N. So the CPU can find 2 to the N memory units.

The data bus

Data transfer between THE CPU and memory or other components is accomplished by the data bus. The width of the data bus determines the speed of data transfer between the CPU and the outside world. Eight data buses can transmit one 8-bit binary data (that is, one byte) at a time. Sixteen data buses can transmit two bytes at a time, and 32 data buses can transmit four bytes at a time…

Control bus

The control between the CPU and other components is accomplished through the control bus. How many control buses there are means how many kinds of control the CPU provides over external devices. Therefore, the width of the control bus determines the CPU’s ability to control external components.

The process of reading memory once

Memory structure

The memory IC is a complete structure. It also has power supply, address signal, data signal, control signal and addressing IC pins to read and write data. The following is a virtual IC pin diagram

In the figure, VCC and GND represent the power supply, A0-A9 is the pin of the address signal, D0-D7 represents the control signal, RD and WR are good control signals, I used different colors to distinguish, after connecting the power supply to VCC and GND, In most cases, +5V means 1, 0V means 0.

We all know that memory is used to store data, so how much data can this memory IC store? D0-d7 represents the data signal, that is, 8 bits = 1 byte of data can be input or output at a time. A0-a9 is a total of 10 address signals, which means that you can specify 00000 00000-11111 11111 2 ^ 10 = 1024 addresses. Each address stores 1 byte of data, so we can deduce that the memory IC capacity is 1 KB.

If we are using 512 MB of memory, this is equivalent to 512000 (512 * 1000) memory ics. Of course, it is unlikely that a computer will have this many memory ics, however, in general, a memory IC will have more pins and thus more data to store.

Memory reading process

The following is a memory read process.

To describe the process in detail, suppose we want to write 1byte of data to the memory IC, the process looks like this:

  • Firstly, connect the +5V power supply for VCC and 0V power supply for GND, and useA0 - A9To specify where the data is stored, and then enter the value of the data intoD0 - D7Data signal, and putWR (write)Is set to 1. After these operations, data can be written to the memory IC
  • When reading data, you only need to specify the data storage place through a0-A9 address signal, and then set the value of RD to 1.
  • RD and WR in the figure are also called control signals. When WR and RD are both 0, write and read operations cannot be performed.

conclusion

In this article, we mainly discuss the instruction set, instruction set classification, and assembly related hardware, bus, respectively, what are the role, and then we read a memory process to connect the CPU and memory interaction process.

Original is not easy, if there is help, please readers four consecutive (point in, look at, share, message), thank you

Pay attention to the public number programmer CXUAN reply cXUAN get quality data.

I have written six PDFS of my own, very hardcore, which are linked below

I have written six PDFS of my own, very hardcore, which are linked below

I have written six PDFS of my own, very hardcore, which are linked below

Cxuan wrote four PDFS.

Cxuan added two PDFS.