Absolute value of signed constants inside a macro

Status
Not open for further replies.

atferrari

Well-Known Member
Most Helpful Member
PIC 18F family - Assembler
There is a macro enabling, at compiling time, two other macros based on the sign of these parameters, ranging from 127 to -127
Code:
CONSTANT PARAM0 -3 
CONSTANT PARAM1 124 
CONSTANT PARAM2 -56 
CONSTANT PARAM3 -7 
CONSTANT PARAM4 78 
CONSTANT PARAM5 -6
For that I used the expresion PARAM#v(i) where i is a local variable. So far, so good.

My next (unsolved) problem is to get the absolute value of those constants to be used in further code inside the same macro. The solution has eluded me for the last three days.

Any help appreciated.

(Having the awful feeling that the solution is just in front of me)
 
Something like,

Code:
    if n>127
        n=0-n
    endif
;or
    if n>127
        n=~n
        n=n+1
    endif

The above is pseudo because I've never used the C18 macros.

Mike.
 
Last edited:
Solved

Thanks Mike for your time.

Finally solved as follows:

Code:
ABS_TO_TABLE  MACRO TAPS
              LOCAL INDEX
              LOADREG FSR2L,LOW TABLE_COEFFS
              LOADREG FSR2H,HIGH TABLE_COEFFS
 
              INDEX=0
 
              WHILE INDEX<TAPS           ;INDEX =0 to TAPS-1
 
              IF COEFF#v(INDEX)>=0      ;positive coefficient
                MOVLW COEFF#v(INDEX)    ;absolute value already
              ELSE                      ;negative coefficient
                MOVLW -(COEFF#v(INDEX)) ;we get absolute vale
              ENDIF
 
              MOVWF POSTINC2            ;absolute value goes to table.
 
              INDEX=INDEX+1
              ENDW
 
              ENDM
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…