The problem is the "negate" step. You cannot negate the two bytes separately. Negation is the same as a ones-complement followed by adding one. This is covered in Atmel's application note #202. They do it like this: ;***** Register Variables .def ng1l = r16 .def ng1h = r17 ;***** Code ng16: com ng1l ;Invert low byte ;Calculated by com ng1h ;Invert high byte ;inverting all subi ng1l,low(-1) ;Add 0x0001, low byte ;bits then adding sbci ng1h,high(-1) ;Add high byte ;one (0x0001) ;Expected result is 0xCBEE Jim --- In AVR-Chat@yahoogroups.com, "Leon Heller" <leon.heller@d...> wrote: > ----- Original Message ----- > From: Geert De Pecker > To: AVR-Chat@yahoogroups.com > Sent: Sunday, January 02, 2005 10:21 PM > Subject: [AVR-Chat] newbie absolute value > > > Hi, > > I have a question about abtaining the absolute value. I want to > substract a 1 16bit values from eachother, but when the result is > negative, set a bit and get the absolute value of the difference. > > I thought of the following, but that doesn't seem to work: > > ... > ldi direction, 0 > ; Substract values > sub moveL, curPosL > sbc moveH, curPosH > ; If result positive, ok > brcc PM1 > ; Result negative, take absolute value and set flag > neg moveL > neg moveH > ldi direction, 1 > PM1: > > Any hints? > > I'd compare them first, to find which is the bigger of the two, then > subtract them appropriately with two separate bits of code, setting the flag > as necessary. > > Leon > > > > -- > No virus found in this outgoing message. > Checked by AVG Anti-Virus. > Version: 7.0.298 / Virus Database: 265.6.7 - Release Date: 30/12/2004
Message
Re: newbie absolute value
2005-01-03 by jimmcguffin
Attachments
- No local attachments were found for this message.