Detailed general register-memory read and write

Before reading this article, you can take a look at the
data width of this article to
facilitate understanding of the subsequent content

  • In the data width, it has been said that there are many containers in the computer
  • Some containers are provided by cpu, and some containers are provided by memory.
  • In fact, there is no difference in essence, they are all used to store data
  • Then why put some containers in the cpu and some containers in the memory
  • Putting the data in the cpu is very fast, but the cpu is very expensive
  • Put it in the memory will be relatively slow, but the memory should be relatively cheaper
  • So it is better to use both.

Commonly used 32-bit containers provided in cpu

We call it 32-bit general register

  • The designated purpose of the 32-bit general-purpose register is as follows
    Insert picture description here
    . You must remember the things in this picture. It is best to map the number to the register. It will be useful for some relevant understanding of the hard-coded later *

  • You don’t need to remember the part with the red circle on the picture. It’s best to forget its purpose. This is how the cpu recommends you to use the container. In fact, as long as it is a container, we know how to use it, so you don’t need to remember it.
    eg: We can understand it in this way. If you buy a cup (container), is this cup only used for water?
    You can also use it for wine and other things

  • So we don't need to remember how the cpu recommends you to use this container, it would be too rigid.

  • 32-bit general-purpose register, since it is 32-bit, it can store 32 binary numbers,

  • So you can calculate its storage range 0~FFFFFFFF

  • Commonly used registers provided in cpu also include 16-bit registers and 8-bit registers

Insert picture description here
So what is the relationship between these registers?
Let’s take a
Insert picture description here
look at thepicturebelow and before we look at the following, we can take a look at the common assembly instructions and DTDebug in this article.

  • It's okay if you still don't understand

  • We use the following code to demonstrate, you will find the mystery. -`Insert the
    code here
    MOV EAX,0xAAAAAAAA
    MOV AX,0xBBBB
    MOV AH,0XCC
    MOV AL,0XDD

  • Let's demonstrate with DTDebug
    Insert picture description here

Now the program executes to the MOV EAX, 0XAAAAAAAA statement, but it has not been executed yet. Every time we execute it, we pay attention to the change of EAX in the black circle.
The result after executing the first one
Insert picture description here

Then execute the second item,
Insert picture description here
then execute the third item,
Insert picture description here
and then execute the fourth item.
Insert picture description here
Seeing here, I believe everyone has understood.
A 32-bit general-purpose register includes not only 16-bit registers but also 8-bit registers.
In order to let everyone understand, I use 32 bits here. An example of general-purpose register EAX.
From the effect shown in the above code, we can find that the low 0~15 of EAX are
AX. The high 8~15 of
AX are AH . The low 0~7 of AX are AL.

RAM

Why introduce memory since there are registers? ? ? ?
Although they are essentially the same, the registers are inside the cpu, and the registers provided by the cpu are relatively limited and very expensive.
The price of memory is relatively cheap, so it can be done very large

The amount of memory is extremely large, and it is impossible to give a name to each memory unit, so it is replaced by a number. We call a computer cpu 32-bit or 64-bit, which mainly refers to the width of the memory number, not the width of the register. There are many books saying that the reason why it is called a 32-bit computer is because the width of the register is 32 bits, which is not accurate, because there are many registers that are larger than 32 bits.
If your computer's cpu is 32 bits, then your memory number is (for convenience, I am here to write hexadecimal)
0x00000000
0x00000001
0x00000002
0x00000003 each number corresponds to a byte of memory
...
...
...
0xFFFFFFF

  • Why do people often say that the maximum memory that a 32-bit computer can recognize is 4G
  • Because a 32-bit computer can be numbered to the power of 2^32, that is to say, it can address 2 to the 32th power of memory, and a memory is as fast as one byte, so it can recognize up to 4G of content.
  • We finally call the number the address of the memory

Write/read data from the specified memory

mov dword ptr ds:[0x0012FF34],0x12345678 //Write the immediate number 0x12345678 into the memory with the memory address number 0x0012FF34.
mov eax,dword ptr ds:[0x0012FF34] //Read the value in the memory number 0x0012FF34 to the register eax.
dword: Indicates how much to read/write. The above two lines of instructions write 4 bytes and read 4 bytes. Byte is one byte and word is two bytes.
ptr: point represents a pointer behind (pointer means that it is not an ordinary value, but an address, that is to say, the memory number
ds: segment register, not to mention here, remember it as a segment register Just fine.
0x0012FF34: Memory number.
Note: Do not write the address number casually, because the memory is protected and not all memory can be read and written directly (special processing is required)

Addressing formula

Addressing formula 1: [immediate data]
Read the value of the memory:
MOV EAX,DWORD PTR DS:[0x13FFC4]
MOV EAX,DWORD PTR DS:[0X13FFC8]
Write data to the memory:
MOV DWORD PTR DS:[0x13FFC4] ,EAX
MOV DWORD PTR DS:[0x13FFC8],EBX
obtains the memory number:
LEA EAX, DWORD PTR DS:[0x13FFC4]
LEA EAX, DWORD PTR DS:[ESP+8]
Addressing formula 2: [reg] reg represents register can It is any one of the 8 general registers.
Read the value of the memory:
MOV ECX, 0x13FFD0
MOV EAX, DWORD PTR DS: [ECX]
Write data to the memory:
MOV EDX, 0x13FFD8
MOV DWORD PTR DS: [EDX], 0x87654321
Get the memory number:
LEA EAX,DWORD PTR DS:[EDX]
MOV EAX,DWORD PTR DS:[EDX]
Addressing formula 3: [reg+immediate number]
Read the value of the memory:
MOV ECX,0x13FFD0
MOV EAX, DWORD PTR DS :[ECX+4]
Write data to memory:
MOV EDX,0x13FFD8
MOV DWORD PTR DS:[EDX+0xC],0x87654321
Get the memory number:
LEA EAX,DWORD PTR DS:[EDX+4]
MOV EAX,DWORD PTR DS:[EDX+4]
Addressing formula four: [ reg+reg {1,2,4,8}] *
Read the value of the memory:
MOV EAX,13FFC4
MOV ECX,2
MOV EDX,DWORD PTR DS:[EAX+ECX 4]
Write data to the memory:
MOV EAX ,13FFC4
MOV ECX,2
MOV DWORD PTR DS:[EAX+ECX
4],87654321
Get the memory number:
LEA EAX,DWORD PTR DS:[EAX+ECX 4]
Addressing formula five: [reg+reg {1, 2, 4, 8} + immediate data]

Read the value of the memory:
MOV EAX, 13FFC4
MOV ECX, 2
MOV EDX, DWORD PTR DS: [EAX+ECX 4+4]
Write data to the memory:
MOV EAX, 13FFC4
MOV ECX ,2
MOV DWORD PTR DS:[EAX+ECX
4+4], 87654321
Get the memory number:
LEA EAX,DWORD PTR DS:[EAX+ECX*4+2]
You can use the DTDebug software to demonstrate the above 5 formulas and deepen them. understanding

Guess you like

Origin blog.csdn.net/qq_51196205/article/details/109312193