Can I install an 8g memory stick into a 32-bit system? Why?
Regarding 32-bit and 64-bit, this concept has always been confusing.
When buying computers, we have seen 32-bit and 64-bit CPUs .
When downloading software, we have also seen 32-bit or 64-bit software .
Even when installing a virtual machine, we have seen 32-bit and 64-bit systems .
When writing code, our values can also be defined as int32 or int64 .
Of course we know very well that when installing software, we generally choose 64-bit software for 64-bit systems, and there will definitely be nothing wrong with it, but why is this? Since CPU, software, operating system, and numerical values are all 32-bit and 64-bit, they can be combined into various problems at will. For example, can a 32-bit system install 64-bit software? Can 32-bit systems calculate int64 values? What is the relationship between them? This article will try to explain it clearly.
From code to executable file
Let’s start with a familiar scenario. For example, when we write code, we write it in the code editor.
// test.c
#include <stdio.h>
int main()
{
int i,j;
i = 3;
j = 2;
return i + j;
}
But this code is for humans to read, and machines cannot understand it, so this code will be converted into assembly code by the compiler .
Assembly coding is the bald one we learned in college.
// gcc -S test.c
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl $0, -4(%rbp)
movl $3, -8(%rbp)
movl $2, -12(%rbp)
movl -8(%rbp), %eax
addl -12(%rbp), %eax
popq %rbp
retq
Don’t read the content above, it’s not necessary.
As for assembly, there are still various symbols such as movl and pushq. Although it is indeed not good-looking, in the end it is still for people to see . What the machine CPU needs, in the end, still needs binary encoding such as 0101 , so you still need to use an assembler. Convert assembly into binary machine code . We can see that the following content is divided into 3 columns. The left is the instruction address, the right is the assembly code content, and the middle is the instruction machine code, which is hexadecimal and can be converted into a binary 01 string. This is what the machine CPU can recognize. .
// objdump -d test
0000000000001125 <main>:
1125: 55 push %rbp
1126: 48 89 e5 mov %rsp,%rbp
1129: c7 45 fc 03 00 00 00 movl $0x3,-0x4(%rbp)
1130: c7 45 f8 02 00 00 00 movl $0x2,-0x8(%rbp)
1137: 8b 55 fc mov -0x4(%rbp),%edx
113a: 8b 45 f8 mov -0x8(%rbp),%eax
113d: 01 d0 add %edx,%eax
113f: 5d pop %rbp
1140: c3 retq
1141: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
1148: 00 00 00
114b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
The machine code will eventually be placed in the executable file we compile and generate .
In other words, the code we usually write will eventually become a bunch of 01 machine codes, placed in executable files and lying on the disk.
From executable file to process
Once we execute the following command
./可执行文件名
This executable file will be loaded into memory , become a process , and run.
The machine code in the executable file will also be loaded into the memory. It is like a list filled with todo lists, and the CPU will execute the above machine code line by line according to this list. From the effect point of view, the process is moving.
For the CPU, when it reaches a specific encoding value, it will perform a specific operation. For example, to calculate 2+3, you actually read data 2 and 3 from memory through the bus , and then put them in the register . Then use an adder to add the two values and put the result into the register. Finally, this value Write it back to the memory, and repeat this cycle, executing the machine code line by line until exiting.
The meaning of CPU bits
In the above process, the most important keywords are CPU registers, bus, and memory .
A CPU register is, to put it bluntly, a small box that stores values. The size of the box is called the bit width . A 32-bit CPU can put a maximum value of 2^32. 64-bit is the maximum value of 2^64. The 32-bit wide CPU here is what we often call 32-bit CPU, and the same is true for 64-bit CPU.
The bus is used for signal transmission between the CPU and the memory . The bus can be divided into data bus, control bus and address bus . The functions are as their names suggest, let’s give an example to illustrate their functions. During the running of a process, the CPU will perform operations line by line based on the machine code of the process.
For example, if there is a line that adds the data at address A and the data at address B, then the CPU will send a signal to the memory device through the control bus and tell it that the CPU now needs to find the address of data A in the memory through the address bus. , and then obtain the value of A data, assuming it is 100, then this 100 will be transmitted back to a certain register of the CPU through the data bus . The same goes for B. Assume B=200 and put it in another register. At this time, after A and B are added, the result is 300. Then the control CPU finds the returned parameter address through the address bus , and then transmits the data result back through the data bus. in memory. For this deposit and retrieval, the CPU issues instructions to the memory through the control bus .
The bus can also be understood as having a width . For example, if the width is 32 bits, then 32 0 or 1 signals can be transmitted at one time, and the value range that this width can express is 0 to 2^32.
The bus width of a 32-bit CPU is generally 32 bits. As mentioned above, the CPU can use the address bus to perform addressing operations in the memory . So now the maximum addressable range of this address bus is 2^32. , in fact, it is 4G.
For a 64-bit CPU, the bus width is supposed to be 64 bits, but in fact it is 48 bits (sometimes it is said to be 40 bits or 46 bits, it doesn’t matter, you just know it is big), so the addressing range can reach 2^ The 48th power is 256T.
The meaning of system and software bits
上面提到了32位CPU和64位CPU的内存寻址范围,那么相应的操作系统,和软件(其实操作系统也能说是软件),也应该按CPU所能支持的范围去构建自己的寻址范围。
比方说下面这个图,在操作系统上运行一个用户态进程,会分为用户态和内核态,并设定一定的内存布局。操作系统和软件都需要以这个内存布局为基础运行程序。比如32位,内核态分配了1个G,用户态分配了3G,这种时候,你总不能将程序的运行内存边界设定在大于10G的地方。所以,系统和软件的位数,可以理解为,这个系统或软件内存寻址的范围位数。
一般情况下,由于现在我们的CPU架构在设计上都是 完全向前兼容 的,别说32位了,16位的都还兼容着,因此64位的CPU是能装上32位操作系统的。
同理,64位的操作系统是兼容32位的软件的,所以32位软件能装在64位系统上。
但反过来,因为32位操作系统只支持4g的内存,而64位的软件在编译的时候就设定自己的内存边界不止4个G,并且64位的CPU指令集内容比32位的要多,所以32位操作系统是肯定不能运行64位软件的。
同理,32位CPU也不能装64位的操作系统的。
程序数值int32和int64的含义
这个我们平时写代码接触的最多,比较好理解了。int32也就是用4个字节,32位的内存去存储数据,int64也就是用8个字节,64位去存数据。这个数值就是刚刚CPU运行流程中放在 内存里 的数据。
那么问题又来了。
32位的CPU能进行int64位的数值计算吗?
先说结论, 能 。但比起64位的CPU, 性能会慢一些 。
如果说我用的是64位的 CPU ,那么我在计算两个int64的 数值 相加时,我就能将数据通过64位的 总线 ,一次性存入到64位的 寄存器 ,并在进行计算后返回到内存中。整个过程一步到位,一气呵成。
但如果我现在用的是32位的CPU,那就憋屈一点了,我虽然在代码里放了个int64的数值,但实际上CPU的寄存器根本放不下这么大的数据,因此最简单的方法是,将int64的数值,拆成前后两半,现在两个int64相加,就变成了4个int32的数值相加,并且后半部分加好了之后,拿到进位,才能去计算前面的部分,这里光是执行的指令数就比64位的CPU要多。所以理论上,会更慢些。
系统位数会限制内存吗?
上面提到了CPU位数,系统位数,软件位数,以及数值位数之间的区别与联系。
现在,我们回到标题里提到的问题。
32位CPU和系统插8g内存条,能用吗?
系统能正常工作,但 一般用不到8G ,因为32位系统的总线寻址能力为2的32次方,也就是4G, 哪怕装了8G的内存,真正能被用到的其实只有4g,多少有点浪费。
注意上面提到的是 一般 ,为什么这么说,因为这里有例外,32位系统里,有些是可以支持超过4G内存的,比如 Windows Server 2003 就能最大支持64G的内存,它通过使用 PAE (Intel P hysical A ddress E xtension)技术向程序提供更多的物理内存,PAE本质上是通过 分页管理 的方式将32位的总线寻址能力增加到36位。因此 理论上 寻址能力达到2的36次方,也就是64G。
至于实现细节大家也不用关心,现在用到这玩意的机器也该淘汰的差不多了,而且都是windows server,注意 Windows Server 2003 名字里带个 server ,是用来做服务器的,我们一般也用不到,知道这件事,除了能帮助我们更好的装x外,就没什么作用了。
所以, 你当32位系统最大只能用到4G内存,那也没毛病。
64位CPU装32位操作系统,再插上8g的内存条,寻址能力还是4G吗
上面提到32位CPU就算插上8G内存条,寻址能力也还是4G,那如果说我现在换用64位的CPU,但装了个32位的操作系统,这时候插入8G内存条,寻址能力能超过4G吗?
寻址能力,除了受到cpu的限制外,还受到操作系统的限制,如果操作系统就是按着32位的指令和寻址范围(4G)来编译的话,那么它就会缺少64位系统该有的指令,它在运行软件的时候就不能做到超过这个限制,因此 寻址能力还会是4G。
最后留下一个问题吧。
上面提到,我们平时写的代码(也就是C,go,java这些),先转成汇编,再转成机器码。最后CPU执行的是机器码,那么问题来了。
为什么我们平时写的代码不直接转成机器码,而要先转成汇编,这是不是多此一举?
总结
-
CPU位数主要指的是寄存器的位宽,
-
32位CPU只能装32位的系统和软件,且能计算int64,int32的数值。内存寻址范围是4G。
-
64位CPU,同时兼容32位和64位的系统和软件,并且进行int64数值计算的时候,性能比32位CPU更好,内存寻址范围可以达到256T。
-
32位CPU和操作系统,插入8G的内存,会有点浪费,因为总线寻址范围比较有限,它只能用上4G不到的内存。
-
If a 64-bit CPU is equipped with a 32-bit operating system, even if 8G of memory is plugged in, the effect will still be less than 4G of memory.
at last
When I first started working, I always thought that int32, which is 2.1 billion, must be enough for such a large value, but the reality hit me in the face several times.
When I was making games in the past, the blood volume was initially defined as int32. The game settings were that you could charge money to improve the character's attributes and increase the upper limit of the blood volume. No one expected that the bosses would use krypton gold to force the blood volume to increase. The amount reached the maximum value of int32. So the planner made a one-sentence request: "The blood volume must be supported to the size of int64." This was the simplest planning project I have ever seen, but it also made people work the most overtime.
That was the first time I felt the power of money.
Spring recruitment has begun. If you are not fully prepared, it will be difficult to find a good job in spring recruitment.
I’m sending you a big employment gift package, so you can raid the spring recruitment and find a good job!