Absolute value of signed constants inside a macro

Thread Starter

atferrari

Joined Jan 6, 2004
4,770
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
Rich (BB 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)
 

beenthere

Joined Apr 20, 2004
15,819
If all values are signed, then test for the MSB being set. If it is, convert to a positive number by zeroing the MSB. You can skip the test and simply clear the MSB, for that matter.
 

Papabravo

Joined Feb 24, 2006
21,225
If all values are signed, then test for the MSB being set. If it is, convert to a positive number by zeroing the MSB. You can skip the test and simply clear the MSB, for that matter.
This only works for numbers in sign-magnitude representation. In 2s complement notation you can't get the additive inverse by clearing the MSB. There are two ways I know of to take the 2s complement:

  1. Subtract the negative number from 0
  2. Take the 1s complement and add 1
 

AlexR

Joined Jan 16, 2008
732
If all values are signed, then test for the MSB being set. If it is, convert to a positive number by zeroing the MSB. You can skip the test and simply clear the MSB, for that matter.
That is not quite right!
Yes you can test for a negative number by looking at the MSB or for that matter just load the number into the the W register and check the N bit, but to get the absolute value of a negative number you have to take the 2's complement of the number (invert the bits and add 1).
Another way to do it iis to multiply the negative number by -1, this is simple in the PIC18 series since they have a multiple command in their instruction set.
 

Thread Starter

atferrari

Joined Jan 6, 2004
4,770
Thanks to all for your time.

Had to use MPSIM to be sure (shame on me :( ) that my negative constants were expressed in (what else?) two's complement.

Finally solved as follows:

Rich (BB 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
 
Top