*
*``````````````````````````````*
* DEMO.MATHBAS.ASM             *
*                              *
* A DEMO OF THE 8-BIT AND THE  *
* 16-BIT MACROS FOR ADDING,    *
* SUBTRACTING, MULTIPLYING AND *
* DIVIDING. A 16-BIT COMPARE   *
* MACRO IS ALSO INCLUDED AS    *
* PART OF THE COLLECTION.      *
*                              *
* AUTHOR:    NATHAN RIGGS      *
* CONTACT:   NATHAN.RIGGS@     *
*            OUTLOOK.COM       *
*                              *
* DATE:      04-JUN-2021       *
* ASSEMBLER: MERLIN 8 PRO      *
* OS:        DOS 3.3           *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** ASSEMBLER DIRECTIVES
*
         CYC   AVE
         EXP   OFF
         TR    ON
         DSK   DEMO.MATHBAS
         OBJ   $BFE0
         ORG   $6000
*
*``````````````````````````````*
*  TOP INCLUDES (HOOKS,MACROS) *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
         PUT   MIN.HEAD.REQUIRED.ASM
         USE   MIN.MAC.REQUIRED.ASM
         PUT   MIN.HEAD.MATH.ASM
         USE   MIN.MAC.MATH8.ASM
         USE   MIN.MAC.MATH16.ASM
]HOME2   EQU   $FC58
*
*``````````````````````````````*
*      PROGRAM MAIN BODY       *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
         JSR   ]HOME2
*
*``````````````````````````````*
* 8-BIT BASIC MATH MACROS      *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** THIS COLLECTION CONTAINS MACROS FOR 8-BIT
** BASIC MATH, INCLUDING ADDITION, SUBTRACTION,
** MULTIPLICATION AND DIVISION. CURRENTLY, ONLY
** UNSIGNED NUMBERS ARE SUPPORTED, THOUGH THIS
** WILL LIKELY CHANGE IN A NEAR-FUTURE REVISION.
*
*``````````````````````````````*
* 8-BIT ADDITION               *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** 8-BIT ADDITION IS RATHER TRIVIAL, BUT IS
** INCLUDED HERE FOR BEGINNERS' REFERENCE. IF
** YOU ARE CAPABLE OF WRITING THESE ROUTINES
** YOURSELF, YOU ARE LIKELY TO SAVE AT LEAST
** 8 CYCLES, AS THE MACRO USES THE RETURN
** ADDRESS FOR PASSING BACK THE SUM. THIS SUM
** IS ALSO PASSED BACK VIA THE .A REGISTER,
** BUT THERE ARE STILL WASTED CYCLES.
*
** ADDITION IS PARTICULARLY SIMPLE: PASS THE
** TWO BYTE VALUES TO BE ADDED, THEN EXPECT
** THE SUM IN .A AS WELL AS IN RETURN.
*
** NOTE THAT CURRENTLY, THE 8-BIT ADDITION
** AND SUBTRACTION ROUTINES ONLY ACCEPT A
** DIRECT ADDRESS, AND THUS DOES NOT ALLOW
** FOR THE INDIRECT ADDRESSING WORKAROUND THAT
** MOST OF THE REST OF THE LIBRARY USES. THIS
** IS FOR THE SAKE OF PRESERVING CYCLES AND
** DISCARDING BYTES.
*
         JSR   ]HOME2
         _PRN  "8-BIT ADDITION",8D
         _PRN  "==============",8D8D
         _PRN  "10 + 20 =",8D
         ADD8  NUM81;NUM82
         DUMP  #RETURN;RETLEN
         _WAIT
*
*``````````````````````````````*
* 8-BIT SUBTRACTION            *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** 8-BIT SUBTRACTION WORKS THE SAME WAY AS
** ADDITION (EXCEPT, OBVIOUSLY, IN THE FACT
** THAT THEY ARE OPPOSITE OPERATIONS), AND
** ALSO HAS THE SAME LIMITATION: NO ADDRESSING
** MODES OTHER THAN THE STANDARD PASSING OF
** ADDRESSES WHERE THE ARGUMENT VALUES ARE
** LOCATED.
*
         JSR   ]HOME2
         _PRN  "8-BIT SUBTRACTION",8D
         _PRN  "=================",8D8D
         _PRN  "20 - 10 =",8D
         SUB8  NUM82;NUM81
         DUMP  #RETURN;#1
         _WAIT
