试图理解为什么页面大小是 2 的幂?

发布于 2024-08-03 18:23:47 字数 219 浏览 7 评论 0原文

我读到了这个:

回想一下,分页是通过 将地址分解为页面并 偏移数。最有效的是 将地址分成X页位并 Y 偏移位,而不是执行 对要计算的地址进行算术 页码和偏移量。因为 每一位位置代表一个功率 共 2 个,将地址拆分为 位导致页面大小为 2 的幂。

我不太明白这个答案,有人可以给出更简单的解释吗?

I read this:

Recall that paging is implemented by
breaking up an address into a page and
offset number. It is most efficient to
break the address into X page bits and
Y offset bits, rather than perform
arithmetic on the address to calculate
the page number and offset. Because
each bit position represents a power
of 2, splitting an address between
bits results in a page size that is a
power of 2.

I don't quite understand this answer, can anyone give a simpler explanation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

殤城〤 2024-08-10 18:23:47

如果要将(线性)地址转换为页面:偏移量,则需要将地址除以页面大小,并将整数答案作为页面,将提醒作为偏移量。

这是使用编程语言中的整数除法和模数(MOD、“%”)运算符来完成的。

计算机将地址表示为数字,并存储为二进制位。

这是一个示例地址:12 是二进制的 1100。

如果页面大小为 3,那么我们需要计算 12/3 和 12%3 来找到页面和偏移量(分别为 4:0)。

但是,如果页面大小为 4(2 的幂),则二进制中的 4 为 100,并且可以使用特殊的“快捷方式”来计算整数除法和模数:您可以去掉最后两个二进制数字进行除法,并且可以仅保留模数的最后两位二进制数字。所以:

12/4 == 12>>2 (移动以删除最后两位数字)

12%4 == 12&(4-1) (4- 1=3 是二进制 11,“&”(AND)运算符仅保留这些)

If you are converting a (linear) address to page:offset, you want to divide the address by the page size and take the integer answer as the page, and the reminder as the offset.

This is done using integer division and modulus (MOD, "%") operators in your programming language.

A computer represents an address as a number, stored as binary bits.

Here's an example address: 12 is 1100 in binary.

If the page size is 3, then we'd need to calculate 12/3 and 12%3 to find the page and offset (4:0 respectively).

However, if the page size is 4 (a power of 2), then 4 in binary is 100, and integer division and modulus can be computed using special 'shortcuts': you can strip the last two binary digits to divide, and you can keep only the last two binary digits for modulus. So:

12/4 == 12>>2 (shifting to remove the last two digits)

12%4 == 12&(4-1) (4-1=3 is binary 11, and the '&' (AND) operator only keeps those)

小…楫夜泊 2024-08-10 18:23:47

使用二的幂可以产生更高效的硬件,这就是硬件设计人员所做的。考虑一个具有32位地址和n位页号的cpu:

+----------------------+--------------------+
| page number (n bits) | byte offset (32-n) |
+----------------------+--------------------+

这个地址被发送到虚拟内存单元,虚拟内存单元直接将页号和字节偏移分开,根本不需要任何算术运算。即,它将 32 位值视为位数组,并且(或多或少)有一条线直接连接到每个位。这允许内存硬件并行提取页号和字节偏移量,而无需执行任何算术运算。

同时,这种方法需要在位边界上进行分割,这直接导致页面大小为 2 的幂。

Working with powers of two leads to more efficient hardware so that's what hardware designers do. Consider a cpu with a 32-bit address, and an n-bit page number:

+----------------------+--------------------+
| page number (n bits) | byte offset (32-n) |
+----------------------+--------------------+

This address is sent to the virtual memory unit, which directly separates the page number and byte offset without any arithmetic operations at all. Ie, it treats the 32-bit value as an array of bits, and has (more or less) a wire running directly to each bit. This allows the memory hardware to extract the page number and byte offset in parallel, without performing any arithmetic operations.

At the same time, this approach requires splitting on a bit boundary, which leads directly to power-of-2 page sizes.

伴我老 2024-08-10 18:23:47

