'**************************************************************** '* Name : MATH32u.BAS * '* Author : Ranjit Diol http://www.compsys1.com/workbench/ * '* Notice : Copyright (c) 2003 COMPSys * '* : All Rights Reserved * '* Date : 9/18/03 * '* Version : 1.0 * '* Notes : 32 bit Add/Subbtract Routines * '* : Use a terminal for the demonstration * '* : 9600 baud 8,N,1 * '* : Compiler: PICBasicPro Ver 2.43 * '* * '**************************************************************** ' '**************************************************************** ' DISCLAIMER: This file is being released as non-commericial * ' software. It is being provided "AS IS", neither the author, * ' nor COMPSys shall be held liable for any damages, direct or * ' indirect caused by its use. * '**************************************************************** ' 'Notes: ' Since PBP does not have a double word type 32 bit values have to be made up ' of two word variables. Hexadecimal values are used to load the variables ' 32bit value = HIGH word and LOW word ' Num32 = Num1H and Num1L , where Num1H and Num1L are 16bit word values ' 'Example: ' Num1 = 2234567890 decimal = 8530 CCD2 hexadecimal ' Num2 = 1098765432 decimal = 417D D478 hexadecimal ' Addition: ' Num1 + Num2 = 3333333322 decimal = C6AE A14A hexadecimal ' Subtraction: ' Num1 - Num2 = 1135802458 decimal = 43B2 F85A hexadecimal ' ' Results are returned in Res32H and Res32L ' 'Note: All values are unsigned ' '**************************************************************** ' Variables: ' Num1H,Num1L is the pair for the first 32bit number ' Num2H,Num2L is the pair for the second 32bit number ' Res32H, Res32L is the pair for the result ' cbit is a bit variable which is used to hold the carry result ' 'MCU: PIC16F877 w/20Mhz osc and a Max232 for serial I/O DEFINE LOADER_USED 1 'If using a bootloader DEFINE OSC 20 'Xtal speed DEFINE DEBUG_REG PORTC 'Debug pin port DEFINE DEBUG_BIT 6 'Debug pin bit DEFINE DEBUG_BAUD 9600 'Debug baud rate 'MODE: TRUE (if using a MAX232) or INVERTED (direct connection) DEFINE DEBUG_MODE 0 'Debug mode: 0 = True, 1 = Inverted DEFINE DEBUGIN_MODE 0 'Debugin mode: 0 = True, 1 = Inverted DEFINE DEBUGIN_REG PORTC 'Debugin pin port DEFINE DEBUGIN_BIT 7 'Debugin pin bit SYMBOL cb=STATUS.0 'Carry status register bit Res32H var word 'Result high word Res32L var word 'Result low word Num1H var word 'Number 1 high word Num1L var word 'Number 1 low word Num2H var word 'Number 2 high word Num2L var word 'Number 2 low word cbit var bit 'Carry bit debug 13,10,"Starting..",13,10 goto Demo '*********** SUB ROUTINES *************** '32 bit Addition unsigned 'Given Num1H,Num1L,Num2H,Num2L returns Res32H,Res32L Add32unsigned: Res32L = Num1L + Num2L 'Add the low values cbit = cb 'Check the carry bit Res32H=Num1H+Num2H+cbit 'Add the high values plus the carry bit return '32 bit Subtraction unsigned Sub32unsigned: Res32L = Num1L - Num2L 'Subtract the low values cbit = cb 'Check the carry bit if cbit = 1 then Res32H=Num1H-Num2H 'No carry else Res32H=Num1H-Num2H-1 'Adjust for the carry bit endif return '*********** END SUB ROUTINES ************ Demo: debug 10,13,"DEMO of 32 bit insigned Addition & Subtraction",13,10 debug "32bit values: Num1 = 2234567890 (dec) = 8530CCD2 (hex)",13,10 debug " Num2 = 1098765432 (dec) = 417DD478 (hex)",13,10 'Assign the word values in hex Num1H = $8530 : Num1L = $CCD2 Num2H = $417D : Num2L = $D478 'Perform the calculations. 'The results should be: Addition=C6AEA14A Subtraction=43B2F85A debug 13,10,"Calculated results:",13,10 goto Calc '============ MAIN PROGRAM =============== Main: debug 10,13,"You can try it by entering two 32bit numbers as hex values",13,10 debug "The first number should be the one with the higher value",13,10 debug "Enter a ~ (tilde) to begin.....",13,10 debugin [wait("~")] debug 13,10,"Enter Num1 as a high word and low word in hex",13,10 debugin 20000,Main,[hex4 Num1H,hex4 Num1L] debug 13,10,"Enter Num2 as a high word and low word in hex",13,10 debugin 20000,Main,[hex4 Num2H,hex4 Num2L] Calc: gosub Add32unsigned debug 13,10,"ADDITION Result in hex: ",hex4 Res32H,Hex4 Res32L, " (Carry bit=",dec cbit,")",10,13 gosub Sub32unsigned debug 13,10,"SUBTRACTION Result in hex: ",hex4 Res32H,Hex4 Res32L, " (Carry bit=",dec cbit,")",10,13 goto main '========= END MAIN PROGRAM ============= end