Electronic – What does the 8086 CPU do with the data returned from an address in RAM

computer-architecturecpuramx86

I understand how a CPU works fairly well, but there is this one thing which I've never really gotten the hang of.

Say we have an Intel 8086 CPU (16 bits wide registers) which is about to fetch its next instruction from RAM (also with a 16 bit data width). As the CPU fetches the instruction, say at 0x44AB, what exactly does RAM return? Is it an 8 bit op-code and an 8-bit operand? Does it return a 16 bit op-code, and then the operand is stored in 0x44AC?

In the first case, operands cannot have a value greater than 0xFF (255), but then why would you have 16-bit registers? In the second case, the program counter must be incremented by two between each instruction, and you use twice as much memory.

So how does this work? The question might seem a bit vague, but all I'm asking is really what the CPU does with the data returned from an address in RAM.

EDIT: Made the question more specific so that it's easier to give a good answer.

Best Answer

Neither, as for the 8086 CPU, program code is just bytes of data in memory, and the bitness of registers have almost nothing to do with opcode length, as you can move bytes or 16-bit words, and also far jump to a 32-bit address (defined by segment and offset).

As the 8086 is a CPU with a 16-bit bus and the opcodes can be 1 to 6 bytes long, the opcodes may or may not be aligned in any way to the memory. However a typical optimization (manually or by compiler) is to place 16-bit variables and targets for code jumps at even addresses in the memory.

That is because the bus is 16 bits wide, so it must have 16-bit memory at each even memory address, and all odd memory addresses are simply the high 8-bit part of the 16-bit bus.

So, if there is a command which tells the CPU to go execute code at 0x44AB, which is an odd address, the CPU Bus Interface Unit (BIU) will first read one 8-bit byte from that address into the prefetch queue, and continue fetching 16-bit words at a time from address 0x44AC onwards into the prefetch queue.

The CPU Execution Unit (EU) will simply take bytes from the prefetch queue, or wait for bytes to appear if it is empty, and then decode the opcodes and takes any amount of bytes necessary from the queue to fully decode the opcodes.

If you do want to access read or write 16-bit word on odd addresses, the BIU has to do two 8-bit accesses. It needs to access a byte at the high part of 16 bits of a memory location, and the other byte at the low part of 16 bits of the next memory location.

Related Topic