tags:

views:

82

answers:

4

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?
A: 

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.

ndim
+2  A: 

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.

anon
Quite true. However when I read the OP I see "how do I?" between the lines and note that most operating systems are protective of their device registers so generally prevent user code from touching them directly, preferring that the OS kernel read and write the registers on the user code's behalf.
msw
+4  A: 

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, and the same instruction can write it back to RAM. To use the devices on the computer, that's another story.

Devices use so called interrupts, which are events which are fired when the device wants your (the CPU's) attention. In assembler you register your function to the interrupt to handle it 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 the hard disk sectors X to Y. Then the harddisk spins up, fetches some of the data and fires an interrupt. Your code, which you registered for that interrupt, has to handle it and 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 inbetween. Only as soon as the device is done, it raises an interrupt and your assembler code can respond accordingly.

Virtlink
+1  A: 

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.

Lucas Jones