Take the 2-minute tour ×

I know there is this question already discussed, but I still don´t understand something, so please just help me clarify it.

What I understand there is 2 way to do I/O aka communicate from CPU with other HW. One is to use in and out instructions, and second is the memory mapped.

But what I don´t actually understand is, is IN and OUT instructions are used, you define source port. But what is this port? I mean, is it different set of pins on CPU or what? And, to what is that port connected?

And for the memory mapped, I miss just a tiny detail. Wheather memory mapped I/O must be first set by IN and OUT instructions, or does the device actually somehow itself connects to the RAM and reads it? Thanks.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

On a Z80, there's a pin on the chip that is asserted (set to the high or logical 1 state) when the instruction is an IN or OUT instruction. The hardware connected to the chip monitors that pin, plus the Read/Write pins, plus some of the address pins, to determine whether the operation refers to them. Each device is configured to recognize some number as their port number and to respond accordingly. So, when you write an assembler instruction such as:


the chip sets the low-order 8 address pins to 15 and writes the contents of register A to the data pins. If the hardware is configured and connected properly, it knows that is intended for it. Similarly with IN(15),A.

With memory mapped I/O, there is a memory location reserved for the hardware. When the CPU writes to address 0xFFF0, say (assuming a convenient 8-bit micro-architecture such as the the 6502 or 6800 or 6809 - or, indeed, Z80), then the hardware connected to respond to that address is not a RAM chip but the device. Typically, there is at least one nearby address that is used for reading; sometimes, the same address is used for both read and write.

In both cases, the issue is basically that the hardware connected to the CPU recognizes certain patterns of activity on the chips pins (some control pins, the data pins and the address pins) as referring to them. You can run into problem if several different devices all think the same address or I/O port refers to them.

Although I've used 8-bit chips for the examples, the same basic principles apply to 16-bit, 32-bit or 64-bit chips.

share|improve this answer
Can I have one further question? So, when using IN(OUT) or memory mapped I/O, is RAM disconnected from the CPU, or does it the same time it writes to some chip also writes to the RAM? –  user32569 May 4 '10 at 15:56
@b-gen-jack-o-neill: for an IN or OUT instruction, the memory would (normally) ignore the request because the I/O pin is asserted. With memory mapped I/O, there is no RAM at the address - the I/O device is mapped there. So, the net result is "no, what is written goes to the I/O device". If someone misdesigned the memory system, I suppose you might have memory paying attention to the I/O request, but chaos will ensue -- does the I/O device or the memory win on an IN instruction where the memory tries to provide an answer as well as the I/O device. It won't work, and hence you won't see it. –  Jonathan Leffler May 6 '10 at 5:02

Your Answer


