Subtracting Numbers with DOS Alone
A subtraction routine is easily derived from the addition routine presented in the Real Math article. If you understand it, the changes needed to create a subtraction routine aren't too hard to understand.

The first part of the routine, up through the counting on the number line for the first number is nearly identical in both routines. The first significant difference is that the number line arguments are reversed on the recursive command line. Second, the shifter for the first number is also reversed to make the number of ticks left on the command line be equal to the first number. The third difference is that the carry flag is handled slightly differently, being included with the processing of the second number. Finally, a leading zero processor needed to be added at the end of the routine to give a consistent output format.

:: SUBTRACT.BAT - An efficient DOS routine that finds the unsigned difference :: :: of two numbers, i.e. Num2 is assumed to be larger than Num1. :: Use COMPARE.BAT to insure this is true before calling :: SUBTRACT or results might be incorrect. ::Tom Lavedas <lavedas@pressroom.com> :: http://www.pressroom.com/~tglbatch @echo %dbgs% off %4 if [%3]==[] for %%v in (echo goto:End) do %%v Syntax: %0 VarName Num1 Num2 set {0}=%0 %1 for %%v in (%1 [CF] {S} \0 \1 \2 \3 \4 \5 \6 \7 \8 \9) do set %%v=&gt; nul &gt; %2.{1 echo.%/ Pad numbers out to eight digits /% &gt; %3.{2 echo. for %%v in (?.{? ??.{? ???.{? ????.{? ?????.{? ??????.{? ???????.{?) do ren %%v 0%%v &gt; %temp%.\{t}.bat %{0}% {1 {2 goto:Parse call :Parse numbers if exist *.%2 copy *.%2 %%_%256%%? &gt; nul %/ Get last digit /% &gt;&gt;%temp%.\{t}.bat for %%v in (0 %%_%256%%?) do echo set %2}=%%v if exist *.%2 ren *.%2 ???????.%2 %/ Drop last digit /% for %%v in (???????.%2) do ren %%v 0%%v %/ Shift right 1 digit /% for %%v in (shift %4) do %%v %temp%.\{t}.bat for %%v in (00000000.{? %%_??56%%?) do del %%v %{0}% 2 3 goto:Sub 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 :Error for %%v in (echo goto:End) do %%v ERROR - Improperly formed number. &gt; con :Sub tract digits set \%{1}%= 1 2 3 4 5) do shift; for %%v in (%\9% x%\8% x%\7% x%\6% x%\5% x%\4% x%\3% x%\2% x%\1% x%\0%) do goto Error set \%{1}%=&gt; nul set \%{2}%= %[CF]%) do shift; for %%v in (%\0% x%\1% x%\2% x%\3% x%\4% x%\5% x%\6% x%\7% x%\8% x%\9%) do goto Error for %%v in (set shift) do %%v {S}=%0 %{S}%&gt; nul for %%v in (\%{2}% [CF]) do set %%v=&gt; nul if [%9]==[] set [CF]=1&gt; nul &gt; %temp%.\{t}.bat if exist *.{? %{0}% {1 {2 goto:Parse call %{0}% 2 3 goto:Trim %{S}% :Trim leading zeros if not [%5]==[] if %5==0 for %%v in (shift goto:Trim) do %%v for %%v in (1 2 3) do shift for %%v in (0 %2%3%4%5%6%7%8%9) do set {S}=%%v for %%v in (%{0}%) do set {0}=%%v echo.%{0}%=%{S}% set %{0}%=%{S}%&gt; nul :End for %%v in (*.{? %temp%.\{t}.bat?) do del %%v for %%v in ([CF] {S} {0} {1} {2}) do set %%v=&gt; nul ::

  Some Caveats

The subtraction routine returns an unsigned number. That is, it assumes the first number is equal to or less than the second number. Unexpected results will occur if this is not true. Use the Comparison routine before calling SUBTRACT to insure proper results.

Input strings containing more than eight characters will also give unexpected results, while strings with non-number characters return an error message and the named result variable is empty. Use that fact for error handling in the calling routine.




Top | General Notes | Homepage