Absolute Value

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
beamrider
Vic 20 Scientist
Posts: 1448
Joined: Sun Oct 17, 2010 2:28 pm
Location: UK

Absolute Value

Post by beamrider »

Anyone have any example code to calculate the absolute value of a number in 6502 assembly.

e.g. abs(x2 - x1) to derive distance between two 8-bit numbers
User avatar
Mike
Herr VC
Posts: 4832
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

For abs(x2-x1), I'm using this code snippet in my Bresenham line plotters:

Code: Select all

 SEC
 LDA x1
 SBC x2
 BCS positive
 EOR #$FF     ; C flag is clear here,
 ADC #$01     ; form two's complement
.positive
 STA dx
... with the absolute value now being stored in dx. x1 and x2 are both unsigned 8-bit values.
User avatar
beamrider
Vic 20 Scientist
Posts: 1448
Joined: Sun Oct 17, 2010 2:28 pm
Location: UK

Post by beamrider »

Mike wrote:For abs(x2-x1), I'm using this code snippet in my Bresenham line plotters:

Code: Select all

 SEC
 LDA x1
 SBC x2
 BCS positive
 EOR #$FF     ; C flag is clear here,
 ADC #$01     ; form two's complement
.positive
 STA dx
... with the absolute value now being stored in dx. x1 and x2 are both unsigned 8-bit values.
perfect..thanks... :D
Post Reply