Santiago - 2009-02-22

As i have to deal with signed basic math operations and i could not find another way of having it, i wrote some functions to have it working.

These functions are just for 1 byte values, but used variables are declared as word, the high byte is just for signing purposes: 0 for positive and 1 for negative.

Example:________________________________________________________________

dim value1 as word
dim value2 as word
dim result as word

value1=2
value2=8
result=0

result = Signed_Sub(value1, value2) 'result = 2-8 = -6

result = Signed_Mul(result, value1) 'result = -6 * 2 = -12

result = Signed_Add(result, value2) 'result = -12 + 2 = -10

result = Signed_Div(result, value1) 'result = -10 / 2 = -5

  'Now result = 5, result_H = 1 (negative)

result = Signed_Mul(result, result) 'result = -5 * -5= 25

  'Now result = 25, result_H = 0 (positive)
_________________________________________________________________________

This is a first attemp of signed math, any idea, suggestion or feedback will be apreciated.

Here are the functions, you can save in a file.h in the "include" folder and use #include "file.h" in your program:
_________________________________________________________________________

Function Signed_Add(first_val as word, second_val as word) as word

    if first_val_H.0 off then
        if second_val_H.0 off then  'Adding two positive numbers           
            Signed_Add_H = 0        'set positive sign
            goto general_add
        End if
                                    'Substract: firt_val-second_val
        Signed_Add_H = 0            'set positive sign
        goto general_sub
    End if

    if first_val_H.0 on then
        if second_val_H.0 on then   'Adding to negative numbers
            Signed_Add_H = 1        'set negative sign
            goto general_add
        End if
                                    'Substract: second_val-first_val
        Signed_Add_H = 1            'set negative sign
        goto general_sub
    End if

    general_sub:
        MOVF  second_val,W
        SUBWF first_val,W 
        BTFSC STATUS,C
        goto exit_general_sub
        if Signed_Add_H.0 on then set Signed_Add_H.0 off    'toggle sign
        if Signed_Add_H.0 off then set Signed_Add_H.0 on
        SUBLW 255
        ADDLW 1
        exit_general_sub:
        MOVWF Signed_Add
        goto  exit_signed_add

    general_add:
        MOVF  first_val,W
        ADDWF second_val,w
        MOVWF Signed_Add            'Signed_Add = first_val+second_val

    exit_signed_add:
End Function

Function Signed_Sub(first_val as word, second_val as word) as word
    if second_val_H.0 on then set second_val_H.0 off    'toggle second_val sign
    if second_val_H.0 off then set second_val_H.0 on
    Signed_Sub = Signed_Add(first_val, second_val)
End Function

Function Signed_Mul(first_val as word, second_val as word) as word
    If first_val_H = second_val_H then
        Signed_mul_H = 0
        goto continue_signed_mul
    End if
    Signed_mul_H = 1
    continue_signed_mul:
    MOVF  first_val,W
    MOVWF SysCalcTempA
    MOVF  second_val,W
    MOVWF SysCalcTempB  
    call  SysMultSub
    MOVF  SysCalcTempX,W
    MOVWF Signed_mul
End Function

Function Signed_Div(first_val as word, second_val as word) as word
    If first_val_H = second_val_H then
        Signed_div_H = 0
        goto continue_signed_div
    End if
    Signed_div_H = 1
    continue_signed_div:
    MOVF  first_val,W
    MOVWF SysCalcTempA
    MOVF  second_val,W
    MOVWF SysCalcTempB  
    call  SysDivSub
    MOVF  SysCalcTempA,W
    MOVWF Signed_Div
End Function
_________________________________________________________________________