2

For simplifying discussion, I assume there is only one executing thread. The following are just my wild speculations:

1, If the CPU reads a byte from an address of memory, then it can repeatedly read the same value from the same address.

2, If the CPU reads a byte from a port repeatedly, then it may read a different value each time.

I think the difference between the two ways is that the port controller can automatically update the value on the port after each read operation.

However, I could not find any text book explicitly supporting my statements.

Am I correct?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • CPU can read a different value from an address of memory too, because some memory addresses can be mapped to something else than RAM, depending how the hardware has been designed (just like I/O lines, that can be mapped to almost anything). – tigrou Dec 22 '12 at 20:19
  • Thank you, tigrou. The case you mentioned has been excluded in this discussion. – xmllmx Dec 22 '12 at 20:20
  • 1
    Also : at hardware level there is no conceptual difference between ports and memory : both are accessed by sending electric signals through the address and control bus. When you read or write ports using IN/OUT instructions, the difference is that CPU set a pin to high (or logical state "1") that means "hey RAM this is not for you but for I/O device". to answer your questions : 1) there is no guarantee on this, depending how hardware has been designed. if address is mapped to RAM: yes (fortunatly), otherwise it depends. 2) yes. – tigrou Dec 22 '12 at 21:09
  • Thank you very much, tigrou. Your explanation is helpful. – xmllmx Dec 23 '12 at 06:56

1 Answers1

5

IO instructions are supposed to interact with hardware. Therefore IO instructions in general do not (and are not supposed to) conform to memory semantics. Instead, the semantics depends on the hardware (and the corresponding protocol) they communicate with. Don't think of the IO space as memory.

Memory instructions can access memory as well as memory mapped IO. When they access memory mapped IO they behave like IO instructions. Only when they do access real memory, memory semantics apply. This means for example that a read to a memory location always returns the last value that was written to this location.

It is important for the CPU to distinguish between address spaces used for memory mapped IO and for real memory. For example data from memory mapped IO must not be stored in the cache and memory instructions must not speculatively access memory mapped IO locations. Because of this accessing a memory mapped IO space is significantly slower than accessing real memory.

To distinguish between memory mapped IO and real memory the processor usually uses the page table, but there are other mechanisms like Memory type range registers.

Last not least there may be other hardware able to write to memory. Examples are systems with multiple cores and/or direct memory access. These systems need to be aware of each other and use a cache coherency protocol to maintain the correct memory semantics. Of curse this also requires to distinguish between IO mapped memory and real memory.

The conclusion is that real memory also behaves like memory and conforms to memory semantics. IO and memory mapped IO can behave any way it wants.

Mackie Messer
  • 7,118
  • 3
  • 35
  • 40
  • Thank you very very much, Mackie. You are a real professional and an excellent mentor. I learned from your post much than many text books of assembly language. Many thanks again. – xmllmx Jan 08 '13 at 23:17
  • Great answer, but I still have a doubt...even when using DMA, do I still have a separated ammount of memory to map io devices? I mean, with 32 bits CPU part of this address space will be used to map memory IO even when I have DMA in my computer? – fredcrs Mar 31 '13 at 03:10
  • 2
    @fredcrs Yes and yes, memory mapped IO means that part of the address space is dedicated to perform IO and cannot be used for normal memory. You could build a computer without memory mapped IO and do all IO in the IO space. Modern PCs use both, memory mapped IO and the IO space. Don't forget that not all processors have a seperate IO space. (e.g. ARM does not) – Mackie Messer Mar 31 '13 at 11:33