Search code examples
assemblyiox86cpu-architecture

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, read from the HDD, etc.

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

Solution

  • 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.