Playback speed
Share post
Share post at current time

Paid episode

The full episode is only available to paid subscribers of Computer, Enhance!

Simulating Conditional Jumps

Time to promote the decoding byte offset to a real register.

This is the eighth video in Part 1 of the Performance-Aware Programming series. Please see the Table of Contents to quickly navigate through the rest of the course as it is updated weekly. The homework files (listing 48, 49, and optionally 50) are available on the github.

If you've done the homework up to this point, you've already implemented part of today’s homework. It may not have seemed like it, because you did it for a different purpose. You weren't thinking about simulating an 8086 when you were doing it, but anyone who processes the bytes of an instruction stream has to do this operation, not just the CPU!

In order to process an instruction stream, you needed something that told you where in the instruction stream you were. As you decoded instructions, you must have kept track of which byte you were on, so you knew which byte to process next. You may have done this using a utility, like an iostream or something, which feeds you bytes at a time. Or you may have done this with an integer which you incremented each time you read a byte. But somehow, something in your program kept track of where you were.

The CPU has to do the exact same thing. It has a dedicated register called the Instruction Pointer (IP) register whose sole purpose is keeping track of the byte offset in the instruction stream. This register tells the processor where the next byte of code comes from as it decodes an instruction stream.

The IP register is similar to the flags register we looked at in the previous post: the IP register is not a register you operate on using the standard operations you use on the general purpose registers. You cannot target this register using the same arithmetic or logic instructions you use for the other registers. If you recall the REG and R/M decoding tables from the decoding homework, you already knew this: just like the flags register wasn’t in that table, neither is the IP register. So most instructions cannot operate on these registers. But just like the flags register, there are special instructions that do operate on the IP register.

So what exactly is the IP register and why do we care about it?

The full video is for paid subscribers

Programming Courses
A series of courses on programming topics.
Casey Muratori