Hexadecimal subtraction : using absolute value

Status
Not open for further replies.

Steve311

Member
Hi All,

What is the best way to represent a absolute value of a number in assembly?

Suppose I want to represent the following in decimal: ABS[-20 - (65)]???

OR Hex equivalevt with sign bit: ABS [0x94 - (0x41)]??

Thanks!
 
Just do a conditional test first to see which number is the greater, then subtract the lesser from the greater.

If is A > B, x = A - B
else x = B - A

Does that give you what you want?
 
Hi chris, yes and no actually. i assume that is in c, i am actually using assembly with an enhanced mid range pic. how is that converted into assembly?
 
To get the ABS value of Var do,
Code:
		movfw	Var		;move into W
		btfss	Var,7		;test if negative
		goto	notNegative	;no, so all done
		comf	Var,w		;complement and move to W
		addlw	1		;add 1 to complete negation
notNegative

The above loads W with the ABS value and leaves Var unchanged.

Edit, If you want the variable to be negated as well then do,
Code:
		btfss	Var,7		;test if negative
		goto	notNegative	;no, so all done
		comf	Var,f		;complement and keep in file
		incf	Var,f		;add 1 to get complete negation
notNegative	movfw	Var		;mov into W

Mike.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…