On the CPU - (CPU register structure [])

1.1 CPU internal structural analysis of

CPU and memory is an electronic component composed of many transistors, often referred to as IC (Integrated Circuit, IC). From a functional point of view, the internal CPU registers consists of four parts, a controller, and a clock operator, the current signal from the mutual communication between the portions.

Registers: used to handle temporary target instruction, data, etc., which can be seen as a memory. Depending on the type of an internal CPU registers will be 20 to 100.

Controller: is responsible for the memory read instructions, the register data and the like, and controls the overall computer according to the execution result of the instruction.

Operator: is responsible for the operation of data read from memory register.

Clock: responsible for issuing CPU clock signal to start timing. However, some computer clock is located outside the CPU.

1.2 CPU register is a collection of

Is a program that registers the object as described in

Use high-level language programs will be converted into machine language after compilation, is then processed by the internal CPU registers. Different types of CPU, the number of its internal registers, the kind and the value range stored in the register is different. According to different functions, we can register roughly divided into eight categories.

Accumulation registers: data storing operation is performed and data operations.

Flag registers: CPU state after arithmetic processing is stored.

Program Counter: The memory address where the next instruction is stored.

Base register: starting address data stored in the memory.

Index register: the relative address is stored in the base register.

General-purpose registers: storing arbitrary data.

Instruction register: store instruction. CPU internal use, the programmer can not read and write to the register by the program.

Stack Register: starting address of the memory stack area.

Wherein the program counter, accumulator register, flag registers, instruction registers and stack registers have only one, generally a plurality of other registers.

1.3 determine the program flow of the program counter

Address 0100 is the start of the program running. After the Windows operating system and other programs to be copied from the hard disk into memory, the program counter (a CPU-register) is set to 0100, then the routine is executed. CPU for each instruction, the program counter will be incremented by one. For example, the CPU 0100 execute the instruction address, the program counter value becomes 0101 (when executed occupy a plurality of memory addresses, instruction values ​​corresponding increase in length). Then, the CPU of the controller will refer to the program counter value, the read command from the memory and executed. In other words, the program counter determines the flow of the program.

1.4 conditional branching and looping mechanism

Program execution process is divided into sequential, conditional branching and looping three. Sequential execution means executes instructions in the order of address of the content. Conditional branch refers to any instruction execution addresses depending on the conditions. Loop refers to the same address is repeatedly executed instruction.

The figure represents the absolute value of the output value of the memory state stored in the program memory of the display. Start of the program run is 0100 addresses. With the increase in value of the program counter, when the address reaches 0102, if the accumulated value of the register is positive, a jump instruction (Jump instruction) execution jumps to 0104 addresses. At this time, since the accumulated value of the register 123, is positive, so the instruction address 0103 is skipped, the program flow directly jumps to 0104 addresses. That is to say, "jump to address 0104," the indirect instruction is executed, "0104 is set to the program counter address" this operation.

条件分支和循环中使用的跳转指令,会参照当前执行的运算结果来判断是否跳转。无论当前累加寄存器的运算结果是负数,零还是正数,标志寄存器都会将其保存。CPU在进行运算时,标志寄存器的数值会根据运算结果自动设定。条件分支在跳转指令前会进行比较运算。至于是否执行跳转指令,则由CPU在参考标志寄存器的数值后进行判断。运算结果的正,零,负三种状态由标志寄存器的三个位表示。如图:

标志寄存器的第一个字节位,第二个字节位和第三个字节位的值为1时,表示运算结果分别为正数、零和负数。

CPU执行比较的机制很有意思,因此请大家务必牢记。例如,假设要比较累加寄存器中存储的XXX值和通用寄存器中存储的YYY值,执行比较的指令后,CPU的运算装置就会在内部(暗中)进行XXX-YYY的减法运算。而无论减法运算的结果是正数、零还是负数,都会保存在标志寄存器中。程序中的比较指令,就是在CPU内部做减法运算。

1.5函数的调用机制

哪怕是高级语言编写的程序,函数调用处理也是通过把程序计数器的值设定成函数的存储地址来实现的。

 上图是给变量a和b分别代入123和456后,将其赋值给参数(parameter)来调用MyFunc函数的C语言程序。图中地址是将C语言编译成机器语言后运行的地址。由于1行C语言程序在编译后通常会变成多行的机器语言,所以图中的地址是离散的。

此外,通过跳转指令把程序计数器的值设定成0260也可实现调用MyFunc函数。函数的调用原点(0132地址)和被调用函数(0260地址)之间的数据传递,可以通过内存或寄存器来实现。

机器语言的call指令和return指令能够解决这个问题。函数调用使用的是call指令,而不是跳转指令。在将函数的入口地址设定到程序计数器之前,call指令会把调用函数后要执行的指令地址存储在名为栈的主存内。函数处理完毕后,再通过函数的出口来执行return命令。return的功能是把保持在栈中的地址设定到程序计数器中。如图所示,MyFunc函数被调用之前,0514地址保持在栈中。MyFunc函数的处理完毕后,栈中的0514地址就会被读取出来,然后再被设定到程序计数器中。

1.6 通过地址和索引实现数组

如图所示,出现的基址寄存器和变址寄存器。通过这两个寄存器,我们可以对主内存上特定的内存区域进行划分,从而实现类似于数组的操作。

首先,用十六进制数将计算机内存上00000000~FFFFFFFF的地址分出来。那么,凡是该范围的内存区域,只要是有一个32位的寄存器,即可查看全部的内存地址。但如果想要像数组那样分割特定的内存区域以达到连续查看的目的,使用两个寄存器会更方便些。例如查看10000000地址~1000FFFF地址时,可以将10000000存入基址寄存器,并使变址寄存器的值在0000000~0000FFFF变化。CPU则会把基址寄存器+变址寄存器的值解释为实际查看的内存地址。变址寄存器的值就相当于高级编程语言程序中数组的索引功能。

 

Guess you like

Origin www.cnblogs.com/mylearning-log/p/10932053.html