Mats wrote:
I would say that the task simply is not appropriately formulated in the first place!
Strange; I understood the problem as written perfectly.
Quote:
Code:
CLC
LDA TEMP1
ADC LVALL
STA LVALL
LDA #00
ADC LVALH
STA LVALH
BVS ERROR
RTS
If now the final sum in LVALL,LVALH stays positive at all times, fine! If not, also fine! This is the straight-forward approach in the spirit of the chip design! Rock solid! No "tricks" needed!
The poster of the question is adding a signed 8-bit value to a signed 16-bit quantity. This
mandates that the 8-bit value be
sign-extended first. The code above works if and only if the signed value is guaranteed to be positive at all times.
I'm not a fan of unnecessary tricks except where performance counts (see my thread on arithmetic shifts). I would start off by writing this instead:
Code:
clc
lda TEMP1
bmi isNeg
adc LVALL
sta LVALL
lda #0
adc LVALH
sta LVALH
back:
...etc...
isNeg:
adc LVALL
sta LVALL
lda #$FF
adc LVALH
sta LVALH
jmp back
That would be my initial solution. It takes more memory, and MAY even take more time to execute, but it'll work. Then, from there, I'd optimize as required.
update -- I did a preliminary timing analysis of my code versus lee's with the assumption that everything is stored in zero-page, and it looks like mine is only 3 cycles slower than his in the case of a negative number. Not bad at all, I'd say.