开发者

How can assembly access things outside the CPU, like the HDD or RAM?

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, r开发者_Go百科ead from the HDD, etc.

  • How do you go beyond computations in the CPU with assembler?


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. The MOV 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 and OUT 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