I'm trying to translate in assembly 6502 language of portion of code that I wrote in C and I'm blocking on dealing with overflow circumstance.
The code consist in computing the signed difference between two signed angle and use the result to index a look-up table.
If the difference is "small" (fits in one signed byte) I can directly use it to index an accurate table.
Otherwise, I divide the difference by two to index a less accurate table.
Code: Select all
signed char RayAlpha;
signed char RayBeta;
signed int RayDeltaAngle;
RayDeltaAngle = RayBeta - RayAlpha;
if (abs(RayDeltaAngle) < 127){
RayDivisor = tab_divisor_acc[(unsigned char)RayDeltaAngle];
} else {
RayDeltaAngle = RayDeltaAngle>>1;
RayDivisor = tab_divisor[(unsigned char)RayDeltaAngle];
}
I only managed to translate some easy parts but I don't know to solve the FIXME and code the DUNNO in the following code:
Code: Select all
;; RayDeltaAngle = RayBeta - RayAlpha;
lda _RayBeta
sec
sbc _RayAlpha
sta _RayDeltaAngle
sbc #0 // FIXME : won't work if signed angle difference overflows ..
sta _RayDeltaAngle+1
// if (abs(RayDeltaAngle) < 127){ // DUNNO how to code this overflow condition
;; RayDivisor = tab_divisor_acc[(unsigned char)RayDeltaAngle];
ldy _RayDeltaAngle
lda _tab_divisor_acc, y
sta _RayDivisor
// } else {
;; RayDeltaAngle = RayDeltaAngle>>1;
;; RayDivisor = tab_divisor[(unsigned char)RayDeltaAngle];
lda _RayDeltaAngle
cmp #$80
ror
tay
lda _tab_divisor, y
sta _RayDivisor
It's breaking my head