如果您有 n 个二进制数字可供使用,那么您可以编码 2n 个不同的值。

给定一个地址,您的描述指出一些位将用于页面,一些位用于偏移量。由于您使用整数个二进制位作为偏移量 Y,因此页面大小自然是 2 的幂,特别是 2Y

If you have n binary digits at your disposal, then you can encode 2n different values.

Given an address, your description states some bits will be used for the page, and some for the offset. As you're using a whole number of binary bits for the for offset Y, then the page size is naturally a power of 2, specifically 2Y.

夜声 2024-08-10 18:23:47

因为数据的表示。页偏移量设置页的大小,并且由于数据以二进制表示,因此您将有 n 位来定义偏移量,因此您将拥有大小为 2^n 的页。

假设您有一个像 10011001 这样的地址,并且您将其拆分为 1001:1001 作为 page:offset。由于您有 4 位来定义偏移量,因此页面大小为 2⁴。

Because the representation of the data. The page offset sets the size of the page, and since the data is represented in binary, you will have a number n of bits to define the offset, so you will have pages with a size of 2^n.

Let's suppose you have an address like this 10011001, and you split it in 1001:1001 for page:offset. Since you have 4 bits to define the offset, your page's size is 2⁴.

三岁铭 2024-08-10 18:23:47

如果不是 2 的精确幂,则某些内存地址将无效。例如,如果页面大小是 5 字节,那么为了区分每个字节,我们需要在地址的偏移部分使用 3 位。因为使用2位只能寻址4个字节。但对 5 字节页使用 3 位偏移量会保留两个地址未使用。这就是为什么页面大小应该是......

If it is not exact power of 2 then some memory address will be invalid. eg if the page size is 5 byte then to distinguish each byte we need 3 bit in the offset part of the address. Because using 2 bit only 4 byte can be addressed. But using 3 bit offset for 5 byte page keep two address unused. thats why page size should be .......

分开我的手 2024-08-10 18:23:47

由于所有地址都是二进制的,并且分为f和d,f=帧号,d=偏移量。由于大小为 2 的幂,人类无需进行大量的数学计算,只需观察地址位即可识别 f 和 d。如果页面大小为 2^n,则物理地址的最后 n 位表示偏移量,其余位表示页号。
希望这个答案对您有帮助。

since all addresses are in binary and are divided into f and d, f=frame no, d=offset. due to be the size in power of 2 a human no need to go for huge mathematical calculation, just by watching the address bits you can identify the f and d. If page size was 2^n the in physical address last n bits represents the offset and remaining bits represent page numbers.
Hopefully this answer will help you.

ˉ厌 2024-08-10 18:23:47

我意识到我没有得到的是页面大小如何是 2 的幂。后来我发现:

在最小级别,页面是 1 位(0 或 1),偏移量是1 位(0 或 1)。将它们组合在一起,页面大小将为 2 x 2(或 2^2),因为它们都具有位数(各 2,因此 2 x 2)。

现在,如果页面/偏移量更大,那么它将是 nxn——n 是它们都拥有的位数。

I realized what I didn't get was how the page sizes are a power of 2. I found out afterwards:

At the smallest level, the page is 1 bit (0 or 1) and the offset is 1 bit (0 or 1). Combining these together, the page size would be 2 x 2 (or 2^2), because of the number of bits they both have (2 each, so 2 x 2).

Now if the page/offset were larger, then it would be n x n -- n being the number of bits they both have.

箹锭⒈辈孓 2024-08-10 18:23:47

页码始终为 2 次方 (2^n)。
有以下几个原因:
1.内存地址也是2^n,所以移动页面会更容易。
2.有两个位代表页面:
a.页码b.偏移编号
所以,这也是原因。
3.内存是量子化的形式,如电荷(q)。

The page no. is always in the power of 2 as (2^n).
There are several reasons:
1.The memory address is also in 2^n so it will be more easy to move a page.
2.There are two bits which represent the page:
a.Page no. b.Offset no
So,This is also the reason.
3.Memory is in quantised form like charge(q).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文