Assembly language--register (CPU working principle)

Source: Internet
Author: User

This article address: http://www.cnblogs.com/archimedes/p/assembly-register.html, reprint please indicate source address.

This article will mainly introduce the registers in the 8086 CPU,

A register is a unit that stores information, or a device or a container, such as memory is also a storage medium or a storage unit, in fact, the register from the understanding and memory is similar,

Only registers (the registers discussed here are registers in the CPU, not including the registers on the peripherals) are located inside the CPU, the registers are the scarce resources in the CPU, and for a assembler programmer, the main use of the CPU is the register, assembler Programmer can use instructions to read and write Registers in the CPU, which can realize the control of the CPU, of course, different CPU, register number and structure are not the same, such as 8086 CPU, the number of registers is 14, and 8086 all the registers in the CPU The structure of the 16-bit , that is, a register can be stored under the 2B is 2 bytes, and to the 80386 CPU, the number of registers is also more than 8086 .

The 8086 CPU has a total of 14 registers and is all 16 bits.

That is, a total of 14 ax,bx,cx,dx,sp,bp,si,di,ip,flag,cs,ds,ss,es .

These 14 registers are divided into general registers, control registers and segment registers in a certain way.

Universal Registers:

AX,BX,CX,DX is called a data register:

AX (ACCUMULATOR): Cumulative register, also known as accumulator;

BX (Base): Base address register;

CX (Count): Counter register;

DX (data): Information register;

SP and BP are also known as pointer registers:

SP (Stack Pointer): stack pointer register;

BP (base Pointer): base pointer register;

SI and DI are also known as the variable address registers:

SI (source Index): source variable address register;

DI (Destination Index): Destination variable address register;

Control Register:

IP (instruction Pointer): instruction pointer register;

Flag: Sign Register;

Segment Register:

CS (Code Segment): Snippet register;

DS (Data Segment): segment register;

SS (Stack Segment): stack segment register;

ES (Extra Segment): additional segment register;

General purpose Registers

As can be known from the above, in the 8086 CPU, the General Register has 8, respectively, is the Ax,bx,cx,dx,sp,bp,si,di,

As for why they are named as universal registers, it is because each of these registers has its own special purpose,

For example, CX is a count register, which is the register used to specify the number of cycles when using the loop instruction loop.

If each of them has only one dedicated function, then they can only be called private registers,

It is because these registers can also be used to transfer data and staging data, so they are called general-purpose registers.

Here are some of the general registers in order:

Data register (AX,BX,CX,DX):

Data registers have AX,BX,CX,DX four components, because the CPU before 8086 is 8-bit CPU, so in order to be compatible with the previous 8-bit program, in the 8086 CPU, each data register can be used as two separate registers, thus, each 16-bit register can be Used as 2 independent 8-bit registers.

AX registers can be divided into two independent 8-bit AH and AL registers;

BX register can be divided into two independent 8-bit BH and BL registers;

The CX register can be divided into two independent 8-bit CH and CL registers;

DX registers can be divided into two independent 8-bit DH and DL registers;

In addition to the above 4 data registers, no other registers can be divided into two independent 8-bit registers;

Note that the word "standalone" in the above flag indicates that AH and AL are used as 8-bit registers,

Can be seen as unrelated, that is to say, two completely unconnected registers X and Y,

AX Register:

Another name for Ax is called an accumulator, or simply an accumulator, which can be divided into 2 independent 8-bit registers, AH and AL, and the AX register is the highest-utilization register when writing the assembler.

Since AX is a data register, it is a matter of course that it can be used to store normal data.

BX Register:

The first can be clearly, BX as a data register, indicating that it can be staged general data, in addition to the function of temporary general data, BX as a universal register,BX is mainly used for its exclusive function-addressing (addressing the physical memory address) , The data stored in the BX register is generally used as an offset address.

CX Register:

CX Register as a data register, it also has the same characteristics as AX,BX, that is, the general data can be staged, CX also has its special purpose, CX C is translated into the function of counting, which is the counter, When using loop loop instructions in assembly instructions, you can specify the number of cycles that need to be cycled through CX , and the CPU will do two things each time the loop command is executed:

One is to make CX = cx–1, the even if CX counter automatically minus 1, and the other is to determine the value in CX, if the value in CX is 0, it jumps out of the loop, and continues the instruction under the loop,

Of course, if the value in CX is not 0, the instructions specified in the loop continue to be executed.

DX Register:

DX registers, as one of the data registers, also have the same characteristics as AX,BX,CX, that is, the general data can be staged

8086 how the CPU accesses the physical address

8086 logical structure of the relevant parts:

Address Adder uses: Physical address = Segment Address * 16 + offset Address method to calculate physical address

The working process of the 8086CPU can be described briefly as follows:

(1) Read the instruction from the memory unit pointed to by the CS:IP, and read the instruction into the instruction buffer

(2) ip=ip+ the length of the instruction read, thus pointing to the next instruction

(3) Execute the instruction, go to step (1), repeat the process

Experiment

View CPU and memory, program with machine instructions and assembly instructions

1, the use of preparatory knowledge-debug

Debug is a DOS utility, a program debugging tool for programmers to use to check bytes anywhere in memory and to modify bytes anywhere. It can be used to execute a program on a per-instruction basis to verify that the program is running correctly, to track execution, to compare values before and after an instruction executes, to compare and to move the range of data in memory, to read and write files and disk sectors.

Debug commands are more than 20, and some of the debug commands that will be used here are explained below:

-R: View, change the contents of the CPU register

-D: View in-memory content

-e: Overwrite in-memory content

-U: Translate in-memory machine instructions into assembly instructions

-T: Execute a machine instruction

-A: Write a machine instruction in memory in the format of the assembly instruction

In the Start menu, Run dialog box, enter "cmd" carriage return, you can use Alt+enter to turn the window into full screen

Enter the debug command

(1) Use the R command to view and change the contents of the CPU register

Note the value of CS and IP, cs=1388,ip=0100, which is the instruction that the CPU is currently reading and executing at memory 1388:0100

(2) Use the D command to see what's in memory

d Segment Address: Offset address Debug will list the contents of 128 memory units starting from the specified memory unit

You can also use the view range of the specified D command, at which point the "D-segment Address: Start offset address end offset Address" format

For example, to see what's in 1000:0--1000:9.

(3) Use the e command to overwrite content in memory

Use the e command to write the machine code to memory:

Use the U command to view the meaning of the machine code in memory:

Execute the machine code in memory with the T command:

Use the T command to continue execution:

Use the a command to write machine instructions in memory:

The above using the e command to write to the machine instructions, it is not very convenient, it is best to directly in the form of assembly instructions to write instructions, so the following a command appears:

Write instructions to a memory unit starting with 1000:0 with a command

Experimental tasks

(1) Using debug, the following program segment is written to the memory, executed one after the other, observing the changes in the contents of the relevant registers in the CPU after each instruction execution

Resources

"Assembly Language"--Wang Shuang

Assembly language--register (CPU working principle)

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.