*
*``````````````````````````````*
* 8-BIT MULTIPLICATION         *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** 8-BIT MULTIPLICATION IS A BIT MORE COMPLICATED
** THAN ADDITION AND SUBTRACTION UNDER THE HOOD,
** BUT CALLING IT REMAINS THE SAME. LIKE WITH
** THE OTHER 8-BIT BASIC MATH MACROS, ONLY LITERALS
** AND DIRECT ADDRESSES ARE ACCEPTED AS PARAMETERS,
** UNLIKE THE REST OF THE LIBRARY, TO SAVE RESOURCES
** IN SUBROUTINES THAT ARE LIKELY TO BE CALLED MUCH
** MORE THAN OTHERS AS PART OF A PROGRAM (AND OFTEN
** IN LOOPS).
*
         JSR   ]HOME2
         _PRN  "8-BIT MULTIPLICATION",8D
         _PRN  "====================",8D8D
         _PRN  "10 * 20 = ",8D
         MUL8  NUM81;NUM82
         DUMP  #RETURN;RETLEN
         _WAIT
*
*``````````````````````````````*
* 8-BIT DIVISION               *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** THIS MACRO IS USED LIKE ALL OTHER 8-BIT
** MACROS, BUT THE ORDER IS IMPORTANT HERE:
** THE FIRST VALUE PASSED IS THE DIVIDEND
** WHILE THE SECOND VALUE US THE DIVISOR. IT
** MAY HELP TO THINK OF THE SEMI-COLON HERE
** AS STANDING FOR "BY" SO THAT THE STATEMENT
** CAN BE READ AS "DIVIDE DIVIDEND BY DIVISOR."
*
** 8-BIT DIVISION IS LIMITED TO ARGUMENTS THAT
** ARE EITHER A DIRECT ADDRESS OR A LITERAL VALUE,
** LIKE OTHER 8-BIT MATH ROUTINES.
*
         JSR   ]HOME2
         _PRN  "8-BIT DIVISION",8D
         _PRN  "==============",8D8D
         _PRN  "20 / 10 = ",8D
         DIV8  NUM82;NUM81
         DUMP  #RETURN;RETLEN
         _WAIT
*
*``````````````````````````````*
* 16-BIT BASIC MATH MACROS     *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** BOTH ADDITION AND SUBTRACTION OF 16-BIT
** NUMBERS IS ALSO RATHER TRIVIAL, BUT CAN
** BE CONFUSING TO SOMEONE NEW TO 6502
** ASSEMBLY (ESPECIALLY WITHOUT A FULL
** UNDERSTANDING OF HOW ADC WORKS). EVEN STILL,
** IT CAN BECOME A BIT TEDIOUS TO CONSTANTLY
** WRITE OUT THE ROUTINES MANUALLY, AS THEY DO
** CONSTITUTE A SMALL NUMBER OF BYTES THAT CAN
** GET TIRESOME ON REPEAT.
*
*``````````````````````````````*
* 16-BIT ADDITION              *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** USING THE 16-BIT ADDITION MACRO IS SIMILAR TO ITS
** 8-BIT COUSIN: TWO ADDRESSES ARE PROVIDED THAT HOLD
** THE VALUES TO BE ADDED. THE TWO-BYTE SUM IS THEN
** PASSED BACK VIA RETURN (WITH RETLEN HOLDING ITS
** LENGTH, 2) AS WELL AS IN .A (LOW BYTE) AND .X (HIGH).
*
** LIKE 8-BIT ADDITION, 16-BIT ADDITION ALSO HAS EXTRA
** BYTES AND CYCLES THAT CAN BE EASILY DISCARDED EITHER
** BY ALTERING THE MACRO AND RELATED SUBROUTINE ITSELF OR
** BY SIMPLY DOING THE ADDITION MANUALLY.
*
         JSR   ]HOME2
         _PRN  "16-BIT ADDITION",8D
         _PRN  "===============",8D8D
         ADD16 NUM161;NUM162
         _PRN  "300 + 400 =",8D
         DUMP  #RETURN;RETLEN
         _WAIT
