To put numbers into the machine right now, we have to edit the bits in memory slots by hand, and to see the result, we have to look into the registers. For people to use it, they need to type numbers on a keyboard and see results on a screen.
Devices that let the CPU exchange values with the outside world, like keyboards and monitors, are called input/output devices. We could make new instructions for handling devices, but it’s simpler to assign a few memory addresses to them.
Then the CPU reads input with LOAD 14 and sends results out with STORE 15,
just as before. No new instructions are needed. This approach is called
memory-mapped I/O.
Let’s attach two devices to the machine from the instruction set post. The input device writes into slot 14 through the memory’s K0 port, and the output device reads slot 15 through the S0 port. The memory holds a program that keeps adding 1 to the input and sending it out.
Address 0: LOAD 14 ← input into R0
Address 1: ADDI R0, 1 ← add 1
Address 2: STORE 15 ← to the output
Address 3: JUMP 0 ← back to the start
Monitors work the same way. Each dot on the screen gets its own memory slot, and a dot is painted black when its slot’s bit is 1. This region is called the framebuffer. The output device above showed a single slot, slot 15, while a monitor shows millions of such slots at once. Real screens use several bytes per dot to hold colors.
0: white, 1: black
Change the top line to 1111111111. Only the top line of the screen turns
black.
A keyboard gives each key a number (a key code) and writes the number of the pressed key into an input slot. The CPU reads that slot to know which key was pressed.
By assigning a few memory slots to devices, the CPU exchanges numbers with the outside using only LOAD and STORE. Now we have a CPU, memory, and input/output devices. In the next post, let’s put the three together and lay out the whole computer.
Slot 14 is the input slot and slot 15 is the output slot. With 3 on the input device, we run the program below. When it stops at HALT, what number does the output device show? Answer with a number.
Address 0: LOAD 14 Address 1: MOVE R1, R0 Address 2: ADD R0, R1 Address 3: STORE 15 Address 4: LOAD 15 Address 5: ADD R0, R1 Address 6: STORE 15 Address 7: HALT