程序集如何访问 CPU 外部的事物,例如 HDD 或 RAM?
所以我看了一下x86汇编语言;所有命令都非常清楚,但是:我没有看到任何可以实际触发计算机中某些内容的内容,例如:访问 RAM 而不仅仅是 CPU 寄存器、从 HDD 读取等。
- 如何使用汇编器超越 CPU 中的计算?
So I took a look at the x86 assembly language; All the commands are pretty clear but: I don't see anything that can actually trigger something in the computer like: Access RAM and not only CPU registers, read from the HDD, etc.
- How do you go beyond computations in the CPU with assembler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 x86 汇编中,
MOV
指令用于从 RAM 获取数据并将其放入 CPU 的寄存器之一中,您可以在其中对其进行操作。MOV
指令还可以将数据写回 RAM。要使用计算机上的设备,那就是另一回事了。设备使用所谓的中断,这些事件是当设备需要您(CPU)关注时触发的事件。在代码中,您注册函数以在中断触发时处理中断。要从设备获取数据或从设备获取数据,您可以使用
IN
和OUT
指令,它们通过数据总线移动数据。这样,您就可以向设备提供指令,例如:获取硬盘 X 到 Y 扇区的数据。然后硬盘旋转,获取一些数据并触发中断。您为该中断注册的代码必须处理它、获取数据并将其写入某个适当的 RAM 位置。大多数 CPU 和设备还支持 DMA (直接内存访问),您只需在其中指定一个位置设备必须在 RAM 中写入数据,然后在不中断 CPU 的情况下执行此操作。只有当设备完成后,它才会引发中断,您的汇编代码才能做出相应的响应。In x86 assembly, the
MOV
instruction is used to get data from RAM and put it in one of the CPU's registers, where you can manipulate it. TheMOV
instruction can also write data back to RAM. To use the devices on the computer, that's another story.Devices use so called interrupts, which are events that are fired when the device wants your (the CPU's) attention. In you code you register your function to handle the interrupt when it fires. To get data to and from the device, you can use the
IN
andOUT
instructions, which move data over the data bus. This way, you can provide the device with instructions, for example: get the data from hard disk sectors X to Y. Then the hard disk spins up, fetches some of the data and fires an interrupt. Your code, which you registered for that interrupt, has to handle it, get the data and write it to some appropriate RAM location. Most CPU's and devices also support DMA (Direct Memory Access), in which you only specify a location in RAM where the device has to write it's data, which it then does without interrupting the CPU in between. Only as soon as the device is done, it raises an interrupt and your assembler code can respond accordingly.通常,您可以读/写内存映射设备,或者使用特殊的 I/O 指令读/写 I/O 端口。内存映射设备是 CPU 和实际硬件之间共享的内存 - 访问它使硬件执行某些操作。
Typically, you read/write to a memory mapped device, or use special I/O instructions to read/write to I/O ports. A memory mapped device is memory that is shared between the CPU and actual hardware - accessing it makes the hardware do something.
可以使用
MOV
等指令来访问RAM。访问磁盘等的 API 取决于您使用的操作系统 - 在 Linux 上,请查看 系统调用接口。在 Windows 上,请查看 本教程 - 只需跳过介绍部分东西。另一件要做的事情是从汇编代码中调用 C 库。
您可能感兴趣的项目是用 16 位 ASM 编写的教育操作系统。它可以访问文件系统,还有更多 - MikeOS,尽管请注意,大多数内容不适用于由于各种原因,程序在现代操作系统下运行。
You can use
MOV
, etc. instructions to access RAM. The APIs that access the disk, etc. are dependent on the OS you are using - on Linux, look into the System call interface. On Windows, have a look at this tutorial - just skip the introductory stuff.Another thing to do is call C libraries from your assembly code.
A project which might be of interest to you is an educational OS written in 16-bit ASM. It does filesystem access, and quite a bit more - MikeOS, although be aware that most things won't apply to programs running under a modern OS for a variety of reasons.
您编写访问 RAM(
mov
和其他具有内存操作数)的代码以进行内存映射 IO 或 x86 的特殊 IO 空间(in
、out
)。艺术在于知道何时向哪个地址写入/读取什么内容。
You write code which accesses RAM (
mov
and others with memory operand) for memory-mapped IO or the x86's special IO space (in
,out
).The art is knowing what to write to/read from what address and when.