Step 2 of 2
Record the Borrow
A flag that nobody reads changes nothing. Extend the program so that it also reacts to the borrow.
Keep storing the difference at 2002H, and additionally store 01H at
2003H if the subtraction borrowed, or 00H if it did not.
The one thing: The carry flag is the shortest-lived thing in the machine. Branch on it immediately, or turn it into a byte, before anything else overwrites it.
What is being checked
2002Hstill holds the difference2003Hholds01Hafter a borrow and00Hotherwise2000Hand2001Hstill hold the original numbers- The program reaches
HLT
Flags are the most perishable thing in the machine
The carry flag is one bit, and almost every arithmetic or logical instruction
rewrites it. SUB, ADD, ANA, XRA, RLC — all of them. So the moment
after a subtraction is the only moment you can trust the borrow, and if you
need it later than that you must turn it into something more durable, which is
exactly what this step is doing.
Some instructions are deliberately harmless to the flags, and they are worth memorising because they are what lets you do bookkeeping between a test and its branch:
MOV,MVI,LDA,STA,LXI— no flags touched at allINXandDCX— no flags touched, unlike their 8-bit cousinsINRandDCR— set the zero, sign and parity flags, but leave carry alone
XRA A is the usual way to clear the accumulator, and it is the one to avoid
here: it clears the carry flag along with A.
Progress is kept for this tab only. Sign in to save it across devices.