- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习Windows下的X64汇编语言和MASM64,就是看最新版的《64位汇编语言的艺术》一书。
我有一个关于书中引用的问题:
You do have to worry about MMU page organization in memory in one situation. Sometimes it is convenient to access (read) data beyond the end of a data structure in memory. However, if that data structure is aligned with the end of an MMU page, accessing the next page in memory could be problematic. Some pages in memory are inaccessible; the MMU does not allow reading, writing, or execution to occur on that page.Attempting to do so will generate an x86-64 general protection (segmentation) fault and abort the normal execution of your program. If you have a data access that crosses a page boundary, and the next page in memory is inaccessible, this will crash your program. For example, consider a word access to a byte object at the very end of an MMU page, asshown in Figure 3-2.
As a general rule, you should never read data beyond the end of a data structure. If for some reason you need to do so, you should ensure that it is legal to access the next page in memory (alas, there is no instruction on modern x86-64 CPUs to allow this; the only way to be sure that access is legal is to make sure there is valid data after the data structure you are accessing).
所以我的问题是:假设我有那个确切的案例。数据段末尾的字变量。如何防止异常?通过手动填充 00h 单元格?正确地将每个变量与其大小对齐?如果我对齐所有内容,如果最后一个变量是跨越 4k 边界的 qword 会发生什么?如何预防?
MASM 会自动分配另一个顺序数据段来容纳它吗?
最佳答案
读取页面中已知包含任何有效字节的任何位置是安全的,例如在具有未对齐的 foo: dq 1
的静态存储中。如果你有它,mov rax, [foo]
总是安全的。
您的汇编器 + 链接器将确保 .data
、.rdata
和 .bss
中的所有存储实际上都由有效页面支持操作系统会让你触摸。
你的书的意思是你可能有一个 3 字节结构的数组,例如 RGB 像素。 x86 没有 3 字节加载,因此使用 mov eax, [rcx]
加载整个像素结构实际上会加载 4 个字节,包括您不关心的 1 个字节。
通常这很好,除非 [rcx+3]
位于未映射的页面中。 (例如,缓冲区的最后一个像素,在页面末尾结束,下一页未映射)。跨入另一个不需要数据的缓存行对性能来说不是很好,因此这是与 2 或 3 个单独加载(如 movzx eax, word ptr [rcx]
/movzx)的权衡edx, byte ptr [rcx+2]
这在 SIMD 中更常见,您可以在加载它们后在寄存器中一次更多地使用多个元素。像 movdqu xmm0, [rcx]
加载 16 个字节,包括 5 个完整像素和我们不打算在此向量中处理的另一个像素的 1 个字节。
(平面 RGB 没有这个问题,其中所有 R 分量都是连续的。或者一般来说,AoS 与 SoA = 数组结构对 SIMD 有好处。如果展开,你也不会有这个问题你的循环是 3 或什么,所以 3x 16 字节向量 = 48 字节覆盖 16x 3 字节像素,如果需要,可能会做一些改组或者有 3 个不同的向量常量,如果你需要不同的常量来排列你的结构的不同组件或像素或其他。)
如果遍历一个数组,你在最后一次迭代中会遇到同样的问题。如果数组大于 1 个 SIMD 向量(XMM 或 YMM),而不是最后一个 n % 4
元素的标量,您有时可以安排在数组末尾结束的 SIMD 加载,因此它与先前的完整向量部分重叠。 (为了减少分支,保留 1..4 个清理元素而不是 0..3,所以如果 n
是向量宽度的倍数,那么“清理”是另一个完整向量。)这有效非常适合制作 ASCII 字符串的小写副本:重做任何给定字节的工作很好,而且你没有就地存储,所以你甚至没有存储转发停顿,因为你不会'负载与以前的商店重叠。对数组求和(您需要避免重复计算)或就地工作不太容易。
另见 Is it safe to read past the end of a buffer within the same page on x86 and x64?
这对 strlen
来说是一个挑战,您不知道您被允许读取的数据是否延伸到下一页。 (除非您一次只读取 1 个字节,这比使用 SSE2 慢 16 倍。)
AVX-512 通过故障抑制屏蔽了加载/存储,因此 k1=0x7F 的 vmovdqu8 xmm0{k1}{z}, [rcx]
将有效地加载 15 字节,而不是即使第 16 个字节(掩码为零)扩展到未映射的页面,也会出现错误。与 AVX vmaskmovps
等相同。但是 AMD 上的商店版本很慢。
Attempting to do so will generate an x86-64 general protection (segmentation) fault
实际上是一个 #PF
页面错误,用于访问未映射或权限被拒绝的页面。但是,是的,同样的区别。
关于assembly - 访问跨 MMU 页边界的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72479456/
在我的应用程序中播放背景音乐时遇到问题。 首先,我在第一个 Storyboard View Controller 中的 ViewDidLoad 方法中开始播放音乐。即使我从一个页面跳转到另一个页面,它
我想跨行连接数组,然后进行不同的计数。理想情况下,这会起作用: WITH test AS ( SELECT DATE('2018-01-01') as date, 2 as value,
这是一个场景: Repo A 是一个包含大量模块和依赖项的怪异代码。安装起来并不容易。它由其他人维护并托管在 Github 上。 Repo A 包含一个非常有用的模块 X,并且几乎不依赖于 Repo
目前,我在一台服务器上运行了一个应用程序。有一个 crontab 设置,因此根据指定的规则,在某些时间运行任务。 现在,我正在考虑将我的应用程序迁移到 docker 容器中,以便我能够独立运行我的应用
我有一个全局表,我想在两个不同的 Lua 状态之间保持同步。根据我所阅读和理解的内容,唯一的方法似乎是,在我的 C 后端,在状态之间进行表的深层复制(如果表已被修改)。有没有更好的办法 ? 另外,我看
我们目前有一个 asmx webservice,它公开了一个方法来对 Sql 数据库进行各种更新,内部包装在 SqlTransaction 中。 我正在 WCF 中重写此服务,我们希望将现有方法拆分为
我是 Qt 的新手,所以请原谅这个问题的简单性,但我对 Qt 线程有点困惑。假设我有 3 个线程:主要的默认 GUI 线程和我自己创建的 2 个线程(称为 WorkerThread)。我的每个 Wor
我们的产品有一个 Restful API 和一个服务器渲染的应用程序(CMS)。两者共享数据库。两者都是用django编写的 两者所需的字段和模型并不是相互排斥的,有些仅针对 API,有些针对 CMS
我正在实现一个基于角色的访问控制系统,它具有以下数据库表。 groups --------- id (PK) name level resources --------- id (PK) name r
我有三个应用程序,为了便于管理,我希望将它们分开。他们按照建议作为 Plack 服务器运行 here , 代理在 nginx 后面。 我想有一个单独的应用程序来管理登录,并在所有其他应用程序之间共享该
我的主窗口上有一个 UIWebView。我可以通过我的第二个 View Controller 来控制它吗?如果可以的话你能给我举个例子吗? 最佳答案 是的,你可以。 “如何”是一个基本的 Cocoa/
我想制作一个小型应用程序,从连接到串行端口的设备收集数据,并将其通过 LAN 传递到另一个应用程序,后者将其存储在数据库中。 我已经在一台 PC 上的一个应用程序中完成了此操作,因此实际上会将应用程序
从主 AppDomain,我试图调用在不同 AppDomain 中实例化的类型中定义的异步方法。 比如下面的类型MyClass继承自 MarshalByRefObject并在新的 AppDomain
因为 LiveServerTestCase继承自 TransactionTestCase ,默认行为是在每个测试方法结束时删除测试数据。我想用LiveServerTestCase类,但保留方法之间的测
我正在开发我的第一个 WPF/MVVM 应用程序,但我在命令知识方面遇到了限制! 这是我的场景。 我有一个窗口——Customer.xaml。 它包含 2 个用户控件 查看CustomerSearch
这是我的 WPF 应用程序模型的简化版本: Employee +Name:string Client +Name:string +PhoneNumber:string Appointmen
我有一个 mercurial 存储库,它使用子存储库功能(如 .hgsub 文件中定义的)引入依赖项,但我正在努力让它在 TeamCity 中工作。 我启用了 mercurial_keyring 扩展
我正在尝试使用新的 Azure 虚拟网络公共(public)预览版的对等互连功能来加入我在两个不同订阅(即不同租户)上拥有的两个网络。这可能吗?我没有看到任何其他说法,但是当我尝试在 PowerShe
我有 2 个存储库。由于主干代码位于一个 protected 存储库中,因此我进行了 checkout ,然后 checkin 到另一个存储库(因为用户没有第一个 protected 存储库的权限)。
我有一个项目,其调用结构与此类似: 主要项目/应用 我的图书馆代码 别人的库代码 我的图书馆代码 一切都是用 C# 编写的,我可以访问“其他人的库代码”。他们的代码不包含在我的项目中,因为它是开源的而
我是一名优秀的程序员,十分优秀!