*
*``````````````````````````````*
* 16-BIT SUBTRACTION           *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** 16-BIT SUBTRACTION FOLLOWS THE SAME SETUP
** AND RULES AS 16-BIT ADDITION:
*
         JSR   ]HOME2
         _PRN  "16-BIT SUBTRACTION",8D
         _PRN  "==================",8D8D
         _PRN  "400 - 300 = ",8D
         SUB16 NUM162;NUM161
         DUMP  #RETURN;RETLEN
         _WAIT
*
*``````````````````````````````*
* 16-BIT MULTIPLICATION        *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** BY NOW, THE METHOD SHOULD BE OBVIOUS: MUL16
** TAKES TWO ARGUMENTS WHICH ARE ADDRESSES THAT
** HOLD THE TWO VALUES TO BE MULTIPLIED.
*
         JSR   ]HOME2
         _PRN  "16-BIT MULTIPLICATION",8D
         _PRN  "=====================",8D8D
         _PRN  "300 * 400 = ",8D
         MUL16 NUM161;NUM162
         DUMP  #RETURN;RETLEN
         _WAIT
*
*``````````````````````````````*
* 16-BIT DIVISION              *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** 16-BIT DIVISION WORKS LIKE 8-BIT DIVISION,
** WITH THE RESULT STORED IN RETURN (2 BYTES)
** AS WELL AS IN .A (LOW BYTE) AND .X (HIGH).
** ADDITIONALLY, THE REMAINDER IS HELD IN THE
** .Y REGISTER--A MODULUS FUNCTION CAN SIMPLY
** CALL THIS DIVISION MACRO AND THEN READ THE
** VALUE OF .Y FOR THE ANSWER.
*
         JSR   ]HOME2
         _PRN  "16-BIT DIVISION",8D
         _PRN  "===============",8D8D
         _PRN  "400 / 300 = ",8D
         DIV16 NUM162;NUM161
         DUMP  #RETURN;RETLEN
         _WAIT
*
*``````````````````````````````*
* 16-BIT COMPARISON            *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** LASTLY, A 16-BIT COMPARISON MACRO IS PROVIDED
** THAT MIMICS THE FUNCTIONALITY OF THE CMP INSTRUCTION,
** BUT OF COURSE FOR 16-BIT VALUES. THE ADDRESSES
** OF TWO SEPARATE VALUES ARE PROVIDED, AND THEN THE
** COMPARISON IS MADE WITH THE FOLLOWING RESULTS:
*
** Z FLAG = 1 IF BOTH VALUES ARE EQUAL
** C FLAG = 0 IF FIRST IS GREATER THAN SECOND
** C FLAG = 1 IF FIRST IS LESS THAN OR EQUAL TO SECOND
** N FLAG = 1 IF A SIGNED FIRST IS GREATER THAN A SIGNED SECOND
** N FLAG = 0 IF SIGNED FIRST IS LESS THAN OR EQUAL TO
**            THE SIGNED SECOND PARAMETER
*
         JSR   ]HOME2
         _PRN  "16-BIT COMPARISON",8D
         _PRN  "=================",8D8D
         _PRN  "CMP16 #300;#400",8D8D
         CMP16 NUM161;NUM162
         BEQ   :GREATER
         BNE   :LESSOREQ
:GREATER
         _PRN  "FIRST IS GREATER THAN SECOND.",8D8D
         JMP   :EXIT
:LESSOREQ
         _PRN  "FIRST IS <= THE SECOND PARAMETER.",8D8D
:EXIT
         _WAIT
*
         JSR   ]HOME2
         _PRN  "DONE.",8D8D8D
*
         JMP   REENTRY
*
*``````````````````````````````*
*        BOTTOM INCLUDES       *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** BOTTOM INCLUDES
*
         PUT   MIN.LIB.REQUIRED.ASM
*
** INDIVIDUAL SUBROUTINE INCLUDES
*
** 8-BIT MATH SUBROUTINES
*
         PUT   MIN.SUB.MULTU8.ASM
         PUT   MIN.SUB.DIVDU8.ASM
*
** 16-BIT MATH SUBROUTINES
*
         PUT   MIN.SUB.ADDIT16.ASM
         PUT   MIN.SUB.SUBT16.ASM
         PUT   MIN.SUB.COMP16.ASM
         PUT   MIN.SUB.MULTU16.ASM
         PUT   MIN.SUB.DIVDU16.ASM
*
NUM81    DB    10
NUM82    DB    20
NUM161   DW    300
NUM162   DW    400
