Simulating ADD, SUB, and CMP

They appear no more difficult than MOV, but when you look closer, they turn out to be quite tricky...


This is the seventh 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 46, and optionally 47) are available on the github.

Hopefully you noticed how much easier the homework was in the previous post as compared to the decoding homework. Once you’ve decoded an immediate or register mov instruction, the simulation part is as simple as a changing the value in an array. It's pretty basic compared to the decoding operations which involved bit field manipulation and dependent parsing of variable numbers of bytes.

In this post I’d like to extend our simulation to cover the same basic arithmetic operations that we learned to decode in the decoding homework. These will be a little bit more challenging than mov, not because the operations themselves are any more complicated, but rather because — unlike to mov instruction — arithmetic operations modify more than just their destination register!

Thanks to the decoding homework, we know something about the registers in the 8086. We know we've got four registers that can be addressed as pairs of 8-bit registers or single 16-bit registers, and four registers that can be addressed only as 16-bit registers:

The full video is for paid subscribers