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
Absolute Value
Moderator: Moderators
- Mike
- Herr VC
- Posts: 4832
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
For abs(x2-x1), I'm using this code snippet in my Bresenham line plotters:
... with the absolute value now being stored in dx. x1 and x2 are both unsigned 8-bit values.
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
perfect..thanks...Mike wrote:For abs(x2-x1), I'm using this code snippet in my Bresenham line plotters:... with the absolute value now being stored in dx. x1 and x2 are both unsigned 8-bit values.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