unregistered wrote:
Thanks 3gengames!
But, I'm still confused
because why should I subtract #128 when the number will be higher and lower? There must be something I don't understand...
You just fix it when it's lower, and don't when it's not.
Think about it this way: To check if it's lower before you do the actual subtract, you STILL have to do a compare (subtract). So why do it before? Do the subtract regardless, and fix the result only if it needs it. (The carry will tell you if it needs fixing!)
Read this:
Quote:
Three things to take away:
1. The carry is ALWAYS taken into account when you use adc or sbc, so make sure it's right for the operation you intend to do before that operation runs. (Clear before addition, set before subtraction)
2. The carry will become the opposite of what you would normally initialize it to if the operation goes outside the boundaries of a byte. (So if an addition would have yielded more than 255, or a subtraction would have yielded less than 0.) Otherwise, the carry becomes what you would normally initialize it to.
3. If the carry is the opposite of what you would normally initialize it to, one extra will be used in the operation. (One extra will be subtracted for sbc, or one extra will be added for adc.)
If that's not enough explanation, try the whole post it's from again:
viewtopic.php?p=112830#p112830Then see how this works:
Code:
lda ladypositionlow
sec
sbc #128;#$80
sta camerapositionlow
lda ladypositionhigh
sbc #$00;High byte of $0080
sta camerapositionhigh
bcs abovezero
lda #$00
sta camerapositionhigh
sta camperapositionlow
abovezero:
;Can be optimized in cute ways... ^_^
I mean... I probably shouldn't encourage you to lift it without understanding, but this working code has been available to study.
What is the state of the carry when ladyposition is < 128 and you subtract 128 from it? What is the state of the carry when ladyposition is > 128 and you subtract 128 from it? In which case do you want to discard the result and use zero? Then branch passed the fix on the opposite state of the carry.