Magerman wrote
I'm curious: is there a better way to do it than that, or was that solution perfectly valid?
These are two different questions. They way you did it was perfectly valid -- ANY solution that meets the requirements is perfectly valid.
Whether or not there a "better way to do it" depends on the metric by which "better" is determined.
Fewer instructions?
Faster execution (which can be, but often is not, related to fewer instructions)?
More readable/maintainable (which is usually inversely related to fewer instructions)?
Less time to develop, implement, and test?
Any of these -- and probably several others -- are reasonable metrics by which to compare one solution to another and decide which of them is better.
It just felt like I was sort of forcing it, I want to make sure I'm not missing anything important for later chapters.
For what it's worth, the solution I came up with has nineteen instructions (not counting labels, which are pseudoinstructions since they don't generate any code) and uses one variable, one conditional jump, and one unconditional jump. Eight of the nineteen instructions are A-instructions. Four of the instructions are before the loop and fifteen are in the loop.
I suspect that it can be condensed by perhaps one or two more instructions, but if so I haven't figured it out. It just seems like it should be possible since I access the variable three times and it seems like there should be a clever way to eliminate at least one of them.