Since addition and subtraction are complementary functions, one would expect that the program for subtraction would be complementary. This is quite true - the code is pretty much the same, only the look-up tables are different. The tables for subtraction are derived from those for addition by simple transforms: the result tables are obtained by interchanging the two tables, and reversing the column order; the carry tables simply have their row orders inverted:
set r00=0 9 8 7 6 5 4 3 2 1
set r01=1 0 9 8 7 6 5 4 3 2
set r02=2 1 0 9 8 7 6 5 4 3
set r03=3 2 1 0 9 8 7 6 5 4
set r04=4 3 2 1 0 9 8 7 6 5
set r05=5 4 3 2 1 0 9 8 7 6
set r06=6 5 4 3 2 1 0 9 8 7
set r07=7 6 5 4 3 2 1 0 9 8
set r08=8 7 6 5 4 3 2 1 0 9
set r09=9 8 7 6 5 4 3 2 1 0
set c00=0 1 1 1 1 1 1 1 1 1
set c01=0 0 1 1 1 1 1 1 1 1
set c02=0 0 0 1 1 1 1 1 1 1
set c03=0 0 0 0 1 1 1 1 1 1
set c04=0 0 0 0 0 1 1 1 1 1
set c05=0 0 0 0 0 0 1 1 1 1
set c06=0 0 0 0 0 0 0 1 1 1
set c07=0 0 0 0 0 0 0 0 1 1
set c08=0 0 0 0 0 0 0 0 0 1
set c09=0 0 0 0 0 0 0 0 0 0
set r10=9 8 7 6 5 4 3 2 1 0
set r11=0 9 8 7 6 5 4 3 2 1
set r12=1 0 9 8 7 6 5 4 3 2
set r13=2 1 0 9 8 7 6 5 4 3
set r14=3 2 1 0 9 8 7 6 5 4
set r15=4 3 2 1 0 9 8 7 6 5
set r16=5 4 3 2 1 0 9 8 7 6
set r17=6 5 4 3 2 1 0 9 8 7
set r18=7 6 5 4 3 2 1 0 9 8
set r19=8 7 6 5 4 3 2 1 0 9
set c10=1 1 1 1 1 1 1 1 1 1
set c11=0 1 1 1 1 1 1 1 1 1
set c12=0 0 1 1 1 1 1 1 1 1
set c13=0 0 0 1 1 1 1 1 1 1
set c14=0 0 0 0 1 1 1 1 1 1
set c15=0 0 0 0 0 1 1 1 1 1
set c16=0 0 0 0 0 0 1 1 1 1
set c17=0 0 0 0 0 0 0 1 1 1
set c18=0 0 0 0 0 0 0 0 1 1
set c19=0 0 0 0 0 0 0 0 0 1
The subtraction program even uses the same engines for string processing and computation as does addition.
However, this leaves us with two cleanup tasks: stripping leading zeros (if desired) and flagging underflow. The carry flag marks underflow, which causes the result to be 00000000 and STRIPLD0.BAT handles leading zero suppression:
set ax=%result%
if %CF% == 1 set ax=0000000000
:: // the following line is optional
if %CF% == 0 call stripld0
set result=%ax%
If the code is used to build filenames, leading zero suppression will likely not be wanted - rather it will probably be desired that the result have some specified length. Since the result has the length of the longer of the two original numbers, the base can be padded with leading zeros to force the result to be the desired length.
STRIPLD0.BAT (reusing much of the GETNSTR.BAT code) deals with the leading zeros (the result otherwise has the same number of digits as the longer of the two numbers fed into the engine).
goto %IP% // set to "start" by calling program
:char // handles character part of string
if not %1 == 0 set c=%c%%1
set IP=str
goto end
:str // handles remainder of string
set s=%1
set IP=char
goto end
:start // strip leading zeros
set IP=char
set ax=%result%!
set s=
set c=
:loopa
for %%a in (/%ax%) do call %0 %%a
set ax=%s%
if not %s% == ! goto loopa
set ax=%c%
set c=
set s=
:end
Note that the set c=%1 %c% line (from GETNSTR.BAT) has acquired a test
for not zero and that the order of concatenation is reversed (and the
space has been removed). The second loop and all references to x, y
and bx were also removed, and the input is now taken from RESULT instead
of x (but RESULT is not cleared). The return is in ax.
The subtraction program, then, consists of the supervisor program (TESTSUB.BAT, which sets up the LUT), the string processor (GETNSTR.BAT), the adder program (DADD.BAT), and the leading zero suppressor (STRIPLD0.BAT). The preceding links are for viewing, or downloading the files with non-Netscape browsers. Keep in mind that Netscape trashes line ends when both the source and target files are in DOS format. If you are using that browser under Windows, you should download the ALLSUB.ZIParchive instead, since the server is also running under Windows and the files are therefore in DOS format.
** Copyright 1995, Ted Davis - all rights reserved **
Input and feedback from readers are welcome. NOTE: the subject of the message must contain the word "batch" for the message to get past the spam filter.
Back to the Table of Contents page
Back to my personal links page - back to my home page